Monday, September 22, 2003

Connect to Database (ASP)

The following connects to a database with ASP JScript:

<%
// create connection
var oConn = Server.CreateObject("ADODB.Connection");
// open connection to database
oConn.Open(CONNECTION_STRING);
// generate SQL
sSQL = "SELECT * FROM tablename";
// create recordset from SQL
var oRS = oConn.Execute(sSQL);
// work with recordset
Response.Write(oRS("fieldname"));
// close recordset and connection
oRS.Close();
oConn.Close;
%>

And with ASP VBScript:

<%
Dim oConn, sSQL, oRS
' create connection
Set oConn = Server.CreateObject("ADODB.Connection")
' open connection to database
oConn.Open(CONNECTION_STRING)
' generate SQL
sSQL = "SELECT * FROM tablename"
' create recordset from SQL
Set oRS = oConn.Execute(sSQL)
' work with recordset
Response.Write(oRS("fieldname"))
' close recordset and connection
oRS.Close()
Set oRS = Nothing
oConn.Close()
Set oConn = Nothing
%>

No comments: