Вы находитесь на странице: 1из 4

<%

strconn="DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("myDatabase.mdb")

' Create the connection and recordset objects


Set db = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.RecordSet")

' The SQL statement


Sql = "SELECT ......"
db.Open strconn
rs.Open Sql, strconn, 3
'Do what you want with your recordset here
rs.Close
db.Close

' Don't forget to destroy the objects


Set rs = Nothing
Set db = Nothing
%>

Guide to Database Connection Strings

Category ASP General


Overview Below is an overview of how to choose the correct database connection string for your
situation.
Details OLEDB vs ODBC -- Which should you use?
Given the option, it's a no brainer to use OLEDB over ODBC. ODBC has been
around for years and is based on older and more bug laden technology. Therefore, ALWAYS use
OLEDB if your server/host supports it. Microsoft even says it themselves:

When running Microsoft Jet in an IIS environment, it is recommended that you use
the native Jet OLE DB Provider in place of the Microsoft Access ODBC driver. The
Microsoft Access ODBC driver (Jet ODBC driver) can have stability issues due to the
version of Visual Basic for Applications that is invoked because the version is not
thread safe. As a result, when multiple concurrent users make requests of a
Microsoft Access database, unpredictable results may occur. The native Jet OLE DB
Provider includes fixes and enhancements for stability, performance, and thread
pooling (including calling a thread-safe version of Visual Basic for Applications).

Now that you've picked which database method you will be using, here is a
list of possible connection strings, based on whether you are using Microsoft Access or SQL Server and
OLEDB or ODBC:

SQL Server

ODBC:

• Standard Security:
"Driver={SQL Server};Server=Aron1;Database=pubs;Uid=sa;Pwd=asdasd;"

• Trusted connection:
"Driver={SQL Server};Server=Aron1;Database=pubs;Trusted_Connection=yes;"

• Prompt for username and password:


oConn.Properties("Prompt") = adPromptAlways
oConn.Open "Driver={SQL Server};Server=Aron1;DataBase=pubs;"

1
OLEDB:

• Standard Security:
"Provider=sqloledb;Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;")

• Trusted Connection:
"Provider=sqloledb;Data Source=Aron1;Initial Catalog=pubs;Integrated Security=SSPI;"
(use serverName\instanceName as Data Source to use a specific SQLServer instance, only
SQLServer2000)

• Prompt for username and password:


oConn.Provider = "sqloledb"
oConn.Properties("Prompt") = adPromptAlways
oConn.Open "Data Source=Aron1;Initial Catalog=pubs;"

• Connect via an IP address:


"Provider=sqloledb;Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial
Catalog=pubs;User ID=sa;Password=asdasd;"
(DBMSSOCN=TCP/IP instead of Named Pipes, at the end of the Data Source is the port to
use (1433 is the default))

Access:

ODBC

• Standard Security:
"Driver={Microsoft Access
Driver(*.mdb)};Dbq=\somepath\mydb.mdb;Uid=Admin;Pwd=asdasd;"

• Workgroup:
"Driver={Microsoft Access Driver
*.mdb)};Dbq=\somepath\mydb.mdb;SystemDB=\somepath\mydb.mdw;","admin",""

OLEDB

• Standard security:
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\somepath\mydb.mdb;User
Id=admin;Password=asdasd;"

• Workgroup (system database):


"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\somepath\mydb.mdb;Jet
OLEDB:System Database=system.mdw;","admin", ""
• With password:
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\somepath\mydb.mdb;Jet
OLEDB:Database Password=MyDbPassword;","admin", ""

2
Making Database Connections
Opening a connection to a database is a fairly simple process: first, you create a connection
object, then you open the object using either a DSN or specified drivers and path. The latter is
called a DSNless connection, as it requires no system DSN to be set in the server's ODBC applet
in the Control Panel. If you'd like information on setting up a system DSN, be sure to see Setting
A System DSN, or if you prefer a more visual approach, check out ASPKicker's ODBC
Configuration Page.
MS Access
With DSN and no User ID/Password:
<%
set conn = Server.CreateObject("ADODB.Connection")
conn.open "DSNname"
%>

With DSN and User ID/Password:


<%
set conn = Server.CreateObject("ADODB.Connection")
conn.open "DSNname","username","password"
%>

DSNless

Using physical path as a reference:


<%
Set conn = Server.CreateObject("ADODB.Connection")
DSNtemp="DRIVER={Microsoft Access Driver (*.mdb)}; "
DSNtemp=dsntemp & "DBQ=e:\DataArea\financials.mdb"
conn.Open DSNtemp
%>

Using Server.MapPath, which is the path from the webserver root (by default, c:\inetpub\wwwroot)
<%
Set conn = Server.CreateObject("ADODB.Connection")
DSNtemp="DRIVER={Microsoft Access Driver (*.mdb)}; "
DSNtemp=dsntemp & "DBQ=" & Server.MapPath("/database/casino.mdb")
conn.Open DSNtemp
%>

MS SQL Server
With DSN
<%
set conn = Server.CreateObject("ADODB.Connection")
conn.open "DSN=MyDSN;UID=user;PWD=password;DATABASE=databasename"
%>

3
SQL Server: DSNLess
<%
Set conn = Server.CreateObject("ADODB.Connection")
DSNtemp="DRIVER={SQL
Server};SERVER=ServerName;UID=USER;PWD=password;DATABASE=databasename"
conn.open DSNtemp
%>

FoxPro: DSNLess
<%
Set Conn = Server.CreateObject("ADODB.connection")
ConnStr= "Driver=Microsoft Visual Foxpro Driver;
UID=userID;SourceType=DBC;SourceDB=C:\path\to\database.dbc"
Conn.Open ConnStr
%>
Oracle:
I am just now experimenting with Oracle, and I hope to have more information available at a later
date. However, I've gotten as far as connecting to an Oracle database and successfully displayed
records, so that's a start :) I have found, however, that (1) you really have to use cursorlocation or
things do not work too well and (2) OLEDB is very cool.
ODBC with DSN:

<%
set conn=server.createobject("adodb.connection")
conn.cursorlocation=adUseClient ' requires use of adovbs.inc; numeric value is 3
conn.open "DSN=your_DSN_name;UID=kathi;PWD=xxxxxx"
%>

OLEDB

<%
set conn=server.createobject("adodb.connection")
conn.cursorlocation=adUseClient ' requires use of adovbs.inc; numeric value is 3
DSNTemp="Provider=MSDAORA.1;Password=xxxxx;User ID=kathi;Data Source=xxx.world"
conn.open DSNtemp
%>

Вам также может понравиться