Beruflich Dokumente
Kultur Dokumente
NET
by Steven Nicolaou
Microsoft Certified Solution Developer
Microsoft Certified Professional SQL Server, Visual Basic Desktop & Distributed
Introduction to ADO.NET
For those of you familiar with ActiveX Data Objects (ADO) in Visual Basic, you will be well aware of the concept of
the Connection, the Command and the Recordset. These are the primary classes that constitute ADO’s data
access technology. In the .NET framework, ADO has been through major reengineering and has been renamed
to ADO.NET to reflect its position as the underlying data framework for .NET. Here is a quick overview.
The Connection and Command are still there, but the Recordset has been replaced by the DataSet and we have
a new Object called a DataAdapter , which is a toolset that handles processing between a Command and a
Connection . There is also a new Object called a DataReader, which is essentially what would have been a Read-
Only Forward -Only Cursor Records et in ADO and is the most efficient way to get data.
You will notice that the DataSet has a DataTable collection, which reveals its ability to hold resultsets from more
than one table, a big improvement over ADO Recordsets. Moreover, the tables can be related through
Relationship objects, which allow the programmer to perform joins without going back to the database. In addition
to table relationships, ADO.NET is aware of Primary Key, Foreign Key and Unique constraints, as well as
Autonumber (IDENTITY/SEQUENCE) fields.
A very popular way of working with data in VB and ADO was to keep the connection open, while making changes
directly to the database. ADO.NET encourages greater use of the disconnected model. The programmer should
keep a connection to the database open for as little as possible. This improves scalability and makes more sense
in a web-enabled system. The process of retrieving and updating data should be as follows:
1. Open Connection
2. Get DataSet
3. Close Connection
4. Perform Operations on the DataSet
When you are ready to commit the changes back to the database:
5. Open Connection
6. Submit Changes from DataSet
7. Close Connection
And so on.
In the diagram above, Connected Objects are the ones that are normally used while the Connection to the
database is open and Disconnected Objects are the ones that can be used once the connection is closed.
ADO.NET with VB.NET and C# Steven Nicolaou
ADO.NET
After our brief overview with ADO.NET let’s have a look at how our source code would look. I have provided
examples in both VB.NET and C#. I will be assuming Access as the back-end and will be going through
OLE DB. ADO.NET has a specialised set of classes that are optimised for SQL Server (such as
SqlConnection), but for Access we need to use the regular OLE DB classes.
VB.NET
‘Create the OleDbConnection Object
‘DBPath is a fully qualified path to the MDB File
Private m_Conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
DBPath)
Now the Connection is open, we have several options, each of which I will demonstrate. Most of these
objectives can work using a DataSet, but the new methods have been provided to make specialised
operations more efficient. For example, the system shouldn’t be made to go through the trouble of building a
complete DataSet in memory, if all we need is a single value.
Objective Action
Get a DataSet that can be used offline Use a DataAdapter and call Fill
Get a DataReader (can only be used online) Use a Command and call ExecuteReader
Perform a non-DataSet operation on the Database, Use a Command and call
such as UPDATE, INSERT, or DELE TE ExecuteNonQuery
Get a Single value from a query, such as “SELECT Use a Command and call ExecuteScalar
COUNT(*)”
The Connection is closed and the DataSet is now filled with data and ready for processing. To better
understand the code below, keep in mind the object hierarchy. A DataSet contains many Tables, which
contain many Rows, which contain Fields.
DataSet
L Tables as DataTable
L Rows as DataRow
The entire DataSet object model has been attached in Appendix A for your convenience.
Page 2
ADO.NET with VB.NET and C# Steven Nicolaou
VB.NET
Dim emp As DataRow ‘Single Row in the DataSet
For Each emp In m_ds.Tables(0).Rows
EmployeeId = emp("EmployeeId")
FirstName = emp(“FirstName”)
LastName = emp(“LastName”)
Console.WriteLine(CustomerId,FirstName,LastName) ‘Print out the fields
Next row
C#
foreach(DataRow emp in m_ds.Tables[0].Rows)
{
EmployeeId = emp["EmployeeId"];
FirstName = emp[“FirstName”];
LastName = emp[“LastName”];
Console.WriteLine(CustomerId,FirstName,LastName); //Print out the fields
}
VB.NET
Dim m_cmd As As New OleDbCommand(sqlText, m_Conn)
Dim dr As OleDbDataReader
dr = m_cmd.ExecuteReader(CommandBehavior.CloseConnection)
C#
private OleDbCommand m_cmd = new OleDbCommand(sqlText, m_Conn);
private OleDbDataReader dr = m_cmd.ExecuteReader(CommandBehavior.CloseConnection);
To read the records from a DataReader, we need to call the Read method and simply retrieve the fields by
name. We keep calling until Read returns False, at which point we have read all records.
VB.NET
While dr.Read()
EmployeeId = dr("EmployeeId")
FirstName = dr("FirstName")
LastName = dr("LastName")
Console.WriteLine(CustomerId,FirstName,LastName) ‘Print out the fields
End While
dr.Close() ‘Close the DataReader and release the Connection
C#
while (dr.Read())
{
EmployeeId = dr["EmployeeId"];
FirstName = dr["FirstName"];
LastName = dr["LastName"];
Console.WriteLine(CustomerId,FirstName,LastName); //Print out the fields
}
dr.Close(); //Close the DataReader and release the Connection
Page 3
ADO.NET with VB.NET and C# Steven Nicolaou
VB.NET
‘Now we need to make a Command that we’ll use to send the query
‘The OleDBCommand Constructor takes 2 parameters in this case: The SQL Text, and the Connection
Object
Dim sqlText as String = “DELETE * FROM EMPLOYEES WHERE EMPLOYEE_ID = 5”
Dim RowsAffected as Integer
Dim cmd As New OleDbCommand(sqlText, m_Conn)
RowsAffected = cmd.ExecuteNonQuery()
m_Conn.Close() ‘Don’t forget to close the Connection immediately afterwards
C#
private String sqlText = “DELETE * FROM EMPLOYEES WHERE EMPLOYEE_ID = 5”;
private OleDbCommand cmd = new OleDbCommand(sqlText, m_Conn);
int RowsAffected = cmd.ExecuteNonQuery();
m_Conn.Close(); //Don’t forget to close the Connection immediately afterwards
VB.NET
Dim retItem As Integer
Dim cmd As New OleDbCommand(sqlText, m_Conn)
retItem = cmd.ExecuteScalar()
m_Conn.Close()
C#
int retItem;
private OleDbCommand cmd = new OleDbCommand(sqlText, m_Conn);
retItem = cmd.ExecuteScalar();
m_Conn.Close();
More Information
For more information on ADO.NET and examples using SQL Server visit the MSDN
http://msdn.microsoft.com/libr ary/default.asp?url=/library/en-us/Dndotnet/html/Usingadonet.asp
Page 4
ADO.NET with VB.NET and C# Steven Nicolaou
The DataTableCollection
An ADO.NET DataSet contains a collection of zero or more tables represented by DataTable objects. The
DataTableCollection contains all the DataTable objects in a DataSet .
The DataRelationCollection
A DataSet contains relationships in its DataRelationCollection object. A relationship, represented by the
DataRelation object, associates rows in one DataTable with rows in another DataTable. It is analogous to a join
path that might exist between primary and foreign key columns in a relational database. A DataRelation identifies
matching columns in two tables of a DataSet.
Relationships enable navigation from one table to another within a DataSet. The essential elements of a
DataRelation are the name of the relationship, the name of the tables being related, and the related columns in
each table. Relationships can be built with more than one column per table by specifying an array of DataColumn
objects as the key columns. When a relationship is added to the DataRelationCollection, it may optionally add a
UniqueKeyConstraint and a ForeignKeyConstraint to enforce integrity constraints when changes are made to
related column values.
Page 5