jds – vb.net skill session fall 2004 presented by yuhao lin
Post on 05-Jan-2016
213 Views
Preview:
TRANSCRIPT
JDS – VB.NET Skill JDS – VB.NET Skill SessionSession
Fall 2004Fall 2004
Presented by Presented by YUHAO LINYUHAO LIN
TOPICSTOPICS
• Visual Studio .NET
• VB.NET Code Structure
• VB.NET Language Basics
• Data Access in .NET
What is .NET ?What is .NET ?
• Microsoft® .NET (released 2001) is Microsoft’s new generation "software platform." It's a language-neutral environment for writing programs
• Program in any language, run on one operating system
• Includes comprehensive class libraries that provide rich features
What is VB.NET ?What is VB.NET ?
• Visual Basic .NET
• Next generation of Visual Basic
• A programming language in the .NET framework
• Fully object-oriented
Visual Studio .NETVisual Studio .NET
• Available in Software Lab
• Latest version: 2003
• Integrated Development Environment (IDE) for developing .NET applications
Visual Studio .NETVisual Studio .NET
• Create a new project
• Opening an existing project– Open .sln file– Solution Explorer (design vs code view)– Properties Window– Toolbox: many Controls– Task List
Visual Studio .NETVisual Studio .NET
• Code view– Comments– #Regions: collapse– Intellisense– Run / compile– Debug / step into
• Executable file– Location: bin or obj subdirectory
VB.NET Code StructureVB.NET Code Structure
VB.NET
• Import statements
• Namespace– Class
• Functions / Subs• Properties• Class variables
Java
• Package
• Import statements– Class
• Methods• Accessor methods• Class variables
VB.NET Code StructureVB.NET Code Structure
VB.NETimports System.Data.OleDb
namespace TestNamespacepublic class TestClass
sub NewRecord()…
end sub
function GetRecord() as String
…
return strX
end function
End class
end namespace
Javapackage com.deitel.chp08;
import java.io.*;public class Driver {
void newRecord() {
…
}
String getRecord() {
…
return strX;
}
}
VB.NET LanguageVB.NET Language
• Case insensitive– BUT, always be consistent !!!!!
• No semicolon to end statement• One line per statement, & _ for multi-line
statements:str1 = str2 &_ str1 = str2 & str3
str3
• Very wordy, resembles English language• Good for beginners
Programming ReviewProgramming Review
Car myCar = new Car(“Ford”, “Escort”)
What did I do ??
Common Data TypesCommon Data Types
• Integer
• Double
• Boolean
• String
• Array
Dim j as integer = 0
Dim d as double = -3.55
Dim b as boolean = false
Dim s as string = “Yo ^ !”;
Dim Days( ) As Integer = New Integer(6) {1, 2, 3, 4, 5, 6, 7}‘create an array with 7 elements, starting at index 0
end at 6
Data ConversionData Conversion
• Convert.ToType(var);
Ex: Convert.ToInt32(“45”)
Convert.ToDateTime(“12/31/2000”)
Ex:Dim j as integer
Dim s as string = “16”
If IsNumeric(s) Then ‘ check if s is a valid number
J = Convert.ToInt32(s) ‘ convert s to 32-bit integer, assign to J
End If
OperatorsOperators
DESCRIPTION VB.NET Ex JAVA Ex
String concatenation & str1 = str2 & str3 + str1 = str2 + str3
&= str1 &= str2 += str1 += str2
Addition += int1 += int2 += int1 += int2
Add 1 n/a int1 = int1 + 1 ++ int1++
Comparison >=, <= >=, <=
Equality = If j = 1 == if (j == 1)
Inequality Not If Not j = 1 ! if (j != 1)
<> If j <> 1
MethodsMethods
VB.NETFunction:
Function GetMe(ByVal s as String) as String
‘ note the parameter
s = s.Replace(“h”, “hi”)
Return s
End Function
Subroutine:
Sub SetMe(ByVal s as String)
Dim str1 as String = s
‘ nothing to return
End Sub
JavaReturn Method:
String getMe(String s)
{
s = s.replace(“h”, “hi”);
return s;
}
Void Method:
Void setMe(String s)
{
String str1 = s;
}
PropertiesProperties
VB.NET(1 property)
Dim sCustomerName as String = “”
Property CustomerName() As String
Get
Return sCustomerName
End Get
Set(ByVal Value As String)
sCustomerName = Value
End Set
End Property
Java(2 accessor methods)
String sCustomerName = “”;
String getCustomerName()
{
return sCustomerName;
}
void setCustomerName(String name)
{
sCustomerName = name;
}
LoopsLoops
Dim x as Integer = 0
Do While x < 5
x = x + 1
Loop
Dim x as Integer = 0
While x < 5
x = x + 1
End While
For j as Integer = 0 to 10 Step 1
MsgBox(j)
Next
Dim MyArray() as Integer =
{1, 2, 3, 4, 6, 549}
For Each i as Integer in MyArray
MsgBox(i)
Next
Exception HandlingException Handling
• Syntax:Try
…
Catch var as Exception
…
Finally
…
End Try
• Example:‘ try converting a string to int32, then output
whether the conversion is successful
Dim MyInt as integer
Dim bool as Boolean = true
Try
MyInt = Convert.ToInt32(“Not Possible”)
Catch ex as Exception
MsgBox(“Error: “ & ex.Message)
bool = false
Finally
MsgBox(“Conversion successful: “ & bool)
End Try
Database AccessDatabase Access
• ADO.NET (ActiveX Data Objects)– The .NET way to work with data
• Disconnected Architecture
• 2 Data Providers:– SQL Server: specific provider for SQL Server
• Import System.Data.SqlClient
– OLEDB: everything else, basically…• Import System.Data.OleDb
ADO.NETADO.NET
Data Access ComponentsData Access Components
• Connection string – http://www.able-consulting.com/ADO_Conn.htm
• Connection object
• SQL statement
• Command Object & DataReader– OR –
• Data Adapter & DataSet
Connection StringConnection String
SQL Server Data Provider:
"Network Library=DBMSSOCN;Data Source=111.11.111.111,3333;” & _
“Initial Catalog=epics;User Id=epics;Password=jds"
OLEDB Data Provider (SQL Server):
"Provider=sqloledb;" & _
"Data Source=111.11.111.111,3333;" & _
"Initial Catalog=epics;" & _
"User Id=epics;" & _
"Password=jds"
OLEDB Data Provider (Access):
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=T:\www\mdb\WebInt.mdb"
DataSet vs DataReaderDataSet vs DataReader
• DataReader– Object to access data in a connected,
forward-only, read-only fashion
• DataSet– Data structure to store schema and data in a
disconnected fashion– Useful for editing data offline and later update
to data source
Database ConnectivityDatabase ConnectivityUsing SQL Server Data Provider and DataSet:
Dim strConn as String = "Network Library=DBMSSOCN;Data Source=111.11.111.111,3333;” & _
“Initial Catalog=epics;User Id=epics;Password=jds”
Dim oConn as new SqlConnection(strConn)
Dim strSQL as String = “select * from NODE_PROFILE”
Dim oAdapter as new SqlDataAdapter(strSql, oConn)
Dim ds as new DataSet
oAdapter.Fill(ds, “NODE_PROFILE”)
‘ now we have a DataSet with data from the NODE_PROFILE table
Using OLEDB Data Provider and DataReader:
Dim strConn as String = "Provider=sqloledb;" & "Data Source=111.11.111.111,3333;" & _
"Initial Catalog=epics;" & "User Id=epics;" & "Password=jds“
Dim oConn as new OleDbConnection(strConn)
Dim strSQL as string = “select * from NODE_PROFILE”
Dim oCommand as New OleDbCommand(strSql, oConn)
Dim oReader As OleDbDataReader
oReader = oCommand.ExecuteReader()
‘ now we have a DataReader with data from the NODE_PROFILE table
ADO.NET in JDS.2GO ??ADO.NET in JDS.2GO ??
• Not taking advantage of .NET data providers and data structures
• Using outdated technology: objects from ADODB class
• This semester: possibly transform all of JDS.2GO to ADO.NET-standard
ADO.NETADO.NET
• Hands-on Exercise!!!!!!!!!!!
• Task: build a simple VB.NET application with database connectivity to display the front-end. Based on existing C# code
top related