Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET Database Programming Weekend Crash Course - J. Butler, T. Caudill.pdf
Скачиваний:
31
Добавлен:
24.05.2014
Размер:
3.32 Mб
Скачать

302

Sunday Afternoon

Table 30-1

 

Continued

Feature

ADO

ADO.NET

Scalability

ADO typically incurs

ADO.NET supports disconnected

 

extensive database locks

access to database data, thus

 

that, when combined with

removing much of the competition

 

lengthy active database

for limited database resources and

 

connections, tend to

providing a much more scalable

 

compete for limited

solution

 

database resources and

 

 

thus limit scalability

 

 

 

 

Running ADO under ASP.NET

Here’s an example illustrating the various methods that can be used to run your existing ADO code in the .NET Framework. First let’s look at a typical ASP page that retrieves a list of authors from the Pubs database using ASP and ADO, shown in Listing 30-1.

Listing 30-1 A typical ASP page for retrieving data from a database

<%@ LANGUAGE = “VBSCRIPT” %>

<HTML>

<BODY>

<%

DIM oConn, oRS

Set oConn = Server.CreateObject(“ADODB.Connection”) Set oRS = Server.CreateObject(“ADODB.RecordSet”)

oConn.Open “provider=sqloledb;Data Source=(local);Initial Catalog=pubs;User ID=sa;pwd=”

Set oRS = oConn.execute(“SELECT * FROM Authors;”)

Response.Write(“<H1>ADO Running Under ASP</H1>”) Response.Write(“<H2>Using Late Binding</H2>”) if oRS.BOF and oRS.EOF then

Response.Write(“No Records”) else

oRS.MoveFirst

Do While Not oRS.EOF Response.Write(oRS(“au_fname”) _

& “ “ & oRS(“au_lname”) & “<br>”) oRS.MoveNext

Loop

Response.Write(“<p>End of RecordSet</p>”) end if

oRS.close

Set oRS = nothing

%>

</BODY>

</HTML>

Session 30—Migrating from ADO to ADO.NET

303

This code will not run as it is under ASP.NET, primarily due to syntax differences in the languages. To test this, change the file extension of the previous code from *.asp to *.aspx and see what error messages are displayed. In order to migrate this code to the .NET Framework using VB as the programming language, you would need to eliminate the Set statement from your code. Next, you need to fully qualify your object references, so in Listing 30-1 you must append a .value to each of your RecordSet value references. You must enclose your method parameters in parentheses, and you must set the page directive ASPCOMPAT=”True”. Listing 30-2 shows the required modifications (shown in bold) made to Listing 30-1 to make the ASP code using ADO operational under ASP.NET.

Listing 30-2 A migrated ASP page that runs under ASP.NET using unmanaged ADO code

<%@ LANGUAGE = “VB” ASPCOMPAT=”True”%> <HTML>

<BODY>

<%

DIM oConn, oRS

‘We removed Set statement

oConn = Server.CreateObject(“ADODB.Connection”)

‘We removed Set statement

oRS = Server.CreateObject(“ADODB.RecordSet”)

‘We Added Parentheses

oConn.Open(“provider=sqloledb;Data Source=(local);Initial Catalog=pubs;User ID=sa;pwd=”)

‘We Removed Set statement

oRS = oConn.execute(“SELECT * FROM Authors;”) Response.Write(“<H1>ADO Running Under ASP.NET</H1>”) Response.Write(“<H2>Using Late Binding</H2>”)

if oRS.BOF and oRS.EOF then Response.Write(“No Records”)

else oRS.MoveFirst

Do While Not oRS.EOF

‘ Added .Value Response.Write(oRS(“au_fname”).Value _

& “ “ & oRS(“au_lname”).Value & “<br>”) oRS.MoveNext

Loop

Response.Write(“<p>End of RecordSet</p>”) end if

oRS.close

‘Removed the Set statement oRS = nothing %>

</BODY>

</HTML>

This approach enables you to migrate much of your existing ADO code to ASP.NET by simply handling the syntax differences between VBScript and VB .NET.

However, this approach requires that you utilize late binding. When handling COM objects under the .NET Framework, early binding is the preferred method. Early binding allows your application to bind directly to the address of the function being called and thus avoids the extra overhead in doing a runtime lookup. This generally provides a twofold performance increase over late binding in terms of execution speed. Additionally early binding provides

304

Sunday Afternoon

you with type safety. Early binding also provides compile time warnings if the data type of a parameter or return value is incorrect, saving a lot of time when writing and debugging code.

Cross-Ref

For details on the advantages of early versus late binding refer to Microsoft Knowledge Base Article ID: Q245115, “Using Early Binding and Late Binding in Automation.”

We can perform early binding of ADO COM objects through the use of a .NET Framework utility called Tlbimp.exe. We cover this in the following section.

Early Binding ADO COM Objects in ASP.NET

The .NET Framework introduces two new classifications for object activation:

Managed objects

Unmanaged objects

Managed objects are objects created with .NET-compliant compilers such as C# and VB .NET. Unmanaged objects are the current generation of COM objects including the ADO objects. Managed objects take full advantage of the .NET Framework. For instance, managed objects can be changed without unloading the DLL. Managed objects don’t need to be registered using regsvr32; you can simply copy them from system to system without the headaches associated with DLL hell.

When you are using a managed object, it’s simple to make that object available within your application. All you have to do is import the objects into your code using the @ Import page directive:

<%@ Import namespace=”Myobject”>

To activate the object with VB you instantiate the object as follows.

Dim thisObject as New MyObject()

Working with unmanaged objects is slightly more complicated, as the .NET Framework cannot just access the object as it normally would with managed code objects. In order to use unmanaged objects such as ADO, you need to use Runtime Callable Wrappers (RCW). RCW act as a proxy for the unmanaged object. These wrappers work just like any other managed class in the .NET runtime client, but they just marshal calls between managed and unmanaged code.

In order to use this approach and support early binding of ADO COM objects in an *.aspx page you will need to do the following:

1.Create the RCW for the ADO object, in this case msado15.dll.

2.Add the managed wrapper of the object to the bin directory.

3.Use the object as a normal managed code object.

Session 30—Migrating from ADO to ADO.NET

305

The type library importer utility (TlbImp.exe) is responsible for converting the type definitions found within a COM type library into equivalent definitions in the .NET runtime metadata format. (A full detailed documentation of the utility can be found in the .NET documentation.) In order to use this utility so that you can incorporate your ADO library elements for use in your ASP.NET pages you have to do the following:

1.Locate the ADO objects, typically located at C:\Program Files\Common Files\ system\ado\msado15.dll.

2.Locate the TlbImp.exe file, typically located at C:\Program Files\Microsoft.Net\FrameworkSDK\Bin.

3.Run the TlbImp.Exe import utility as follows:

[Insert Path]\TlbImp [Insert Path]\msado15.dll /out: [Destination Path]\ADODB.dll

This will create a DLL named ADODB.dll with the RCW wrapper for use in your .NET applications. Now that the wrapper is created, the next thing to do is to copy the ADODB.dll to the bin directory of your ASP.NET application. If you do not have a bin directory, you should create one under your application root.

Once the previous steps are carried out, instantiating the object is the same as using any normal managed object. You set up the namespace and the assembly to reflect the ADODB.dll we created earlier.

<%@ Import Namespace=”ADODB”%> <%@ Assembly Name = “ADODB”%> <%@ Page Language=”VB”%>

Then you can just access the ADO COM object as you would any managed COM component and get the benefits associated with early binding as illustrated in Listing 30-3.

Listing 30-3 Accessing an ADO.COM object with early binding

<%@ Import Namespace=”ADODB”%> <%@ Assembly Name = “ADODB”%> <%@ Page Language=”VB”%> <HTML>

<HEAD>

<TITLE>ADO Access from ASP.NET with Early Binding</TITLE> </HEAD>

<BODY>

<%

DIM oConn as New ADODB.Connection DIM oRS as New ADODB.RecordSet DIM oCmd as New ADODB.Command

oConn.Open (“provider=sqloledb;Data Source=(local);Initial Catalog=pubs;User

ID=sa;pwd=”)

oRS.CursorType=ADODB.CursorTypeEnum.adOpenKeyset

Continued