|
|
| How to access SQL Server from ASP |
If you have aSQL Server account with us, it is very easy to access your database from ASP. Here is sample code for establishing a connection
|
Dim objDbConn
' Create the ADODB Connection object
Set objDbConn = Server.CreateObject( "ADODB.Connection" )
' Connect to the DB
objDbConn.Open( "DSN=UserDBconn;UID=youraccountname;PWD=yourpassword" )
|
|
That's all there is to it. Notice the DSN is named "UserDBconn" and the UID is your account name and PWD is your password.
|
|
How to access an MS Access database from ASP
|
Every one of our web sites can utilize an MS Access database. You must create the database and upload it to your /Db directory. This is very important - it will not function correctly from any other directory. The /Db directory has special write access that lets you update your database. Here is sample code for establishing a connection to the database:
|
Dim objDbConn
Set objDbConn = Server.CreateObject("ADODB.Connection")
objDbConn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("\youraccountname\db\MyDbName.mdb")
|
Make sure you specify the path correctly - including the name of your .mdb file (called "MyDbName.mdb" above).
|
| Executing a SQL statement |
Here is some sample code, utilizing the above connection, that executes a SQL statement on a table:
|
Dim objRs
' Run the query
Set objRs = objDbConn.Execute( "SELECT MyColumn FROM ExampleTable" )
' Print out all the records
While Not objRs.EOF
Response.Write( objRs( "MyColumn" ) & "<br>" )
' Move to the next record
objRs.MoveNext
Wend
|
|
|
|