<%@ LANGUAGE = VBScript %> <% Option Explicit %> <% Response.Expires= -1 %> DB Basics (Connection & Query) (ASP)

DB Basics (Connection & Query) (ASP)

See the source code (ASP) for the examples.

<% //Provider info: //Microsoft Access 97 - Microsoft Jet 3.51 OLE DB Provider //Microsoft Access 2000 - Microsoft Jet 4.0 OLE DB Provider //Microsoft Access XP - Microsoft Jet 4.0 OLE DB Provider //Example of connecting to a Database. Dim dbcnUsers ' As ADODB.Connection Dim strDbPathname ' As String Dim strConnection ' As String strDbPathname = Server.MapPath("MyDbAsp.mdb") strConnection = _ "Provider=Microsoft.Jet.OLEDB.4.0;" _ & "Data Source=" & strDbPathname Set dbcnUsers = Server.CreateObject("ADODB.Connection") dbcnUsers.Open(strConnection) %>

1. Connected to database: <%= strDbPathname%>.

2. Example of displaying data from a record set.

<% //Example of running a SQL query. Dim rsUsers ' As ADODB.Recordset Dim strSql ' As String strSql = "select * from users " _ & "order by username" Set rsUsers = dbcnUsers.Execute( strSql ) //Returns a RecordSet //Example of publishing the results of the query. response.write( "
" ) response.write( "

SQL Query executed: " & strSql & "

" ) response.write( "

" ) response.write( "List of Users
" ) response.write( "

" ) response.write( "

" ) response.write( "
" ) %>

3. Close (Recordset & Connection)

<% rsUsers.Close Set rsUsers = Nothing dbcnUsers.Close Set dbcnUsers = Nothing %>