Objects That Are Found in ADO.NET
As mentioned previously, the main object that is used with ADO.NET is the DataSet object. You can see the DataSet object and its properties, methods, and additional objects in Figure 3.1.
Figure 3.1 ADO.NET has several more objects than ADO does.
Take a look at Table 3.1 to see a brief description of some of the objects that you will be using during this How-To.
Table 3.1 ADO.NET Data Objects That Are Used to Manipulate Data
Object |
Purpose |
DataSet |
This object is used in conjunction with the other data controls, storing the results that are returned by commands and the data adapters. Unlike the recordset from ADO and DAO, the data set actually brings back a hierarchical view of the data. Using properties and collections in the DataSet object, you can get overall relations, individual tables, rows, and columns. |
DataTable |
One of the objects off of the data set, the DataTable object enables you to manipulate an individual table's worth of data. The data table is similar to the recordset object that is found in ADO. |
DataView |
Using this object, you can filter and sort your data, keeping various views of the data. Each data table has a default view, which is the starting data view that can be modified and stored in a separate data view. |
DataRow |
This object enables you to manipulate the rows of data in your data tables. This can be thought of as a cache of data that you can manipulate by adding, deleting, and modifying records. You can then accept the changes back to the recordset, where you will then run SQL statements to update data back at the server. |
DataColumn |
As the name suggests, you can get information at the column level by using the DataColumn object. You can get schema information as well as data using this object. For example, if you want to create a list box of names of fields, you could iterate through the DataColumn collection off a data row and retrieve all the names of the fields. |
PrimaryKey |
This object allows you to specify a primary key for a data table. That way, when you use the Find method of the data table, it knows which column to use. |
.NET also provides classes, called data providers, to work with ADO.NET objects to provide access to data. You can see some of those objects in Figure 3.2.
NOTE
Your Visual Studio .NET applications are made up of one or more assemblies. Each assembly contains one or more Namespaces. Namespaces are then made up of one or more classes (objects). Therefore, the Namespace for your OleDb objects is System.Data.OleDb. You can find these objects using the Object browser.
Figure 3.2 You can use either OleDb classes or SQLClient classes for better performance.
In Table 3.2, you can see a brief description of some of the objects that you will be using during this How-To.
Table 3.2 .NET Data Provider Classes That Are Used to Manipulate Data
Object |
Purpose |
Command |
Similar to the ADO Command object, this allows you to execute stored procedures in code. Unlike the ADO version, however, you can create a DataReader object using the ExecuteReader method. |
Connection |
This object opens a connection to the server and database with which you want to work. Unlike the ADO Connection object, the way that the connection remains open depends on the object with which you are working, such as a DataReader or DataSet object. |
DataAdapter |
A real workhorse, the DataAdapter object allows you to create SQL statements and fill datasets with the data. It also creates other necessary action queries, such as Insert, Update, and Delete ADO.NET command objects. |
DataReader |
This object creates a read-only, forward-only stream of data that allows you to quickly populate controls, such as ListBox and ComboBox controls. |
Parameter |
This object allows you to specify the parameter (or parameters if you use more than one) that DataAdapter objects can specify and use. |
TIP
Chapter 1, "Developing Windows Forms Using Bound Controls," mentioned that the OleDb data controls are the ones that you will want to use for various types of backends, whereas the SQLClient data controls work strictly with SQL Server. The same is true of using these objects as well. If you know that you will just be using a SQL Server backend, you will get better performance by using the SQLClient objects because a layer is cut out.
You will get a chance to see all of the items listed in the previous two tables throughout the following How-Tos.
TIP
If you have to stick with ADO or just want to be stubborn, check out using ADO with .NET by reading Appendix A, "Desktop Development with ADO."
Although ADO.NET does take more work sometimes to accomplish a task that you could do using ADO, the power and flexibility of ADO.NET is well worth the learning curve.
NOTE
Although this chapter was written using Windows Forms, the majority of the objects can also be used in Web Forms as well as by using ADO.NET with ASP.NET. You will see this in Chapter 5, "Working with Date in Web Forms."
You will learn various methods for achieving the same goal. When and how you use these methods will depend on the scenario.
All of the examples in this chapter can be found in the Solution called VB.NETChapter 3 on the Web site.