Overview
With the release of the .NET Framework SDK Beta 2, Microsoft has made some fundamental changes. One area that has been changed is ADO.NET. This article illustrates these changes so you can move your ADO code from Beta 1 to Beta 2 as simply as possible. All examples in this article are in C#.
Namespace
The first and most basic change that Microsoft has made is the namespace that you import. The ADO namespace has now been replaced by the OleDb namespace. All code that uses the ADO namespace will no longer run and will return a compile error because the namespace no longer exists in .NET Framework.
In Beta 1 you would use the following:
using System.Data.ADO;
In Beta 2 you now use the following:
using System.Data.OleDb;
Class Names
Coupled with the namespace change, the class names have changes as well. The following table lists some of the Beta 1 class names and the new Beta 2 class names:
Beta 1 | Beta 2 |
ADOCommand | OleDbCommand |
ADOConnection | OleDbConnection |
ADODataReader | OleDbDataReader |
ADODataSetCommand | OleDbDataAdapter |
ADOParameters | OleDbParameterCollection |
ADOParameter | OleDbParameter |
Connection Changes
The basics of the Connection object remain unchanged. Connecting to a database is exactly the same in both versions of ADO.NET. Figure 1 shows how to connect to an Access database using a Connection object. I thought it would be useful to show this. When I was learning ADO.NET, I found it extremely difficult to find such an example.
OleDbConnection cnDB = new OleDbConnection();
cnDB.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\MyDB.mdb;Persist Security Info=False";
cnDB.Open();
|
Figure 1
The major change to the Connection class is in using transactions.
In Beta 1 you begin a transaction using the BeginTransaction method. All operations on the transaction are then handled via methods of the Connection class. Figure 2 shows the opening and then committing of a transaction. This transaction applies to all objects that use that connection.
'begin the transaction
cnDB.BeginTransaction();
'some other code
...
'commit the transaction
cnDB.CommitTransaction();
|
Figure 2
In Beta 2, the BeginTransaction method passes back an OleDbTransaction object. All operations on the transaction are then handled via the transaction object. Another major difference in Beta 2 is the ability to assign a specific transaction to individual Command objects and handle them separately.
Figure 3 shows the creation of a new transaction. This transaction is then assigned to a Command object. The transaction is then committed. Any other Command objects that are not associated with the transaction object are unaffected (unlike Beta 1 in which all Command objects are affected).
'create a new transaction
OleDbTran tran = cnDB.BeginTransaction();
'assign transaction to a new command
cmRunProc.Transaction = tran;
'some other code
...
'commit the transaction
tran.Commit();
|
Figure 3
The major difference between the two betas is in setting the isolation level of the transaction. In Beta 1, you set the IsolationLevel property. In Beta 2, passing an IsolationLevel parameter to the BeginTransaction method sets the isolation level.
Command Changes
The OleDbCommand class is the class whose changes will have the most impact on your code.
In Beta 2, the ActiveConnection property of the command class has been replaced by the Connection property. The Execute method has also been replaced by the ExecuteReader method. Figure 4 shows the Beta 1 method of creating a new Command object and associating the object with a query within the database. The Command object is then executed and the data is returned into a DataReader object.
'create a new command object
ADOCommand cmRunProc = new ADOCommand();
cmRunProc.ActiveConnection = cnDB;
cmRunProc.CommandType = CommandType.Text;
cmRunProc.CommandText = "qryMySelect";
'execute the query
DataReader drResults = new DataReader();
cmRunProc.Execute(out drResults);
|
Figure 4
Figure 5 shows the same task in Beta 2. The fundamental change is that you no longer have to pass an empty DataReader object into the Command object to be populated. The Command object now creates and returns the populated DataReader object.
'create a new command object
ADOCommand cmRunProc = new ADOCommand();
cmRunProc.Connection = cnDB;
cmRunProc.CommandType = CommandType.Text;
cmRunProc.CommandText = "qryMySelect";
'execute the query
DataReader drResults = cmRunProc.ExecuteReader();
|
Figure 5
When invoking the ExecuteReader method, you can now provide a CommandBehavior parameter. A very useful one for ASP.NET is CommandBehavior.CloseConnection, which automatically closes the current connection when the associated DataReader object is closed.
The Beta 2 Command class also no longer has the RecordsAffected property to return the number of records affected by the execution of the Command.
Conclusion
As demonstrated, Microsoft has made some fundamental changes to ADO.NET. Many of these changes will have a positive impact on the way you access databases, and provide increased flexibility and functionality. The examples covered in this article are not the only changes, but are the most obvious and most likely to impact your code. Further changes can be determined by viewing the SDK documentation from each beta.
Reference
.NET Framework SDK Beta can be downloaded at
https://msdn.microsoft.com/downloads/default.asp?url=/downloads/sample.asp?url=/msdn-files/027/000/976/msdncompositedoc.xml&frame;=true
About the Author
James Culshaw, BSc(Hons), MCP, has MCPs in Visual Basic (VB) and SQL Server database design. He has a Bachelor of Science in Computer Science, with honors, from the University of Glasgow, Scotland. He has five years of experience in VB (3-6), Oracle, SQL Server, ASP, and is currently learning C#. He has his own company, Active Data Solutions (https://www.active-data-solutions.co.uk). He can be reached at jculshaw@active-data-solutions.co.uk.
|