19
Visual Basic .NET Programming What is What is What is What is ADO ADO ADO ADO.NET NET NET NET? Data Access Object Remote Data Object Active Data Object ADO.NET * Property of STI Page 1 of 19 ADO .Net group of libraries allow applications to read and update information in databases and other sources includes two .NET data providers: SQL Server .NET Data provider OLE DB .NET Data Provider ActiveX Data Object for .Net Framework

MELJUN CORTES Vb.net ado.net part_i

Embed Size (px)

Citation preview

Page 1: MELJUN CORTES Vb.net ado.net part_i

Visual Basic .NET Programming

What is What is What is What is ADOADOADOADO....NETNETNETNET????

Data Access Object

Remote Data Object

Active Data Object

ADO.NET * Property of STIPage 1 of 19

� ADO .Net

� group of libraries

� allow applications to read and update

information in databases and other sources

� includes two .NET data providers:

• SQL Server .NET Data provider

• OLE DB .NET Data Provider

ActiveX Data Object for .Net Framework

Page 2: MELJUN CORTES Vb.net ado.net part_i

Visual Basic .NET Programming

ADOADOADOADO....NETNETNETNETArchitectureArchitectureArchitectureArchitecture

ADO.NET * Property of STIPage 2 of 19

Page 3: MELJUN CORTES Vb.net ado.net part_i

Visual Basic .NET Programming

....NETNETNETNET Data Data Data Data ProvidersProvidersProvidersProviders

� System.Data.SQLClient namespace

� used for SQL Server 7.0 and later

databases

� System.Data.OleDb namespace

� used for all other databases

ADO.NET * Property of STIPage 3 of 19

� Components:

� Connection object

� Command object

� DataReader object

� DataAdapter object

Page 4: MELJUN CORTES Vb.net ado.net part_i

Visual Basic .NET Programming

Connection Connection Connection Connection ObjectObjectObjectObject

� creates the connection to the database

� connection classes:

� SqlConnection object

� OleDbConnection object

� Steps in connecting to a database:

ADO.NET * Property of STIPage 4 of 19

� Steps in connecting to a database:

� Set the connection type

� Specify the data source

� Connect to the data source

Page 5: MELJUN CORTES Vb.net ado.net part_i

Visual Basic .NET Programming

SqlConnection SqlConnection SqlConnection SqlConnection ObjectObjectObjectObject

� has a property that contains all the information in establishing a connection

� ConnectionString property

� consists of name-value pairs

� Some common keywords used are:

Connection Timeout / Connect Timeout

ADO.NET * Property of STIPage 5 of 19

� Connection Timeout / Connect Timeout

� Initial Catalog

� User ID

� Password / Pwd

� Data Source / Server/ Address / Addr /Network Address

� Integrated Security / Trusted_Connection

Page 6: MELJUN CORTES Vb.net ado.net part_i

Visual Basic .NET Programming

Example: SQL ClientExample: SQL ClientExample: SQL ClientExample: SQL Client....NETNETNETNET Data ProviderData ProviderData ProviderData Provider

Imports System

Imports System.Data.SqlClient

Module SQLClientDemo

Public Sub Main()

Dim strConnection As String =

“Integrated Security=True;Data

Source=Localhost;Initial

ADO.NET * Property of STIPage 6 of 19

Source=Localhost;Initial

Catalog=Pubs;”

Dim conServer As SqlConnection =

New SqlConnection

conServer.ConnectionString =

strConnection

conserver.Open()

End Sub

End Module

Page 7: MELJUN CORTES Vb.net ado.net part_i

Visual Basic .NET Programming

OleDbConnectionOleDbConnectionOleDbConnectionOleDbConnectionObjectObjectObjectObject

� uses a ConnectionString property similar

to SQLConnection but with some

additional keywords such as Provider,

URL, Remote Provider and Remote Server

Imports System

Imports System.Data.OleDb

Module OleDbDemo

ADO.NET * Property of STIPage 7 of 19

Public Sub Main()

Dim strConnection As String =

“Provider=Microsoft.Jet.OLEDB.4.

0;Data Source=C:\NWind.MDB”

Dim conServer As OleDbConnection

= New OleDbConnection

conServer.ConnectionString =

strConnection

conserver.Open()

End Sub

End Module

Page 8: MELJUN CORTES Vb.net ado.net part_i

Visual Basic .NET Programming

Creating Creating Creating Creating CommandsCommandsCommandsCommands

� ADO.NET Command object is used to

execute commands

� SqlCommand

� used for SQL Server database

� OleDBCommand

� used for all other types of databases

� Ways to create a Command:

ADO.NET * Property of STIPage 8 of 19

� Ways to create a Command:

� use the Command constructor and pass the

Connection object as an argument

� use the CreateCommand method of the

Connection object

Page 9: MELJUN CORTES Vb.net ado.net part_i

Visual Basic .NET Programming

Creating Creating Creating Creating CommandsCommandsCommandsCommands

Imports System

Imports System.Data.SqlClient

Module SQLClientDemo

Public Sub Main()

Dim strConnection As String =

“Integrated Security=True;Data

Source=Localhost;Initial

Catalog=Pubs;”

Dim conServer As SqlConnection = New

ADO.NET * Property of STIPage 9 of 19

Dim conServer As SqlConnection = New

SqlConnection

conServer.ConnectionString =

strConnection

conserver.Open()

‘the code that will create a Command

Dim commSQL As SqlCommand = New

SqlCommand

commSql.Connection = conserver

commSql.Commandtext = “Select

count(*) from Employees”

End Sub

End Module

Page 10: MELJUN CORTES Vb.net ado.net part_i

Visual Basic .NET Programming

Executing Executing Executing Executing CommandsCommandsCommandsCommands

� Methods used to execute a Command:

� ExecuteReader

• used when the query will return a stream of

data

� ExecuteScalar

• used when the query return a single value

ADO.NET * Property of STIPage 10 of 19

• used when the query return a single value

� ExecuteNonQuery

• used when the query will not return a

result

� ExecuteXMLReader

• used when the query includes a valid FOR

XML clause and it is valid only in

SqlCommand object

Page 11: MELJUN CORTES Vb.net ado.net part_i

Visual Basic .NET Programming

Imports System

Imports System.Data.SqlClient

Module SQLClientDemo

Public Sub Main()

Dim strConnection As String =

“Integrated Security=True;Data

Source=Localhost;Initial Catalog=Pubs;”

Dim conServer As SqlConnection = New

SqlConnection

Example of Example of Example of Example of Executing CommandsExecuting CommandsExecuting CommandsExecuting Commands

ADO.NET * Property of STIPage 11 of 19

SqlConnection

conServer.ConnectionString =

strConnection

conserver.Open()

Dim commSQL As SqlCommand = New

SqlCommand

commSql.Connection = conserver

commSql.Commandtext = “Select count(*)

from Employees”

‘the code that will query a database and

‘retrieve data

MessageBox.Show(commSql.ExecuteScalar()

.ToString)

End Sub

End Module

Page 12: MELJUN CORTES Vb.net ado.net part_i

Visual Basic .NET Programming

DataReader DataReader DataReader DataReader ObjectObjectObjectObject

� DataReader object

� used to read a single row of data at a time

• SqlDataReader

• OleDbDataReader

� Create an instance of the DataReader

object:

� ExecuteReader method

ADO.NET * Property of STIPage 12 of 19

� ExecuteReader method

� Read method

� Get Method

� Example of Read method

Dim datRead As SqlDataReader = New

SqlDataReader

datRead = commSql.ExecuteReader()

Do Until datRead.Read = False

MessageBox.Show(datRead(1).ToString

& “ “ & datRead(0).ToString)

Loop

datRead.Close()

Page 13: MELJUN CORTES Vb.net ado.net part_i

Visual Basic .NET Programming

DataReader DataReader DataReader DataReader ObjectObjectObjectObject

� Example of Get Method

Dim datRead As SqlDataReader = New

SqlDataReader

datRead = commSql.ExecuteReader()

Do Until datRead.Read = False

ADO.NET * Property of STIPage 13 of 19

MessageBox.Show(datRead.GetString

(1) & “ “ &

datRead.GetString(0))

Loop

Page 14: MELJUN CORTES Vb.net ado.net part_i

Visual Basic .NET Programming

DataAdapter DataAdapter DataAdapter DataAdapter ObjectObjectObjectObject

� used to link between data source and

cached tables

� Ways to create a DataAdapter object:

� Using an existing Connection object

� Using a closed connection

ADO.NET * Property of STIPage 14 of 19

Page 15: MELJUN CORTES Vb.net ado.net part_i

Visual Basic .NET Programming

DataAdapter DataAdapter DataAdapter DataAdapter ObjectObjectObjectObject

� Example of using an existing Connection objectImports System

Imports System.Data.SqlClient

Module SQLClientDemo

Public Sub Main()

Dim strConnection As String = “Integrated Security=True;Data Source=Localhost;Initial Catalog=Books;”

Dim conServer As SqlConnection = New SqlConnection

ADO.NET * Property of STIPage 15 of 19

SqlConnection

conServer.ConnectionString = strConnection

conserver.Open()

Dim commSQL As SqlCommand = New SqlCommand

commSql.Connection = conserver

commSql.Commandtext = “Select count(*) from Catalogue”

‘the code that will create a DataAdapter

Dim adaptSQL As SqlDataAdapter = New SqlDataAdapter

adaptSQL.SelectCommand = commSQL

End Sub

End Module

Page 16: MELJUN CORTES Vb.net ado.net part_i

Visual Basic .NET Programming

DataAdapter DataAdapter DataAdapter DataAdapter ObjectObjectObjectObject

� Example of using a closed connection

Private conServer As

SqlClient.SqlConnection

Public Sub Form1_Load(ByVal sender As

System.Object, ByVal e As

System.EventArgs) Handles MyBase.Load

conserver = New

SqlClient.SqlConnection()

Dim strConnection As String =

ADO.NET * Property of STIPage 16 of 19

Dim strConnection As String =

“Integrated Security=True;Data

Source=Localhost;Initial

Catalog=Books;”

conServer.ConnectionString =

strConnection

conserver.Open()

End Sub

Private Sub Button1_Click ByVal sender As

System.Object, ByVal e As

System.EventArgs) Handles Button1.Click

‘the code that will create a DataAdapter

Dim adaptSQL As

SqlClient.SqlDataAdapter(“Select

count(*) from Catalogue”, conserver)

End Sub

Page 17: MELJUN CORTES Vb.net ado.net part_i

Visual Basic .NET Programming

Populating the Populating the Populating the Populating the DataTableDataTableDataTableDataTable

� DataTable class

� represents one table of in-memory data

� a central object in the ADO.NET library

� Fill method

� used by passing a DataSet and optionally

the required DataTable as parameters

ADO.NET * Property of STIPage 17 of 19

the required DataTable as parameters

� used to fill and update multiple DataSets

Page 18: MELJUN CORTES Vb.net ado.net part_i

Visual Basic .NET Programming

Populating the Populating the Populating the Populating the DataTableDataTableDataTableDataTable

� Example of Fill method

Private conServer As

SqlClient.SqlConnection

Public Sub Form1_Load(ByVal sender

As System.Object, ByVal e As

System.EventArgs) Handles

MyBase.Load

ADO.NET * Property of STIPage 18 of 19

MyBase.Load

conserver = New

SqlClient.SqlConnection()

Dim strConnection As String =

“Integrated Security=True;Data

Source=Localhost;Initial

Catalog=Books;”

conServer.ConnectionString =

strConnection

conserver.Open()

End Sub

Page 19: MELJUN CORTES Vb.net ado.net part_i

Visual Basic .NET Programming

Populating the Populating the Populating the Populating the DataTableDataTableDataTableDataTable

� Example of Fill method (con’t)

Private Sub Button1_Click ByVal

sender As System.Object, ByVal

e As System.EventArgs) Handles

Button1.Click

Dim adaptSQL As

SqlClient.SqlDataAdapter(“Select

ADO.NET * Property of STIPage 19 of 19

SqlClient.SqlDataAdapter(“Select

count(*) from Catalogue”,

conserver)

‘the code that will fill the

DataSet

Dim datBooks As New DataSet()

adaptSQL.Fill(datBooks,

“MyTable”)

‘manipulate data locally using

DataSet

adaptSQL.Update(datBooks,

“MyTable”)

End Sub