meljun cortes vb.net handout ado.net_vb.net_handout_part_i
Post on 14-Apr-2017
31 Views
Preview:
TRANSCRIPT
Visual Basic .NET Programming
ADO.NET * Property of STI Page 1 of 12
TOPIC TITLE: ADO.NET Specific Objectives: At the end of the topic session, the students are expected to: Cognitive:
1. Explain how ADO .NET comes about and identify its uses. 2. Explain the ADO .Net architecture. 3. Enumerate and identify the uses of the namespaces and
components involve in the .NET Data Providers. 4. Identify the use of the Connection object and cite the steps on
how to connect to a database. 5. Identify the use of the Command object and cite some ways on
how to execute commands and return data from a data source. 6. Identify the use of the DataReader object and explain the use of
Read and Get methods in retrieving data. 7. Identify the use of the DataAdapter object and cite the ways on
creating this object. Affective:
1. Listen to others with respect. 2. Participate in class discussions actively.
MATERIALS/EQUIPMENT:
o topic slides o OHP
TOPIC PREPARATION:
o Have the students research on the following: � ADO.Net � .NET Data Providers � Connection object � Command object � DataReader object � DataAdapter object
o It is imperative for the instructor to incorporate various kinds of teaching strategies while discussing the suggested topics. The instructor may use the suggested learning activities below to facilitate a thorough and creative discussion of the topic.
o Prepare the slides to be presented in the class.
TOPIC PRESENTATION: The topic will revolve around ActiveX Data Objects for the .Net Framework. This will be the suggested flow of discussion for the course topic:
1. Introduce to the students the topics to be covered in this session.
2. Ask the students to share their research about ADO .Net. 3. Explain what ADO.NET is. Also, present and explain the
ADO .Net Architecture. 4. Ask the students to tell something about .Net Data Providers. 5. Discuss the .NET Data Providers and give an overview of its
components.
Visual Basic .NET Programming
ADO.NET * Property of STI Page 2 of 12
6. Ask the students to identify the use of the Connection object. 7. Explain the Connection object as well as the steps to connect to
a database. Provide sample codes on how to connect to a SQL Server database and to an Access database.
8. Ask the students to identify the use of the Command object. 9. Explain the Command object and cite the methods on how to
create commands. Provide sample codes for each method 10. Enumerate and describe the methods used in executing
commands. Provide an example. 11. Ask the students to identify the use of the DataReader object. 12. Explain the DataReader object and discuss how to create a
read-only data stream. Provide sample codes using the Read and Get methods.
13. Ask the students to identify the use of the DataAdapter object. 14. Identify the use of the DataAdapter object and mention some
ways of creating this object. Provide sample codes for each. 15. Conduct Quiz #5.
Visual Basic .NET Programming
ADO.NET * Property of STI Page 3 of 12
What is ADO.NET? Page 1 of 19
ADO.NET
Visual Basic .NET Programming
* Property of STIPage 1 of 19
What is What is What is What is ADO.NET?ADO.NET?ADO.NET?ADO.NET?
� 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
Data Access Object
Remote Data Object
Active Data Object
ActiveX Data Object for .Net Framework
What is ADO.NET? The first data access model named DAO (Data Access Object) was created for local databases. This data access model had performance and functionality issues. Some of the issues in using DAO are speed, support limited number of users and since it is older technology as it runs best only in 16-bit environment. It has limited functionality as it does not have interface to e-mail, file systems and custom business objects. Then, RDO (Remote Data Object) and ADO (Active Data Object) came into the picture, which were designed for Client Server architectures. However, ADO was successful and took over RDO. With ADO, all the data is contained in a recordset object which had problems when implemented on the network and penetrating firewalls. ADO was a connected data access, which means that when a connection to the database is established the connection remains open until the application is closed. This raised concerns about database security and network traffic. Also, as databases are becoming increasingly important and as they are serving more people, a connected data access model makes us think about its productivity. For example, an application with connected data access may do well when connected to two clients, the same may do poorly when connected to 10 clients and might be unusable when connected to 100 clients or more. Also, open database connections use system resources to a maximum extent making the system performance less effective. To solve some of the problems mentioned above, ADO .NET came into existence. ADO .NET addresses the above mentioned problems by maintaining a disconnected database access model. ActiveX Data Objects for the .NET Framework (ADO.NET) is a set of classes or group of libraries that allow applications to read and update information in databases and other sources. It provides access to various data sources such as Microsoft SQL Server, OLE-DB compliant databases, MS Access, Oracle and XML. ADO.NET uses NET data providers to provide interface in the application to data sources. The Data Provider is responsible for providing and maintaining the connection to the database. It is a set of related components that work together to provide data in an efficient and performance-driven manner. It includes two .NET data providers namely SQL Server .NET Data provider and OLE DB .NET Data Provider. [What is ADO.NET, Page 1 of 19]
Visual Basic .NET Programming
ADO.NET * Property of STI Page 4 of 12
ADO.NET Architecture Page 2 of 19
ADO.NET
Visual Basic .NET Programming
* Property of STIPage 2 of 19
ADO.NET ADO.NET ADO.NET ADO.NET ArchitectureArchitectureArchitectureArchitecture
ADO.NET Architecture
The figure above shows the ADO.NET architecture which is divided into two logical entities namely command execution and caching. Command execution requires features like connectivity, execution, and reading of results. These features are enabled with .NET Data Providers. The DataSet handles caching of results. The .NET Data Provider enables connectivity and command execution to underlying data sources. Note that these data sources do not have to be relational databases. Once a command has been executed the results can be read using a DataReader. A DataReader provides efficient forward-only stream level access to the results. In addition, results can be used to render a DataSet a DataAdapter. The DataSet object represents a disconnected cache of data. The DataSet is a disconnected, in-memory representation of data. This cache is made up of DataTables and DataRelations that represent the results of the command. The DataSet tracks changes to the underlying data in the cache. Changes can be submitted back to the original data source by using the DataAdapter and applicable Insert, Update, or Delete commands. The DataSet also provides a direct XML view of the underlying data in the cache. [ADO.NET Architecture, Page 2 of 19]
.NET Data Providers Page 3 of 19
ADO.NET
Visual Basic .NET Programming
* Property of STIPage 3 of 19
.NET Data .NET Data .NET Data .NET Data ProvidersProvidersProvidersProviders
� System.Data.SQLClient namespace
� used for SQL Server 7.0 and later
databases
� System.Data.OleDb namespace
� used for all other databases
� Components:
� Connection object
� Command object
� DataReader object
� DataAdapter object
.NET Data Providers System.Data namespace consists mainly of classes that comprise the ADO.NET architecture. The System.Data.SQLClient namespace and the System.Data.OLEDB are the two namespaces that can be used to get access to different data sources. The first one is used to access SQL Server 7.0 and later databases and the latter is used to access any data sources that use the OLE DB. These providers contain four main objects that you can use to connect to a data source, read the data and manipulate the data. These four components are:
• Connection object – which provides a connection to the database
• Command object – which is used to execute a command
• DataReader object – which provides a forward-only, read only,
Visual Basic .NET Programming
ADO.NET * Property of STI Page 5 of 12
connected recordset
• DataAdapter object – which populates a disconnected DataSet with data and performs update
[.NET Data Providers, Page 3 of 19]
Connection Object Page 4 of 19
ADO.NET
Visual Basic .NET Programming
* Property of STIPage 4 of 19
Connection Connection Connection Connection ObjectObjectObjectObject
� creates the connection to the database
� connection classes:
� SqlConnection object
� OleDbConnection object
� Steps in connecting to a database:
� Set the connection type
� Specify the data source
� Connect to the data source
Connection Object The Connection object creates the connection to the database. Microsoft Visual Studio .NET provides two types of Connection classes namely the SqlConnection object, which is designed specifically to connect to Microsoft SQL Server 7.0 or later, and the OleDbConnection object, which can provide connections to a wide range of database types like Microsoft Access and Oracle. The Connection object contains all of the information required to open a connection to the database. There are three basic steps in connecting to a database. These are as follows:
1. Set the connection type. 2. Specify the data source. 3. Connect to the data source.
To achieve step 1, the Connection object is used to connect to a specific data source. Specifically, the SqlConnection and OleDbConnection objects are used for this purpose. After the connection type has been set, the ConnectionString property is used to specify the source database and other information used to establish the connection. Lastly, each Connection object supports an Open method that opens the connection after the connection properties have been set. It also has a Close method that is used to close the database connection. [Connection Object, 4 of 19]
SqlConnection Object Page 5 of 19
ADO.NET
Visual Basic .NET Programming
* Property of STIPage 5 of 19
SqlConnectionSqlConnectionSqlConnectionSqlConnectionObjectObjectObjectObject
� 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
� Initial Catalog
� User ID
� Password / Pwd
� Data Source / Server/ Address / Addr /
Network Address
� Integrated Security / Trusted_Connection
SqlConnection Object
The SqlConnection object has a property that contains all the information in establishing a connection. This property is the
ConnectionString property that consists of name-value pairs providing the information required when connecting to a data source. The most commonly used name-value pairs in ConnectionString
property is shown below:
Name (Keyword) Default Value Description
Connection Timeout / Connect Timeout
15 seconds Delay for a connection to succeed before returning an error.
Initial Catalog None Name of the database
User ID None SQL server logon account
Password / Pwd None SQL server password
Data Source / Server/ Address / Addr / Network Address
None Name or address of SQL server
Integrated Security / Trusted_Connection
False Determines if the connection is a secure connection.
Visual Basic .NET Programming
ADO.NET * Property of STI Page 6 of 12
[SqlConnection Object, Page 5 of 19]
Example SQL Client .NET Data Provider Page 6 of 19
ADO.NET
Visual Basic .NET Programming
* Property of STIPage 6 of 19
Example:Example:Example:Example: SQL ClientSQL ClientSQL ClientSQL Client.NET Data Provider.NET Data Provider.NET Data Provider.NET Data Provider
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
conServer.ConnectionString =
strConnection
conserver.Open()
End Sub
End Module
Example: SQL Client .NET Data Provider 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
conServer.ConnectionString = strConnection
conserver.Open()
End Sub
End Module
The above example shows how to connect to a SQL Server database by using the SQL Client .NET Data Provider. To support a connection to a
database, the .NET Framework provides the SqlConnection class that
is defined in the System.Data.SqlClient namespace. The Imports
statements include this namespace in the file.
[Example SQL Client .NET Data Provider, Page 6 of 19]
OleDBConnection Page 7 of 19
ADO.NET
Visual Basic .NET Programming
* Property of STIPage 7 of 19
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
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
OleDbConnection The OleDbConnection object uses a ConnectionString property
similar to SQLConnection but with some additional keywords that is
now required. These keywords are Provider, URL, Remote
Provider and Remote Server. An example is shown below: Imports System
Imports System.Data.OleDb
Module OleDbDemo
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
The code above shows how to connect to an Access database by using the OLE DB .NET Data Provider. [OleDbConnection, Page 7 of 19]
Visual Basic .NET Programming
ADO.NET * Property of STI Page 7 of 12
Creating Commands Page 8 of 19
ADO.NET
Visual Basic .NET Programming
* Property of STIPage 8 of 19
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:
� use the Command constructor and pass the
Connection object as an argument
� use the CreateCommand method of the
Connection object
Creating Commands Page 9 of 19
ADO.NET
Visual Basic .NET Programming
* Property of STIPage 9 of 19
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
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
Creating Commands The ADO.NET Command object is used to execute commands. The
SqlCommand is used for SQL Server database and the OleDBCommand for all other types of databases. There are two ways to create a Command:
1. Using the Command constructor and pass the Connection
object as an argument.
2. Use the CreateCommand method of the Connection object
The Command object has a property named CommandText that can be used to set and retrieve SQL statement being executed. An example is shown below. The line of codes in italic represents the creation of the Command using the step 1 described above. 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
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
What follows are the lines of codes that represent the creation of the Command using the step 2 described above.
‘the code that will create a Command using
CreateCommand
Dim commSQL As SqlCommand = New SqlCommand
commSql = conserver.CreateCommand
commSql.Commandtext = “Select count(*) from
Employees
[Creating Commands, Pages 8-9 of 19]
Executing Commands There are four methods that can be used to execute a Command as follows:
1. ExecuteReader
2. ExecuteScalar
3. ExecuteNonQuery
Visual Basic .NET Programming
ADO.NET * Property of STI Page 8 of 12
Executing Commands Page 10 of 19
ADO.NET
Visual Basic .NET Programming
* Property of STIPage 10 of 19
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
� 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
4. ExecuteXMLReader
ExecuteReader – This method is used when the query will return a
stream of data such as a Select statement. This method returns a set of records that are encapsulated in a SqlDataReader or OleDbDataReader object.
ExecuteScalar – This method is used when the query return a single
value. Usually, this method is used in Select statement returning an aggregate value. The first column of the first row of the result set is the only data that is selected ignoring the other data returned.
ExecuteNonQuery – This method is used when the query will not return a result such as an Insert statement.
ExecuteXMLReader – This method is used when the query includes a
valid FOR XML clause and it is valid only in SqlCommand object. [Executing Commands, Page 10 of 19]
Example of Executing Commands Page 11 of 19
ADO.NET
Visual Basic .NET Programming
* Property of STIPage 11 of 19
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
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
Example of Example of Example of Example of Executing CommandsExecuting CommandsExecuting CommandsExecuting Commands
Example of Executing Commands 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
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
The code above establishes a connection, creates commands and then
executes the command by using the ExecuteScalar method. A message box is used to display the number of employees. [Example of Executing Commands, Page 11 of 19]
DataReader Object
The DataReader object is used to read a single row of data at a time. There are two versions of this object namely SqlDataReader for SQL Server databases and the OleDbDataReader for other data sources.
To create an instance of the DataReader object, the ExecuteReader
Visual Basic .NET Programming
ADO.NET * Property of STI Page 9 of 12
DataReader Object Page 12 of 19
ADO.NET
Visual Basic .NET Programming
* Property of STIPage 12 of 19
DataReaderDataReaderDataReaderDataReaderObjectObjectObjectObject
� DataReader object
� used to read a single row of data at a time
• SqlDataReader
• OleDbDataReader
� Create an instance of the DataReader
object:
� 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()
DataReader Object Page 13 of 19
ADO.NET
Visual Basic .NET Programming
* Property of STIPage 13 of 19
DataReaderDataReaderDataReaderDataReaderObjectObjectObjectObject
� Example of Get Method
Dim datRead As SqlDataReader = New
SqlDataReader
datRead = commSql.ExecuteReader()
Do Until datRead.Read = False
MessageBox.Show(datRead.GetString
(1) & “ “ &
datRead.GetString(0))
Loop
method of the Command object can be used. Then, the Read method can be invoked to obtain the data in the rows. The columns in the row can be access by name, ordinal number or native type. An example is shown below to demonstrate how DataReader is used to read a single
row of data at a time. A loop is used to traverse to each row of data. 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
conServer.ConnectionString = strConnection
conserver.Open()
Dim commSQL As SqlCommand = New SqlCommand
commSql.Connection = conserver
commSql.Commandtext = “Select count(*) from
Employees”
‘the code that will retrieve data by using the
‘DataReader object
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()
End Sub
End Module
The Close method of the DataReader object is called to ensure that
the data stream is closed before accessing any output or return parameters.
Another way of retrieving data in columns is by using the Get methods.
The Get method is used when you know the data types of columns.
This will enhance performance because no type conversion is required.
The code below shows how to use the GetString method to retrieve
data. Dim datRead As SqlDataReader = New SqlDataReader
datRead = commSql.ExecuteReader()
Do Until datRead.Read = False
MessageBox.Show(datRead.GetString(1) & “ “ &
datRead.GetString(0))
Loop
[DataReader Object, Pages 12-13 of 19]
Visual Basic .NET Programming
ADO.NET * Property of STI Page 10 of 12
DataAdapter Object Page 14 of 19
ADO.NET
Visual Basic .NET Programming
* Property of STIPage 14 of 19
DataAdapterDataAdapterDataAdapterDataAdapterObjectObjectObjectObject
� used to link between data source and
cached tables
� Ways to create a DataAdapter object:
� Using an existing Connection object
� Using a closed connection
DataAdapter Object Page 15 of 19
ADO.NET
Visual Basic .NET Programming
* Property of STIPage 15 of 19
DataAdapterDataAdapterDataAdapterDataAdapterObjectObjectObjectObject
� Example of using an existing Connection objectImports System
Imports System.Data.SqlClient
Module SQLClientDemo
Public Sub Main()
Dim strConnection As String = “Integrated Security=True;DataSource=Localhost;InitialCatalog=Books;”
Dim conServer As SqlConnection = New 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
DataAdapter Object Page 16 of 19
ADO.NET
Visual Basic .NET Programming
* Property of STIPage 16 of 19
DataAdapterDataAdapterDataAdapterDataAdapterObjectObjectObjectObject
� 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 =
“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
DataAdapter Object
The DataAdapter object is used to link between data source and cached tables. It is mainly used to retrieve data and insert it into
DataTable objects within a DataSet. A DataSet is a new object for
the caching of data on the client computer. The DataAdapter is also
used to update changes from the DataSet back into the data source. There are two ways to create a DataAdapter object:
1. Using an existing Connection object 2. Using a closed connection
To use an existing Connection object, create a Command object within
a Connection object. Then, assign the SelectCommand property of
the previously instantiated DataAdapter object to that command. An example code is shown below: Imports 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
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
To use a closed connection, instantiate the DataAdapter object
passing a query string and a Connection object. The DataAdapter
will check if the Connection is open. If it is not, the DataAdapter will
open and close it when the method call is complete. Use this method if the Connection object property has been set and need only to open
the connection to populate the data tables. An example code is shown below: 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 = “Integrated
Security=True;Data Source=Localhost;Initial
Catalog=Books;”
conServer.ConnectionString = strConnection
conserver.Open()
Visual Basic .NET Programming
ADO.NET * Property of STI Page 11 of 12
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
[DataAdapter Object, Pages 14-16 of 19]
Populating the DataTable Page 17 of 19
ADO.NET
Visual Basic .NET Programming
* Property of STIPage 17 of 19
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
� used to fill and update multiple DataSets
Populating the DataTable Page 18 of 19
ADO.NET
Visual Basic .NET Programming
* Property of STIPage 18 of 19
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
conserver = New
SqlClient.SqlConnection()
Dim strConnection As String =
“Integrated Security=True;Data
Source=Localhost;Initial
Catalog=Books;”
conServer.ConnectionString =
strConnection
conserver.Open()
End Sub
Populating the DataTable
The DataTable class represents one table of in-memory data. It is a
central object in the ADO.NET library. DataTable will be discussed
further in the discussion of the DataSet.
The Fill method of the DataAdapter is used by passing a DataSet
and optionally the required DataTable as parameters. It is used to fill
and update multiple DataSets. Once data is acquired, you can work
with the data and if necessary you can use the Update method to synchronize the changes back to the data source. An example is shown below. The code shows how to fill a DataTable.
Note the line of code that invokes the Fill method. It has two
parameters namely the instance of the DataSet and the name of the
DataTable. 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 = “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
Dim adaptSQL As 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
[Populating the DataTable, Pages 17-19 of 19]
Visual Basic .NET Programming
ADO.NET * Property of STI Page 12 of 12
Populating the DataTable Page 19 of 19
ADO.NET
Visual Basic .NET Programming
* Property of STIPage 19 of 19
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
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
EVALUATION/GENERALIZATION:
• DAO is a set of objects that enables client applications to programmatically access data and is used to access local databases.
• RDO and ADO are new technologies used to access client-server based databases.
• The ADO.NET architecture is divided into two logical entities namely command execution and caching.
• The ADO.Net is the technology used to access data sources.
• There are two namespaces in ADO.Net namely the System,Data.SQLClient and System.Data.OleDb.
• The .NET data providers contain four main objects namely Connection, Command, DataReader and DataAdapter,
• To connect to a database, you perform the following: o Set the connection type. o Specify the data source. o Connect to the data source.
• The Connection object is used to establish database connection.
• The Command object is used to create and execute SQL statement.
• The DataReader object is used to read a single row of data.
• The DataAdapter object is used to link between data source and cached tables.
REFERENCES:
� Microsoft Official Course, (2002), 2373B: Programming with Microsoft Visual Basic .NET, Microsoft Corporation
� Holzner, Steven, (2003), Sams teach yourself Microsoft Visual Basic.Net 2003 in 21 days, USA, Sams Publishing
� Liberty, Jesse, (2003), Learning Visual Basic .NET, USA, O'Reilly & Associates, Inc
top related