+ All Categories
Home > Documents > JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

Date post: 05-Jan-2016
Category:
Upload: pierce-boone
View: 213 times
Download: 0 times
Share this document with a friend
26
JDS – VB.NET Skill JDS – VB.NET Skill Session Session Fall 2004 Fall 2004 Presented by Presented by YUHAO LIN YUHAO LIN
Transcript
Page 1: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

JDS – VB.NET Skill JDS – VB.NET Skill SessionSession

Fall 2004Fall 2004

Presented by Presented by YUHAO LINYUHAO LIN

Page 2: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

TOPICSTOPICS

• Visual Studio .NET

• VB.NET Code Structure

• VB.NET Language Basics

• Data Access in .NET

Page 3: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

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

Page 4: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

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

Page 5: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

Visual Studio .NETVisual Studio .NET

• Available in Software Lab

• Latest version: 2003

• Integrated Development Environment (IDE) for developing .NET applications

Page 6: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

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

Page 7: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

Visual Studio .NETVisual Studio .NET

• Code view– Comments– #Regions: collapse– Intellisense– Run / compile– Debug / step into

• Executable file– Location: bin or obj subdirectory

Page 8: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

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

Page 9: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

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;

}

}

Page 10: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

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

Page 11: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

Programming ReviewProgramming Review

Car myCar = new Car(“Ford”, “Escort”)

What did I do ??

Page 12: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

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

Page 13: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

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

Page 14: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

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

Page 15: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

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;

}

Page 16: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

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;

}

Page 17: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

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

Page 18: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

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

Page 19: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

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

Page 20: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

ADO.NETADO.NET

Page 21: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

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

Page 22: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

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"

Page 23: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

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

Page 24: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

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

Page 25: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

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

Page 26: JDS – VB.NET Skill Session Fall 2004 Presented by YUHAO LIN.

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


Recommended