At the time of this writing, Microsoft had just released a third data provider for .NETthe ODBC .NET Data Provider - Release Candidate Beta. You can download this data provider from the Microsoft site at http://www.microsoft .com/data/download_odbcnetrc.htm.
Figure 8 shows the various paths an application can take to connect to a database through ADO.NET. When you're choosing a path, the first factor to consider is which .NET data provider you should use. If you use SQL Server 7.0 or later, using the SQL Server .NET data provider makes sense. If the database is SQL Server 6.5 or any nonSQL Server database that comes with an OLE DB provider (e.g., Oracle), you'd use the OLE DB .NET data provider. Note that you can still use the OLE DB .NET data provider even if you're using SQL Server 7.0 and later, but you'll lose the performance benefit of communicating directly with SQL Server through TDS. However, the benefit of this nonspecific path is portabilityyou can interchange the databases you use without modifying code.
Next, determine the tasks you need to perform. If you simply need to read and display the data from a data source, the DataReader object is probably sufficient. But if you need to manipulate the data (perhaps you need to perform some editing and deletion), use the DataSet object. Use DataSet only when you need to because DataSet is inherently slower than DataReader. (DataSet uses DataReader to populate its table.)
An Example in ADO.NET
Let's look at ADO.NET in action in a Web service. Listing 2 shows a Web service that returns a DataSet object. The code in Listing 2 is similar to the code in Listing 1. The Web service in Listing 2 retrieves the Authors table from the Pubs database and exposes it as a Web service. The Web service uses the SQL Server .NET data provider, as the following namespace import line shows:
Imports System.Data.SqlClient
First, the Web service establishes a connection with the SQL Server 2000 database:
Dim conn AS New SqlConnection("server=localhost;
uid=sa; password=; database=pubs")
Then, the Web service uses a Command object to execute a query against the database:
Dim comm AS New SqlCommand(sql, conn)
Next, the Web service uses a DataAdapter object to fill a DataSet:
DataAdapter.Fill(ds, "Authors_table")
Note that the connection closes as soon as the DataSet is filled, unlike connections in ADO, which must be open while you loop through a Recordset. The resulting DataSet returns as a Web service. Figure 9 shows a section of the DataSet that you get when you execute the new Web service. The DataSet, with its schema, is represented in XML format. A client application might choose to bind this DataSet by using a DataGrid. Listing 3 shows the code to perform this bind. Figure 10 shows the resulting DataSet bound to a DataGrid control in an ASP.NET Web application, which displays the DataSet in a more user-friendly format. ASP.NET provides many server controls that bind to DataSets automatically.
Using ADO in .NET Applications
Although ADO.NET has many powerful new features, you might want to continue to use ADO for any number of reasons. (For more information about deciding whether to convert to ADO.NET or use ADO and ADO.NET together, see Dino Esposito, "A Soft Landing to ADO.NET," page 37.) If you're developing new .NET applications, ADO.NET is the way to go. But if you're in the process of migrating your applications, you might want to retain ADO in existing projects and use ADO.NET to start new projects. The .NET Framework lets you use ADO in .NET applications through COM interop, which provides backward compatibility without requiring you to modify ADO. You need to import the ADO type library as an assembly, as Figure 11 shows. Then, you can use ADO as the sample code in Listing 4 shows.
Make an Informed Choice
Data-access technology is continually evolving. Before you've even mastered one technology, another comes along. Only one thing is certain: Databases play an increasingly important role in application development. Understanding the latest technologyand the evolutionary changes that created itwill help you choose the right technology for your current jobs and help you make informed choices as your organization's needs change.
End of Article
Prev. page
1
2
[3]
next page -->