Saturday, October 21, 2006

What's is new in ADO .NET?

You can do a lot with Visual Basic by itself, but ultimately, you have to have data to build most systems. VB .NET is designed to work with a new way to access data: ADO .NET.

The simplest definition of ADO .NET is that it is Microsoft's new object library for data access. In one way, this is just the most recent of Microsoft's access methods (previous products were DAO, RDO and ADO). But in another way, this is a new revolution in software like the other .NET products.

The ADO .NET namespace is in System.Data.dll. The default Windows Application project already adds a reference to that DLL, but it's worthwhile to make sure that it's there on your computer.

The objects that are in ADO .NET are roughly split into two groups — content components and managed-provider components. The content components essentially hold actual data and include these classes:

DataSet
DataTable
DataView
DataRow
DataColumn
DataRelation

The managed-provider components interact with the database for data retrievals and updates.

These components include:

connection
command
data adapter
data reader

The names above are "generic" (note that they're not capitalized) because unique managed-provider components are used with different "providers" (software interfaces for actual databases). There are two that are currently included with ADO .NET. SqlClient is used with Microsoft's SQL Server database and OleDb for everything else. For example, SqlClient contains SqlDataReader and OleDb contains OleDbDataReader.

The main new feature in ADO .NET is provided by the DataSet object. This object reads the database and creates an in-memory copy of that part of the database that your program needs. DataSet is a little (but only just a little) like the old Recordset object. Usually a DataSet object will correspond to a real database table or view (or perhaps several), but DataSet is a "disconnected" view of the database data. After ADO .NET creates the data set, there is no longer an active connection to the database. This helps a lot in scalability (when the amount of data that must be handled by the same program increases) because your program only has to connect with a database server when reading or writing from the database.

One of the biggest differences between DataSet and the old Recordset is that data in ADO.NET is transported in XML format. That makes it a structured text document that can be read by anyone on any platform. This makes DataSet "interoperable" between a lot of computers - Microsoft and non-Microsoft since XML is an international and vendor neutral standard. After you create an XML document using methods provided by DataSet, you can parse it in .NET programmatically using the System.Xml.XmlDocument object.

ADO .NET makes it extremely easy to create XML documents. This is the only code you need to create a XML document where the principal customer contact is the owner using the Northwind sample database from Microsoft. Don't worry about all the new and unfamiliar code here ... we'll consider the details of this type of program in future articles.

' Declare the ADO .NET objects
Dim myData As New DataSet()
Dim myXML As String
Dim mySelectQuery As String = _
"SELECT CompanyName FROM Suppliers"
Dim myConnection As New _
OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source={path}\Northwind.mdb")
Dim strSelect As String = _
"SELECT * FROM Customers WHERE ContactTitle = 'Owner'"
Dim dsCmd As _
New OleDbDataAdapter(strSelect, myConnection)
' Fill the DataSet object
dsCmd.Fill(myData, "CustomerOwners")
' Create a text XML document
myData.WriteXml("TestXML.txt")
myConnection.Close()

0 Comments:

Post a Comment

<< Home