Wrox Programmer Forums
|
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 25th, 2004, 01:00 PM
Registered User
 
Join Date: Feb 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to mpearce
Default Which DB connection method is better?

Hi,
  I have seen two methods for connecting to databases and was just wondering which is better and for what situations?

  I've been using method 1 so far, but I haven't seen many other people using it. It seems more people use method 2, but I can't find a good resources on how to use it correctly.

Any help would be appreciated.

Method 1:
Code:
<%
Dim getBooks
Dim BookImg, BookName, BookDesc
Set getBooks = Server.CreateObject("ADODB.Recordset")
getBooks.ActiveConnection = MM_database_STRING  ' MM_database_STRING from include file.
getBooks.Source = "SELECT * FROM Books"
getBooks.CursorType = 0
getBooks.CursorLocation = 2
getBooks.LockType = 1
getBooks.Open()

While (NOT getBooks.EOF AND NOT getBooks.BOF) 
    BookImg = getBooks.fields.item("Book_IMG").value
    BookName = getBooks.fields.item("Book_NAME").value
    BookDesc = getBooks.fields.item("Book_DESC").value
    %>
    <tr>
        <td>&nbsp;&nbsp;</td>
        <td align="center"><img src="<%=BookImg%>" alt="<%=BookName%>"></td>
        <td>&nbsp;&nbsp;</td>
        <td><b><%=BookName%></b>&nbsp;-&nbsp;<%=BookDesc%></td>
    </tr>
    <%
    getBooks.movenext()
Wend
getBooks.Close()
Set getBooks = Nothing
%>
Method 2:
Code:
<%
Dim adOpenForwardOnly, adLockReadOnly, adCmdTable
adOpenForwardOnly = 0
adLockReadOnly = 1
adCmdTable = 2

Dim objConn, objRS
Set objConn = Server.CreateObject("ADODB.Connection")
Set objRS = Server.CreateObject("ADODB.Recordset")
'Now we use this selection to open the connection in the appropriate way
  objConn.Open "Provider=SQLOLEDB;Persist Security Info=False;" & _
               "User ID=sa;Initial Catalog=Movie;" & _
               "Initial File Name=C:\MSSQL7\Data\Movie2000.mdf"
objRS.Open "Movies", objConn, adOpenForwardOnly, adLockReadOnly, adCmdTable

While Not objRS.EOF
  Response.Write objRS("Title") & "<BR>"
  objRS.MoveNext
Wend

objRS.Close
objConn.Close
Set objRS = Nothing
Set objConn = Nothing 
%>
Thanks.

 
Old February 25th, 2004, 02:42 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Well, I guess it all depends on whether you need Books or Movies, I guess ;)

Just kidding. I think there is a third variation that, IMO, is even better. When you can use that solution depends on your needs. Before I get into the third solution, I'll command on these 2 first.

I find method 1 hard to read. What is a CursorType of 0 or a CursorLocation of 2? So, explicitly defining your constants is better because it makes your code easier to read. For more details on a clean way to include these variables, look here.

The first method allows you to define your SQL statement yourself. That's good, as it will also allow you to limit the number of columns and records retrieved from the database. So, for example, you could use SELECT ID, Description FROM MyTable WHERE ID = 1, instead of SELECT * FROM MyTable. (Take a look here to see why SELECT * is bad)

The second method opens the entire table. adCmdTable means you want to open the table instead of opening a selected (no pun intended) subset of records. If you do need all records, it will work. If you only need a subset of the records (and/or columns), you're out of luck.

So it all depends on your requirements. Need the entire table? Method 2 will work (although the overhead may be large, because you can't limit the number of columns). Need to filter records based on, say, the LastName of a user? You have to use method 1.

So, then, what is behind door number 3? Glad you asked. It's a modification of solution 1 which allows for fast, forward-only (think fire hose) data. It doesn't allow updating, ordering, filtering and all the other stuff you'll often see in desktop applications but that is usually useless in ASP pages. It does allow for dumping data on the page as quickly as possible. Here's how it looks:
Code:
Dim MyConnection
Dim MyRecordset
Dim MySQL

MySQL = "SELECT ID, Title FROM Events"

Set MyConnection = Server.CreateObject("ADODB.Connection")
MyConnection.ConnectionString = YourConnectionString
MyConnection.Open

Set MyRecordset = MyConnection.Execute(MySQL)
If Not MyRecordset.EOF Then
    Do While Not MyRecordset.EOF
        Response.Write("Record with ID " & _
            MyRecordset.Fields("ID").Value & _
            " has the title """ & _
            MyRecordset.Fields("Title").Value & """<br />")    
        MyRecordset.MoveNext()
    Loop            
Else
    Response.Write("No Data Found")
End If
MyConnection.Close
Set MyRecordset = Nothing
Set MyConnection = Nothing
After I have created and opened the Connection, I use its Execute method to get a forward-only, read-only recordset. This recordset allows you to dump its contents as quickly as possible on the page, something we all want in our Web app, no?

HtH,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old February 25th, 2004, 08:05 PM
Registered User
 
Join Date: Feb 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to mpearce
Default

Thanks for the response.. I will take a look at what you have posted and hopefully, figure out which is best for me.. :)






Similar Threads
Thread Thread Starter Forum Replies Last Post
db connection dfeuerborn VB Databases Basics 1 August 19th, 2005 02:08 PM
db connection dfeuerborn Excel VBA 1 August 19th, 2005 02:24 AM
db connection dfeuerborn Access VBA 1 August 17th, 2005 10:33 AM
DB Connection stu9820 ASP.NET 1.0 and 1.1 Basics 1 September 26th, 2004 09:01 PM
Open/Close SQL Connection within every method? flyin ADO.NET 8 May 18th, 2004 07:19 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.