Basic ADO and SQL Tutorial
page 2 of 5
by Steven Smith
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 48578/ 218

Connection Object

Now that you have a machine on which to practice, the first thing that is required for you to start using a database with your ASP pages is a connection. Using your editor of choice(Visual Interdev 6.0 is very nice because it has Intellisense, which will help you see the methods and properties of the various ADO objects), write the following code:

 Session("DB_ConnectionString") = _
 "DSN=myDSN;UID=myUID;PWD=myPWD;DB=myDB;HOST=myHOST;SERV=MyService;SRVR=MyServer;PRO=MyProtocol;"
 Dim objConnect   'Declare the connection variable
 'Set up Connection Object
 Set objConnect = Server.CreateObject("ADODB.Connection")
 objConnect.Open Session("DB_ConnectionString")

Notice that I'm using a Session variable for the connection string. This is useful if you plan on using the same database for multiple ASP pages, which is often the case. If you are using Visual Interdev, you can use the data connection wizard to create this Session variable for you. In VI 1.0, you do this by clicking on Project - Add to Project - Data Connection. If you do not already have a DSN, this will also let you create one. MAKE SURE YOU USE A SYSTEM DSN! If you use a File DSN, then the default web user account on Windows NT (IUSR_MACHINENAME) will not be able to access the database. Also note that many of the parameters in the connection string above are not necessary if you are using a database on the same machine as your server. In fact, you do not even need to use a DSN. An alternative, if you have the database as a file on your web server, is to do the following:

 Dim objConnect   'Declare the connection variable
 Dim cnpath    'Connection path to database file
 'Set up Connection Object
 Set objConnect = Server.CreateObject("ADODB.Connection")
 cnpath="DBQ=" & server.mappath("/path/database.mdb")
 objConnect.Open "DRIVER={Microsoft Access Driver (*.mdb)}; " & cnpath

This assumes that the database is located within your web directory, in the subdirectory "path". You can set cnpath to "C:\path\database.mdb" if you want it to use any path on your machine. Once you have established a connection, you are ready to move on to recordset and command objects.

Also note that you could (and should) use an Application variable instead of a Session variable, assuming everybody on your site will be connecting to the same database, which is typically the case.  Finally, it is very important that you only open connections when you need them, and that you keep them open as briefly as possible.  As soon as you're finished with them, call Connection.Close.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 2 and 8 and type the answer here:

User Comments

Title: nbp   
Name: www
Date: 5/17/2008 3:32:07 PM
Comment:
query of searching data in sqlserver with use of asp.net
Title: swd   
Name: www
Date: 4/29/2008 6:06:09 AM
Comment:
sweet.............
Title: Hi...   
Name: Hann
Date: 3/20/2007 9:49:00 AM
Comment:
Can U tell me how to connect MDB file in other host..?

Plizz, Send your reply to
hann21@linuxmail.org


Thanx
Title: hi   
Name: Paapu
Date: 1/23/2007 1:51:19 AM
Comment:
Its very useful, but still u can add some details also..
Title: hll   
Name: pat
Date: 11/17/2006 5:45:08 AM
Comment:
hello thanks a bunch but i guess i need more help
Title: DotNetSpace   
Name: Simple way to handle exception in database connections
Date: 10/26/2006 1:59:35 PM
Comment:
This is a simple way to handle exceptions when using database queries. This link includes an example:

http://www.dotnetspace.com/index.php?option=com_content&task=view&id=19&Itemid=26
Title: DotNetSpace   
Name: DotNetSpace query database connection example
Date: 10/26/2006 1:58:44 PM
Comment:
Shows a simple way to handle exceptions when using database queries. This link includes the example.
Title: use of loks in asp   
Name: praveen
Date: 7/11/2006 9:56:53 AM
Comment:
Hi

Please provide me the information about locks.
Title: asp stuff   
Name: dhvanil
Date: 4/6/2006 10:12:35 AM
Comment:
hi
your site is good
put some examples of every methods
Title: webmaster   
Name: Yusef Jeffries-El
Date: 1/15/2006 1:10:43 AM
Comment:
I just needed a quick reference to the syntax of the INSERT INTO statement using Visual Basic
Title: hi   
Name: vishal
Date: 6/3/2005 10:17:27 AM
Comment:
vishal patel your site is so genious but add the download button on every page






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 12/2/2008 9:44:55 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search