Data Access in Visual Basic

Data Access in Visual Basic

Data access is made up of three components: code that manipulates data objects, data binding, and design-time tools such as the ADO data environment. The users of your applications may regard the run-time behavior as another component of data access; but we will consider the run-time behavior as an element of each of the three components. Let s look quickly at the three components and see where they differ between Visual Basic 6 and Visual Basic .NET.

Code

DAO, RDO, and ADO are implemented as COM libraries, so most code works exactly the same in Visual Basic .NET as it did in Visual Basic 6. For example, the following ADO code opens the Northwind database and then executes a Select statement that returns the list of employees. The first name and last name of the employee is then shown in a message box:

Dim cn As New Connection Dim rs As Recordset cn.Open "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=c:\temp\nwind.mdb" Set rs = cn.Execute("Select * From Employees") MsgBox rs!FirstName & " " & rs!LastName rs.Close cn.Close

This code upgrades to the following:

Dim cn As ADODB.Connection = New ADODB.Connection() Dim rs As ADODB.Recordset cn.Open("Provider=Microsoft.Jet.OLEDB.3.51;Data Source=c:\temp\nwind.mdb") rs = cn.Execute("Select * From Employees") MsgBox(rs.Fields("FirstName").Value & " " & rs.Fields("LastName").Value) rs.Close() cn.Close()

As we ve seen in other examples elsewhere in this book, after it is upgraded the code looks essentially the same, just more explicit. It works perfectly in Visual Basic .NET. Likewise, most RDO and DAO code works perfectly after upgrading, with a few differences that we will discuss later in this chapter.

One major difference across all the data access technologies is how you access fields of a Recordset (or Resultset for RDO). In Visual Basic 6, it is common to access fields using the shorthand coding convention RecordsetName!FieldName. In Visual Basic .NET, this code needs to be expanded in order to resolve the default properties. The Upgrade Wizard does this for you, but the code looks a little different after upgrading. Let s look at an example:

Dim rs As Recordset Dim myString As String myString = rs!Lastname

This code assigns the string myString to the field Lastname of the Recordset rs. It upgrades to the following:

Dim rs As ADODB.Recordset Dim myString As String myString = rs.Fields("Lastname").Value

Notice that rs!Lastname was expanded to rs.Fields( Lastname ).Value. The Upgrade Wizard will handle all of these cases for you.

Data Binding

Data binding allows you to bind the value of a control on a form to a field in a database. Visual Basic 6 supports data binding using ADO, DAO, and RDO. You can bind a control to an ADO data environment, an ADO Data control, a DAO Data control, or an RDO Data control. Visual Basic .NET, however, supports ADO data binding only, not DAO or RDO data binding. This distinction comes from the way in which the different types of data binding are implemented.

DAO and RDO are both older data access technologies. DAO data binding was introduced in Visual Basic 3, and RDO data binding debuted in Visual Basic 4. When the Visual Basic development team first implemented these older forms of data binding, they built them into the forms package. This implementation allowed a seamless integration, but it also tied the data binding technology to Visual Basic Forms. In Visual Basic .NET, Visual Basic Forms has been replaced with Windows Forms a redesigned forms package. The designers of Windows Forms decided not to build DAO and RDO data binding into Windows Forms, and therefore DAO and RDO data binding are not supported. ADO data binding is supported because it is not built into the forms package; instead it is implemented in the COM library MSBind.dll. This library manages ADO data binding in Visual Basic 6, and the updated library, called Microsoft.VisualBasic.Compatibility.Data, manages data binding in Visual Basic .NET. The Upgrade Wizard upgrades data binding to both the ADO Data control and the ADO data environment.

ADO Data Environment

The Visual Basic 6 ADO data environment lets you visually design database connections and database commands using the ADO Data Environment Designer. Although Visual Basic .NET does not support the ADO data environment, the Upgrade Wizard upgrades the connections, commands, and Recordsets to a class that has the same run-time behavior as the Visual Basic 6 data environment.

Components That Don t Upgrade

The components that can t automatically be upgraded to Visual Basic .NET cause errors in the upgraded project. For example, if a Visual Basic 6 form has a DAO Data control, the control is removed during upgrade and EWIs are inserted into the upgrade report. Any code that references the control will cause a compile error.

What should you do if your project uses RDO or DAO data binding? The only solution is to reimplement the data binding in Visual Basic .NET, using ADO or ADO.NET data binding. The best choice is generally ADO.NET. As we discuss in the next section, the design-time experience for ADO.NET is better than for ADO.

Figure 14-1 gives an overview of the different data access technologies in Visual Basic 6 and shows how they upgrade to Visual Basic .NET. Technologies on the left side without an arrow do not automatically upgrade to Visual Basic .NET.

Figure 14-1

How data access technologies upgrade.



Upgrading Microsoft Visual Basic 6.0to Microsoft Visual Basic  .NET
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET w/accompanying CD-ROM
ISBN: 073561587X
EAN: 2147483647
Year: 2001
Pages: 179

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net