Jump to content

Database connection tutorial


Xenon Design
 Share

Recommended Posts

Another one of my tutorials but this ones on connecting. There are two main ways of connecting to a database, using a DSN or Map.Path. I personally use DSN's because they are easy to setup and require less code.DSNYou need to set the system dsn before you try this code. If you dont know how to set up a DSN heres a good tutorial on how to set one up

<%'declare the SQL statement that will query the databasesql="SELECT * FROM table"'initialise the dsn variabledsn="dsn=my_DSN"  'my_DSN will be the name you have chosen for your dsn'Set the recordset and connection variableSet connection = Server.CreateObject("ADODB.Connection")Set recordset = Server.CreateObject("ADODB.Recordset")'Open the connection to the databaseconnection.Open dsn'Open the recordset object, execute the SQLrecordset.Open sql,connection%>

The dsn is set to 'my_DSN'. Map.Path

<%'declare the SQL statement that will query the databasesql="SELECT * FROM table"'Set the recordset and connection variableSet connection = Server.CreateObject("ADODB.Connection")Set recordset = Server.CreateObject("ADODB.Recordset")'define connection string, specify database driver and location of the databasedbPath="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _"Data Source=" & Server.MapPath("database.mdb")'<< SET DB PATH'Open the connection to the databaseconnection.Open(dbPath)'Open the recordset object executing the SQL statement and return recordsrecordset.Open sql,connection%>

You set the database url in the 'Server.MapPath' code.Which way you choose is up to you. There are advantages and disadvantages of choosing one or the other.Having problems with the script? Dont know how to do something?If you are having troubles with my script just reply with your problem and I will try to respond quickly. Also if theres any spelling mistakes just let me know and ill fix it :)Xenon Design :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...