Tuesday, 24 July 2007

Creating/ Opening Database Connection

Creating Connection to the Database:
There are a couple of ways of doing this either by using a system DSN or a DSN-less connection.

To create a DSN-less connection to an Access database we need tell the connection object we created above to open the database by telling the connection object to use the 'Microsoft Access Driver' to open the database 'guestbook.mdb'. For example:

'Set an active connection to the Connection object using a DSN-less connection
adoCon.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("guestbook.mdb")

You'll notice the ASP method 'Server.MapPath' in font of the name of the database. This is used as we need to get the physical path to the database.
Server.MapPath returns the physical path to the script, e.g. 'c:\website\', as long as the database is in the same folder as the script it now has the physical path to the database and the database name.

If on the other hand you want to use a slower DSN connection to the database then you will need to replace the line above with the one below.

'Set an active connection to the Connection object using DSN connection
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Open "accessDSN"
%>

Error generated:

Microsoft OLE DB Provider for ODBC Drivers error '80004005'

[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

/phr004/coursework1/manageproducts.asp, line 45

Solution:

I carefully managed to get this code done correctly to get the database connection;

Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Open "dsn=phr004cw1;uid=phr004;pwd=ce95099wi;"