HOWTO: Read/Write Database Connections
If you are hosting an Access database on your site, you may want to create a read/write connection to the database in ASP code. This article details the proper way to create such a connection.
First and foremost, your database must be stored in a directory in which the permissions have been relaxed to allow for updating. We recommend that you use a subdirectory under your main site directory, and contact us at
support@atlcon.net to have the permissions relaxed and/or have a DSN (Data Services Name) set up for you. (See
this article for more information on DSN-less connections).
Once you have the permissions set properly, creating a read/write connection is fairly straightforward. Simply use the following code snippet as a guide:
<%
'
' NOTE: This code assumes your site is a
' virtual web hosting site under
' http://w3.atlcon.net
' (e.g., http://w3.atlcon.net/~yoursite)
'
<!--#include virtual="/adovbs.inc" -->
'
' Now open a connection objConn
' (example here)
'
' First map the path to your database
db = Server.MapPath _
("/~yourweb/data/yourdb.mdb")
'
' Now create a new connection object
Set objConn = Server.CreateObject("ADODB.Connection")
'
' Build the DSNless connection string
strconn="PROVIDER=Microsoft.Jet.OLEDB.4.0;"
strconn=strconn & "DATA SOURCE="
strconn=strconn & db & ";"
'
' Open the database!
objConn.Open strconn
'
' Now create the Recordset
'
Dim objRS
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open "TableName", _
objConn, adOpenKeyset, adLockOptimistic
'
' Now you can add records
'
objRS.AddNew
objRS("FieldName") = "Somevalue"
objRS("AnotherField") = "Anothervalue"
objRS.Update
%>
Posted by WebWiz at February 05, 2003 02:25 PM