DekGenius.com
Team LiB
Previous Section Next Section

What about ADO?

As far as .NET is concerned, ADO is simply a COM component. That means that all access to ADO is done through .NET COM InterOp. ADO objects and ADO .NET are mutually exclusive. As a result, ADO and ADO .NET can exist together in the same application.

The best way to illustrate this is through an example. The easiest example to look at is an ASP application.

[ASP]

<%@LANGUAGE=VBSCRIPT%>
<!
This ASP example uses ADO to read records from the
Northwind database and print two fields from all returned records to an ASP page. Connection to the Northwind database is through an ODBC system data source. 
>
<html>
<body>
<%
dim ADOconn, ADORecSet, SelectOrderSTR

SelectOrderSTR = _
         "SELECT OrderID, CustomerID, " & _
         "EmployeeID, OrderDate, " & _
         "RequiredDate, ShippedDate, " & _
         "ShipVia, Freight, " & _
         "ShipName, ShipAddress, " & _
         "ShipCity, ShipRegion, " & _
         "ShipPostalCode, ShipCountry " & _
         "FROM Orders with (NOLOCK)"


   set ADOconn = Server.CreateObject("ADODB.Connection")
  ADOconn.Open "DSN=NorthWindODBC;UID=sa;PWD=sa;"


   set ADORecSet = ADOconn.execute(SelectOrderSTR) 
   ' Query didn't return any records.
   if ADORecSet.BOF and ADORecSet.EOF then 
      Response.Write("No Records.")
   else
      'Query didn't return any records..MoveFirst
      Do While Not ADORecSet.EOF
         Response.Write(ADORecSet ("CustomerID") & " : " _
            & ADORecSet ("OrderID") & "<br>")
         ADORecSet.MoveNext
      Loop
      Response.Write("<p>End of data.")   
   end if
   ADORecSet.close
   set ADORecSet = nothing
%>
</body>
</html>

The minimum required to convert the above to ASP .NET is shown below. The file can be created simply by changing the extension of the ASP file to aspx.

[ASP .NET]

<%@Page aspcompat=true Language = VB%>
<!
This ASP example uses ADO to read records from the
Northwind database and print two fields from all returned
records to an ASP page. 
Connection to the Northwind database is through an ODBC 
system data source. 
>
<html>
<body>
<%
dim ADOconn, ADORecSet, SelectOrderSTR

    SelectOrderSTR = _
         "SELECT OrderID, CustomerID, " & _
         "EmployeeID, OrderDate, " & _
         "RequiredDate, ShippedDate, " & _
         "ShipVia, Freight, " & _
         "ShipName, ShipAddress, " & _
         "ShipCity, ShipRegion, " & _
         "ShipPostalCode, ShipCountry " & _
         "FROM Orders with (NOLOCK)"

   
   'Set is removed
   ADOconn = Server.CreateObject("ADODB.Connection")
   'Parentheses added
   ADOconn.Open("DSN=NorthWindODBC;UID=sa;PWD=sa;")

   'Set is removed
   ADORecSet = ADOconn.execute(SelectOrderSTR) 
   ' Query didn't return any records.
   if ADORecSet.BOF and ADORecSet.EOF then 
      Response.Write("No Records.")
   else
      'Query didn't return any records..MoveFirst
      Do While Not ADORecSet.EOF
         ' Specify Value property.
         Response.Write(ADORecSet ("CustomerID").value 
           & " : " & ADORecSet ("OrderID").value & "<br>")
         ADORecSet.MoveNext
      Loop
      Response.Write("<p>End of data.")   
   end if
   ADORecSet.close
   'Set is removed
   ADORecSet = nothing
%>
</body>
</html>

As is clearly shown, the main changes are to make the application conform to the new VB .NET syntax. Also, notice that the page directives attribute, aspcompat, is set to True. This is required so that the compiler knows that the page will include ASP type syntax and directives such as <% %> blocks for server scripts. This is the absolute minimum required. Also notice that in the example, the <%@Page does not specify any code-behind file.

In the example, we are using late binding, but for performance reasons it is recommended that you use early binding. You can only use early binding when you use compiled code, and for ASP .NET, this means using code-behind files. Note that this is the default for ASP .NET.

To use early binding after you have created the aspx file, you need to add the ADO reference. From within your project, go to the Add Reference dialog box and choose the COM tab. Look for the latest version of Microsoft ActiveX Data Objects library. In our example, I will use version 2.7, so I choose Microsoft ActiveX Data Objects 2.7 Library. See Figure 8-1.

Click To expand
Figure 8-1: The Add Reference dialog

Once you click on Select and OK, Visual Studio .NET will add a reference to your project with a namespace called ADODB. See Figure 8-2.

Click To expand
Figure 8-2: ADODB namespace added to the project

The ADODB namespace will now contain all the objects and associated methods that ADO uses. The next step is to create a method in the class that we can use in the HTML section of the aspx file. We can still use the same example that we have used so far. We encapsulate this in a method called GetData.

Public Sub GetData()
        'Create new ADO connection
        Dim ADOconn As New ADODB.Connection()

        'Declare Record Set
        Dim ADORecSet As ADODB.Recordset

        'Declare string variable
        Dim SelectOrderSTR As String

        SelectOrderSTR = _
         "SELECT OrderID, CustomerID, " & _
         "EmployeeID, OrderDate, " & _
         "RequiredDate, ShippedDate, " & _
         "ShipVia, Freight, " & _
         "ShipName, ShipAddress, " & _
         "ShipCity, ShipRegion, " & _
         "ShipPostalCode, ShipCountry " & _
         "FROM Orders with (NOLOCK)"

        ADOconn.Open("DSN=NorthWindODBC;UID=sa;PWD=sa;")

        'Set is removed
        ADORecSet = ADOconn.Execute(SelectOrderSTR)
        ' Query didn't return any records.
        If ADORecSet.BOF And ADORecSet.EOF Then
            Response.Write("No Records.")
        Else
            'Query didn't return any records..MoveFirst
            Do While Not ADORecSet.EOF
              'Specify Value property.

              Response.Write(ADORecSet("CustomerID")
                  .Value & " : " & ADORecSet("OrderID")
                  .Value & "<br>")
                ADORecSet.MoveNext()
            Loop
            Response.Write("<p>End of data.")
        End If
        ADORecSet.Close()
        'Set is removed
        ADORecSet = Nothing
    End Sub

The main difference with our previous example is that we are using late binding and we no longer need to use Server.CreateObject. The IntelliSense features of Visual Studio can now help with the programming and provide compile-time checking and strongly typed data types. However, we are not done yet. The final step is to use the method in the web page. To do this, you need to add a line in the HTML view of the aspx file.

<%@ Page Language="vb" AutoEventWireup="false"
 Codebehind="Test2.aspx.vb"
 Inherits="WebApplication1.Test2"%>
<HTML>
<! This ASP example uses ADO to read records from the 
Northwind database and print two fields from all 
returned records to an ASPX page. Connects to Northwind
database through an ODBC system data source. >
<body>
<%
  GetData()
%>
</body>
</HTML>

As you have probably gathered, .NET offers very good support for migration. In the case of ASP .NET, ASP and ASP .NET pages can even coexist in the same application. This makes it easier to migrate an application in different stages. As for Visual Basic applications, as long as you remember the few syntax changes, the migration process is simple.

Team LiB
Previous Section Next Section