- •Preface
- •Who Should Read This Book
- •Organization and Presentation
- •Contacting the Authors
- •Acknowledgments
- •Contents
- •Introduction
- •Why Microsoft .NET?
- •The Microsoft .NET Architecture
- •Internet Standards
- •The Evolution of ASP
- •The Benefits of ASP.NET
- •What Is .NET?
- •.NET Experiences
- •.NET Clients
- •.NET Services
- •.NET Servers
- •Review
- •Quiz Yourself
- •Installation Requirements
- •Installing ASP.NET and ADO.NET
- •Installing the .NET Framework SDK
- •Testing Your Installation
- •Support for .NET
- •Review
- •Quiz Yourself
- •Designing a Database
- •Normalization of Data
- •Security Considerations
- •Review
- •Quiz Yourself
- •Creating a Database
- •Creating SQL Server Tables
- •Creating a View
- •Creating a Stored Procedure
- •Creating a Trigger
- •Review
- •Quiz Yourself
- •INSERT Statements
- •DELETE Statements
- •UPDATE Statements
- •SELECT Statements
- •Review
- •Quiz Yourself
- •The XML Design Specs
- •The Structure of XML Documents
- •XML Syntax
- •XML and the .NET Framework
- •Review
- •Quiz Yourself
- •ASP.NET Events
- •Page Directives
- •Namespaces
- •Choosing a Language
- •Review
- •Quiz Yourself
- •Introducing HTML Controls
- •Using HTML controls
- •How HTML controls work
- •Intrinsic HTML controls
- •HTML Control Events
- •The Page_OnLoad event
- •Custom event handlers
- •Review
- •Quiz Yourself
- •Intrinsic Controls
- •Using intrinsic controls
- •Handling intrinsic Web control events
- •List Controls
- •Rich Controls
- •Review
- •Quiz Yourself
- •Creating a User Control
- •Adding User Control Properties
- •Writing Custom Control Methods
- •Implementing User Control Events
- •Review
- •Quiz Yourself
- •Common Aspects of Validation Controls
- •Display property
- •Type Property
- •Operator Property
- •Using Validation Controls
- •RequiredFieldValidator
- •RegularExpressionValidator
- •CompareValidator
- •RangeValidator
- •CustomValidator
- •ValidationSummaryx
- •Review
- •Quiz Yourself
- •Maintaining State Out of Process for Scalability
- •No More Cookies but Plenty of Milk!
- •Out of Process State Management
- •Review
- •Quiz Yourself
- •Introducing the Key Security Mechanisms
- •Web.config and Security
- •Special identities
- •Using request types to limit access
- •New Tricks for Forms-based Authentication
- •Using the Passport Authentication Provider
- •Review
- •Quiz Yourself
- •ASP.NET Updates to the ASP Response Model
- •Caching with ASP.NET
- •Page Output Caching
- •Absolute cache expiration
- •Sliding cache expiration
- •Fragment Caching
- •Page Data Caching
- •Expiration
- •File and Key Dependency and Scavenging
- •Review
- •Quiz Yourself
- •A Brief History of Microsoft Data Access
- •Differences between ADO and ADO.NET
- •Transmission formats
- •Connected versus disconnected datasets
- •COM marshaling versus text-based data transmission
- •Variant versus strongly typed data
- •Data schema
- •ADO.NET Managed Provider Versus SQL Managed Provider
- •Review
- •Quiz Yourself
- •Review
- •Quiz Yourself
- •Creating a Connection
- •Opening a Connection
- •Using Transactions
- •Review
- •Quiz Yourself
- •Building a Command
- •Connection property
- •CommandText property
- •CommandType property
- •CommandTimeout property
- •Appending parameters
- •Executing a Command
- •ExecuteNonQuery method
- •Prepare method
- •ExecuteReader method
- •Review
- •Quiz Yourself
- •Introducing DataReaders
- •Using DataReader Properties
- •Item property
- •FieldCount property
- •IsClosed property
- •RecordsAffected property
- •Using DataReader Methods
- •Read method
- •GetValue method
- •Get[Data Type] methods
- •GetOrdinal method
- •GetName method
- •Close method
- •Review
- •Quiz Yourself
- •Constructing a DataAdapter Object
- •SelectCommand property
- •UpdateCommand, DeleteCommand, and InsertCommand properties
- •Fill method
- •Update method
- •Dispose method
- •Using DataSet Objects
- •DataSetName property
- •CaseSensitive property
- •Review
- •Quiz Yourself
- •Constructing a DataSet
- •Tables property
- •TablesCollection Object
- •Count property
- •Item property
- •Contains method
- •CanRemove method
- •Remove method
- •Add method
- •DataTable Objects
- •CaseSensitive property
- •ChildRelations property
- •Columns property
- •Constraints property
- •DataSet property
- •DefaultView property
- •ParentRelations property
- •PrimaryKey property
- •Rows property
- •Dispose method
- •NewRow method
- •Review
- •Quiz Yourself
- •What Is Data Binding?
- •Binding to Arrays and Extended Object Types
- •Binding to Database Data
- •Binding to XML
- •TreeView Control
- •Implement the TreeView server control
- •Review
- •Quiz Yourself
- •DataGrid Control Basics
- •Binding a set of data to a DataGrid control
- •Formatting the output of a DataGrid control
- •Master/Detail Relationships with the DataGrid Control
- •Populating the Master control
- •Filtering the detail listing
- •Review
- •QUIZ YOURSELF
- •Updating Your Data
- •Handling the OnEditCommand Event
- •Handling the OnCancelCommand Event
- •Handling the OnUpdateCommand Event
- •Checking that the user input has been validated
- •Executing the update process
- •Deleting Data with the OnDeleteCommand Event
- •Sorting Columns with the DataGrid Control
- •Review
- •Quiz Yourself
- •What Is Data Shaping?
- •Why Shape Your Data?
- •DataSet Object
- •Shaping Data with the Relations Method
- •Review
- •Quiz Yourself
- •OLEDBError Object Description
- •OLEDBError Object Properties
- •OLEDBError Object Methods
- •OLEDBException Properties
- •Writing Errors to the Event Log
- •Review
- •Quiz Yourself
- •Introducing SOAP
- •Accessing Remote Data with SOAP
- •SOAP Discovery (DISCO)
- •Web Service Description Language (WSDL)
- •Using SOAP with ASP.NET
- •Review
- •Quiz Yourself
- •Developing a Web Service
- •Consuming a Web Service
- •Review
- •Quiz Yourself
- •ASP and ASP.NET Compatibility
- •Scripting language limitations
- •Rendering HTML page elements
- •Using script blocks
- •Syntax differences and language modifications
- •Running ASP Pages under Microsoft.NET
- •Using VB6 Components with ASP.NET
- •Review
- •Quiz Yourself
- •Preparing a Migration Path
- •ADO and ADO.NET Compatibility
- •Running ADO under ASP.NET
- •Early Binding ADO COM Objects in ASP.NET
- •Review
- •Quiz Yourself
- •Answers to Part Reviews
- •Friday Evening Review Answers
- •Saturday Morning Review Answers
- •Saturday Afternoon Review Answers
- •Saturday Evening Review Answers
- •Sunday Morning Review Answers
- •Sunday Afternoon Review Answers
- •What’s on the CD-ROM
- •System Requirements
- •Using the CD with Windows
- •What’s on the CD
- •The Software Directory
- •Troubleshooting
- •ADO.NET Class Descriptions
- •Coding Differences in ASP and ASP.NET
- •Retrieving a Table from a Database
- •Displaying a Table from a Database
- •Variable Declarations
- •Statements
- •Comments
- •Indexed Property Access
- •Using Arrays
- •Initializing Variables
- •If Statements
- •Case Statements
- •For Loops
- •While Loops
- •String Concatenation
- •Error Handling
- •Conversion of Variable Types
- •Index
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