ADO.NET brings along with it four, much-publicised advantages:
Interoperability - All data in ADO.NET is transported in XML format, meaning it's simply a structured text document that can be read by anyone on any platform.
Scalability - The client/server model is out. ADO.NET promotes the use of disconnected datasets, with automatic connection pooling bundled as part of the package.
Productivity - You can't just pick it up and run, but ADO.NET can certainly improve your overall development time. For example, "Typed DataSets" help you work quicker and produce more bug-free code.
Performance - Because ADO.NET is mainly about disconnected datasets, the database server is no longer a bottleneck and hence applications should incur a performance boost.
In ADO.NET, functionality is split into two key class groups — content components and managed-provider components.
The content components essentially hold actual data and include the DataSet, DataTable, DataView, DataRow, DataColumn and DataRelation classes.
We also have the managed-provider components, which actually talk to the database to assist in data retrievals and updates. Such objects include the connection, command and data reader.
Also, the managed-provider components are split into two key groups — one designed for regular data sources, with another finely tuned specifically for SQL Server.
ADO.NET bundles with a bunch of content components. The most important are:
DataSet — This is a lot like the old Recordset object, except that it can hold multiple "tables" of data. You can also setup internal data constraints and relationships.
DataView — The DataView is similar to a regular database view. You can essentially use this object to filter tables inside the DataSet object.
Currently, there are two key sets of managed provider components — one designed for general data access (in System.Data.OleDb) and one fine-tuned for SQL Server (in System.Data.SqlClient).
Both of these comply with the standard data implementations defined in the System.Data.Common namespace.
So, what are the key managed-provider components?
Connection — OleDbConnection + SQLConnection — Like classic ADO, this object implements properties such as the connection string and state. We also have the typical .Open and .Close, plus .BeginTransaction returning an object to control a database transaction. Note that you no longer have a .Execute method on the Connection object.
Command — OleDbCommand + SqlCommand — This is the pipeline to the backend data. You can use the command to either .ExecuteNonQuery, which will action an SQL statement (such as a DELETE command) upon the data — or .ExecuteReader, which links straight in to the Data Reader object.
Data Reader — OleDbDataReader + SqlDataReader — This object essentially takes a stream of data from the Command object and allows you to read it. It's like a forward-only Recordset and is very efficient. However this uses a server-side cursor, so you should avoid it too much as it naturally requires an open connection.
Data Adapter — OleDbDataAdapter + SqlDataAdapter — The Data Adapter object provides an all-in-one stop for your data. It essentially serves as a middle man, going through your connection to retrieve data, then passing that into a DataSet. You can then pass the DataSet back to the Data Adapter, which will go and update the database. The SQL statements for each command are specified in the InsertCommand, UpdateCommand, InsertCommand and DeleteCommand properties.
As more and more companies move toward XML as the standard for cross-platform communication, Microsoft is making an active effort to use this structure to power many of its new technologies.
One application of this is in ADO.NET — where literally all data is intrinsically stored in such a format.
And you can easily tap into this store by getting your information into a DataSet — then accessing the.ReadXml, .WriteXml and .GetXml functions.
In addition, you can parse XML programmatically using the System.Xml.XmlDocument object.
Sample
access using Data Reader:
' Setup
connection
Dim myConnection As New OleDb.OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Program
Files\Microsoft Visual " & _
"Studio.NET\Common7\Tools\Bin\nwind.Mdb")
' Setup
command object, specifying SELECT
' and
connection to use
Dim
myCommand As New
OleDb.OleDbCommand( _
"Select * from Customers", myConnection)
"Select * from Customers", myConnection)
Dim
myReader As OleDb.OleDbDataReader
' Open
connection
myConnection.Open()
' Execute and
put results into reader
myReader = myCommand.ExecuteReader
' Read
through all the records
Do Until myReader.Read = False
MessageBox.Show(myReader("CompanyName"))
Loop
' Close
reader connection before continuing
myReader.Close()
myConnection.Close()
Sample
access using Data Adapter:
' Setup
connection
Dim
myConnection As New
OleDb.OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;"
& _
"Data Source=C:\Program
Files\Microsoft Visual " & _
"Studio.NET\Common7\Tools\Bin\nwind.Mdb")
' We're just
specifying the SELECT here.
' If done
visually using the Server Explorer,
' the
INSERT/UPDATE/DELETE + Parameters
'
collection would be auto-specified for us
Dim
myDataAdapter As New
OleDb.OleDbDataAdapter( _
"Select * from Customers", myConnection)
"Select * from Customers", myConnection)
Dim
myDataSet As New
DataSet()
' Fill
DataSet with table, call it 'Customers'
myDataAdapter.Fill(myDataSet,
"Customers")
' Display
first CompanyName field of first
' row in
Customers table
MessageBox.Show(myDataSet.Tables("Customers").Rows(0)("CompanyName"))
' If we had
specified INSERT/UPDATE/DELETE
' commands +
Parameters collection, we could
' also now
edit the data, then run something like:
' myDataAdapter.Update(myDataSet)
Sample
access using XML:
Dim myDataSet As New DataSet()
myDataSet.ReadXml("c:\books.xml")
' Books.xml
is file bundled with VS.NET,
' typically
located at:
' C:\Program
Files\Microsoft.NET\FrameworkSDK
' ...
\Samples\quickstart\howto\samples\xml
' ...
\xmldocumentevent\vb\books.xml
' Counts the
number of book nodes
MessageBox.Show(myDataSet.Tables("book").Rows.Count)
' Retrieves
the last name of the first author
MessageBox.Show(myDataSet.Tables( _
"author").Rows(0)("last-name"))
"author").Rows(0)("last-name"))
' Updates the
last name
myDataSet.Tables("author").Rows(0)( _
"last-name") = "Flandadenham"
"last-name") = "Flandadenham"
' Rewrites
the XML file
myDataSet.WriteXml("c:\books.xml")
No comments:
Post a Comment