Using ADO.NET Classes


ASP.NET Developer's JumpStart
By Paul D. Sheriff, Ken Getz
Table of Contents
Chapter 10.  Introduction to ADO.NET

ADO.NET provides several classes you will need to become familiar with. Table 10.1 provides a short description of each of the classes you will encounter over the next several chapters.

Table 10.1. ADO.NET Classes
Class Description
DataSet DataSet objects can contain one or more DataTable objects, much like an in-memory database. You have the ability to set relationships between these DataTable objects. You can modify the data in a DataSet, and DataSet objects can hold both the original set of data and any modifications made to this data. You can then use other ADO.NET objects to submit these changes back to the data source. This class does not provide any means to communicate directly with any type of data source that functionality is provided by the DataAdapter objects.
DataTable DataTable objects hold rows of data. Each row is made up of columns, and each column can hold some discreet data. The DataTable object provides a collection of DataRow objects and a collection of DataColumn objects. Each DataRow object contains the data stored within a single row in the DataTable, and DataColumn objects provide information about the schema of the DataTable. Like the DataSet object, DataTable objects have no means to communicate with any type of data source. You can add data to the DataTable object programmatically, one row at a time, or you can use the Fill method of a DataAdapter object to fill a DataTable with data from a data source.
DataView Instances of this class provide a specific view of a DataTable. This object is frequently used to set sort orders or filters on a DataTable object.
Connection[*] Use a Connection object to create a connection to a data source. You can open a connection to a data source and keep this connection object open for as long as you need it.
Command[*] Use a Command object to submit SQL statements to a back-end data source. You can use SQL or calls to stored procedures for all data retrieval and modification. You can use the ExecuteReader method to return a DataReader object, ExecuteNonQuery to run an update/delete/insert query, or ExecuteScalar to return a single value as the result of the SQL you've provided.
DataAdapter[*] The DataAdapter object provides four Command object properties: one each for a Select, Update, Insert, and Delete command. You'll use a DataAdapter object's Fill method (and its SelectCommand property) to populate a DataSet or a DataTable object with data from a database. You can use the Update method of the DataAdapter to save changes you've made to a DataSet back to the original data source(s), and the DataAdapter will determine, based on the changes you've made, which of its commands (Update, Insert, or Delete) to use.
CommandBuilder[*] When you work with a DataAdapter object, you'll need to provide Insert, Update, and Delete commands, in addition to a Select command, if you want to modify data. Although you can supply the SQL expressions yourself, it would be easier if someone else could do the work. That's the job of the CommandBuilder object, which can generate Insert, Update, and Delete commands based on the Select command you supply. A DataAdapter can then use these commands to update the original data source(s). (You should note that the CommandBuilder does build commands for you, but the SQL it creates won't be optimized and will almost always include more fields than you might need.)
DataReader[*] The DataReader class provides a forward-only, read-only cursor (sometimes called a fire hose cursor) that reads data from a data source in the most efficient manner available to ADO.NET. This class is excellent for filling DataGrids, list boxes, and combo boxes. Under the covers, the DataAdapter object uses a DataReader to fill the contents of a DataSet.

[*] We're referring to these objects by generic names, but their actual names include information about the namespace that provides them. For example, the .NET Framework provides SqlConnection and OleDbConnection objects, and we're referring to them both, generically, as the Connection class here. The same goes for Command, CommandBuilder, and so on.

Figure 10.1 shows you how some of the different classes in .NET work together to retrieve data, create DataTables within a DataSet, and how those DataSets can be consumed. Datasets can be created by DataAdapters, which read from a data store such as SQL Server, Oracle, or Access. Datasets can also be created by code you write in VB .NET or C#. Once the DataSet is built, it may be consumed by a WinForm, a WebForm, or a Web Service, or it can be sent across an HTTP interface to some other consumer, such as a Web page written in Java running on a Unix server.

Figure 10.1. An overview of the .NET classes.



    ASP. NET Developer's JumpStart
    ASP.NET Developers JumpStart
    ISBN: 0672323575
    EAN: 2147483647
    Year: 2002
    Pages: 234 © 2008-2017.
    If you may any questions please contact us: