![]() |
|
|
Published: Friday, September 07, 2001 Chapter 6: Data Manipulation with ADO.NET
by Steve Walther In this chapter you will learn the following:
atabase access is a crucial component of almost any ASP.NET application. Fortunately, the .NET framework contains a rich set of classes and controls for working with database data in your ASP.NET pages. We'll approach the subject of database access by dividing the topic into two chapters. In this chapter, you'll be given an overview of ADO.NET, the data access technology built into the .NET framework. You'll learn how to use the ADO.NET classes to perform standard database tasks such as modifying and accessing database data. We'll also cover some of the more advanced features of ADO.NET, such as stored procedure support and filtering and sorting data. In the next chapter, "Data Presentation," you'll learn how to use ADO.NET with ASP.NET controls. You'll learn how to use ADO.NET to bind database data to standard Web Controls, such as the DropDownList and RadioButtonList controls. You'll also learn how to display and edit database data using the more specialized data controls such as the Repeater, DataList, and DataGrid controls. 1. Overview of ADO.NETLet's begin with a quick tour of ADO.NET. The .NET Framework contains several namespaces with dozens of classes devoted to database access. However, for the purposes of explaining ADO.NET in this chapter, I'll make a rough division of these classes into three groups. The first group consists of the following three classes:
If you plan to build your ASP.NET application with Microsoft SQL Server (version 7.0 or greater), these are the classes you'll use most often. These classes enable you to execute SQL statements and quickly retrieve data from a database query. The SqlConnection class represents an open connection to a Microsoft SQL Server database. The SqlCommand class represents a SQL statement or stored procedure. Finally, the SqlDataReader class represents the results from a database query. We'll go into the details of using each of these classes in the next section of this chapter.
These classes work only with Microsoft SQL Server. If you need to work with another type of database, such as an Access or Oracle database, you will need to use the following classes:
Notice that these classes have the same names as the ones in the previous group, except these classes start with OleDb rather than Sql. Why did Microsoft duplicate these classes, creating one version specifically for SQL Server and one version for nonSQL Server databases? By creating two sets of classes, Microsoft was able to optimize the performance of the first set of classes specifically for SQL Server. The OleDb classes use OLEDB providers to connect to a database. The Sql classes, on the other hand, communicate with Microsoft SQL Server directly on the level of the Tabular Data Stream (TDS) protocol. TDS is the low-level proprietary protocol used by SQL Server to handle client and server communication. By bypassing OLEDB and ODBC and working directly with TDS, you get dramatic performance benefits. There's one last group of classes that we'll work with in this chapter. This group contains the following classes:
You can use the classes in this group to build a memory-resident representation of a database (an in-memory database). A DataSet represents the in-memory database itself. Once you create a DataSet, you can populate it with one or more DataTables that represent database tables. You create the DataTables with the help of either the SqlDataAdapter or OleDbDataAdapter class. You can then define various relationships between the tables with the DataRelation class and create filtered or sorted views on the DataTables with DataViews. Why would you want to build an in-memory database? In certain situations, it is useful to have all the data from a database table available to your ASP.NET application in such a way that it is disconnected from the underlying database. For example, using the classes from this group, you can cache one or more database tables in your server's memory and use the same data in multiple ASP.NET pages. You'll see some other applications of these classes in the final section of this chapter.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||