An introduction to ADO.NET - Two ways to create ADO.NET objects
(Page 2 of 4 )
Figure 2-2 shows two basic techniques you can use to create the ADO.NET objects you need as you develop database applications. First, you can use the components in the Data tab of the Toolbox to create ADO.NET objects by dragging and dropping them onto a form. Notice that the names of most of the components in the Data tab are prefixed with either “OleDb” or “Sql.” As you’ll learn in the next figure, these prefixes identify the data provider that these components are associated with.
Before I go on, you should realize that when you drag one of the data adapter components onto a form, Visual Studio starts the Data Adapter Configuration Wizard. This wizard gathers information about the data you want to retrieve and then generates code to create the required ADO.NET objects. You’ll learn how to use the Data Adapter Configuration Wizard in the next chapter.
The Visual Basic project shown in this figure contains four ADO.NET objects: two data adapters named daVendors and daStates, a connection named conPayables, and a dataset named DsPayables1. Because these objects don’t have a visual interface like the controls that you add to a form, they don’t appear on the form itself. Instead, they appear in the Component Designer tray below the form. Then, when you select one of these objects, its properties appear in the Properties window and you can work with them from there.
The second technique for creating ADO.NET objects is to write the code yourself. The code shown in this figure, for example, creates three objects: a connection named conPayables, a data adapter named daVendors, and a dataset named dsPayables. It also uses the Fill method of the data adapter to retrieve data from the database identified by the connection and load it into the dataset. (Don’t worry if you don’t understand all of this code. You’ll learn more about coding these types of statements throughout this book.)
Although creating ADO.NET objects through code is more time-consuming than using the components and wizards, it can result in more compact and efficient code. In addition, because the components and wizards have limitations, there are times when you’ll need to write your own code. You’ll learn more about how you do that in chapter 6.
For now, you should realize that whether you create ADO.NET objects using the components in the Toolbox or using code, you need to be familiar with object-oriented programming techniques such as constructors and overloaded methods. For example, when you use the Fill method of a data adapter to retrieve data from a database and store it in a dataset, you’ll need to know which of the eight overloaded methods to use. And when you create ADO.NET objects like connections and data adapters through code, you’ll need to know which of the overloaded constructors to use. If you’re not familiar with these basic object-oriented programming techniques, we recommend that you review chapters 6 and 15 of our book, Murach’s Beginning Visual Basic .NET.
ADO.NET objects created using components in the Toolbox

Figure 2-2. Two ways to create ADO.NET objects
ADO.NET objects created using code
Dim sConnectionString As String = "data source=DOUG\VSdotNET;"& _
"initial catalog=Payables;integrated security=SSPI;"& _
"persist security info=False;workstation id=DOUG;packet size=4096"
Dim conPayables As New SqlConnection(sConnectionString)
Dim sVendorSelect = "Select * From Vendors"
Dim daVendors As New SqlDataAdapter(sVendorSelect, conPayables)
Dim dsPayables As New DataSet()
daVendors.Fill(dsPayables, "Vendors")
Description - You can use the ADO.NET components in the Data tab of the Toolbox to add ADO.NET objects to a form. Then, you can set the properties of the objects using the Properties window.
- If you add a data adapter from the Toolbox, the Data Adapter Configuration Wizard is started. This wizard helps you create the data adapter and the related connection and command objects. See chapter 3 for details.
- To create ADO.NET objects in code, you use Dim statements that identify the class that each object is created from. This method requires more coding than using the components but is more flexible.
ADO.NET data providers
To access a database, you use an ADO.NET data provider. In the topics that follow, you’ll learn more about the classes that make up a data provider. First, you’ll learn about the two data providers that come with the .NET Framework. Then, you’ll learn what each of the core data provider classes does.
Next: The SQL Server and OLE DB data providers >>
More .NET Articles
More By Murach Publishing
|
This article is excerpted from chapter two of the book VB.NET Database: Database Programming with ADO.NET, written by Anne Prince and Doug Lowe (Murach Publishing; ISBN: 1890774197). Check it out today at your favorite bookstore. Buy this book now.
|
|