Published: Friday, September 15, 2000
Data Access with ASP.NET
One of ASP's greatest features is the ease with which an ASP page can access, retrieve, and modify
database information. ASP.NET, the next generation of ASP, offers data access, but offers a new means to retrieve
the data: ADO+! (For more information on ASP.NET, be sure to read our ASP.NET
Article Index!) This article discusses what ADO+ is and how to use it in an ASP.NET page to access a database.
Also, since ADO+'s syntax and style differs rather dramatically from ADO, I will illustrate how to use old-school
ADO to "talk" to a database through an ASP.NET page.
Using Old-School ADO
If you've looked into ASP.NET much, you've probably been blown away. I know I have. I have been using ASP for over
two years and am very comfortable with ASP... then along comes Microsoft and throws this whole new beast
into the ring, laughing, "Everything you now know is outdated. B'wa ha ha!" The only thing constant in this
industry is change!
ASP.NET is not the only thing that Microsoft has radically changed from its predecessor - ADO+, the new, .NET way
to access data, is a radical change from ADO. If you are having a hard enough time wrapping your head around
ASP.NET and will go looney if you have to totally relearn data access as well, fear not. Old-school ADO can be
used in your ASP.NET pages... At the time of this writing (9/15/2000), I am uncertain of the performance differences
in an ASP.NET page between ADO and ADO+, but if you have concrete results, please do let me know! I assume ADO+ is more efficient, though.
Here is a simple ASP.NET page that will display all of the columns in the authors table of the
pubs database:
<%
Dim objConn
objConn = Server.CreateObject("ADODB.Connection")
objConn.Open("Provider=SQLOLEDB; Data Source=(local); " & _
"Initial Catalog=pubs; User ID=sa")
Dim objRS
objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open("SELECT * FROM authors", objConn)
Dim objField
Do While Not objRS.EOF
For Each objField in objRS.Fields
Response.Write(objField.Name & " - " & _
objField.Value & "<BR>")
Next
Response.Write("<P><HR><P>")
objRS.MoveNext()
Loop
objRS.Close()
objConn.Close()
objRS = Nothing
objConn = Nothing
%>
|
The above code is almost exactly as it would be in a regular ASP page. The main differences are syntactical
differences between the new VB 7 syntax and VBScript. For example, note that we do not use Set
statements anywhere. Rather than: Set objConn = Server.CreateObject("ADODB.Connection"), we simply
omit the Set keyword. Also, all method calls must have parenthesis. In old school ASP, we
might do something like:
objRS.Open "SELECT * FROM authors", objConn
|
but with ASP.NET and the new VB 7 syntax, we must put parenthesis around the Open method call!
Now that we've examined how to access data using old-school ADO, it's time to look at how to accomplish this task
using the new ADO+. This topic will be addressed in Part 2!
Read Part 2