连接数据库查询手册
类别: ASP教程
下面将简单介绍一下几种ADO连接方式:ODBC DSN,ODBC DSN-Less,
OLE DB Provider,和"MS Remote" Provider.
1。ODBC DSN连接
I.DSN
oConn.Open "DSN=AdvWorks;" & _
"UID=Admin;" & _
"PWD=;"
注意:从MDAC2.1开始就不能够在使用这样的方式了,就是只把DSN文件名放在ConnectString中
你必须同时使用DSN,UID,PWD标志。例如下面的方式在MDAC 2.1中将会出错:
oConn.Open "AdvWorks"
II.File DSN
oConn.Open "FILEDSN=somepathmydb.dsn;" & _
"UID=Admin;" & _
"PWD=;"
III.ODBC DSN-Less Connections
a)ODBC Text Driver
oConn.Open _
"Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
"Dbq=somepath;" & _
"Extensions=asc,csv,tab,txt;" & _
"Persist Security Info=False"
注意:需要在SQL语句中指定使用到的文件名。例如:
oRs.Open "Select * From customer.csv", _
oConn, adOpenStatic, adLockReadOnly, adCmdText
b)ODBC Driver for Access
i)普通安全模式:
oConn.Open _
"Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=somepathmydb.mdb;" & _
"Uid=Admin;" & _
"Pwd=;"
ii)如果使用了System database:
oConn.Open _
"Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=somepathmydb.mdb;" & _
"SystemDB=somepathmydb.mdw;", _
"admin", ""
c)ODBC Driver for SQL Server
i)普通安全模式
oConn.Open "Driver={SQL Server};" & _
"Server=carl2;" & _
"Database=pubs;" & _
"Uid=sa;" & _
"Pwd=;"
ii)使用信任安全模式:
oConn.Open "Driver={SQL Server};" & _
"Server=carl2;" & _
"Database=pubs;" & _
"Uid=;" & _
"Pwd=;"
注意:要使用空白的Uid和Pwd
d)ODBC Driver for Oracle
i)使用现有的Oracle ODBC Driver from Microsoft:
oConn.Open _
"Driver={Microsoft ODBC for Oracle};" & _
"Server=OracleServer.world;" & _
"Uid=demo;" & _
"Pwd=demo;"
ii)使用老版本的Oracle ODBC Driver from Microsoft:
oConn.Open _
"Driver={Microsoft ODBC Driver for Oracle};" & _
"ConnectString=OracleServer.world;" & _
"Uid=demo;" & _
"Pwd=demo;"
IIII)使用微软的OLE DB Data Link Connections方式Data Link File (UDL)
a)使用绝对路径
oConn.Open "File Name=somepathpubs.udl;"
b)使用相对路径
oConn.Open "File Name=pubs.udl;"
V)OLE DB Provider Connections方式
a)OLE DB Provider for ODBC Databases
i)Access (Jet):
oConn.Open _
"Provider=MSDASQL;" & _
"Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=somepathmydb.mdb;" & _
"Uid=Admin;" & _
"Pwd=;"
ii)SQL Server:
oConn.Open _
"Provider=MSDASQL;" & _
"Driver={SQL Server};" & _
"Server=carl2;" & _
"Database=pubs;" & _
"Uid=sa;" & _
"Pwd=;"
b)OLE DB Provider for Microsoft Jet (Access)
i)普通安全模式:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=somepathmydb.mdb;" & _
"User Id=admin;" & _
"Password=;"
ii)如果使用了System database:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=somepathmydb.mdb;" & _
"Jet OLEDB:System Database=system.mdw;", _
"admin", ""
注意:当使用OLE DB Provider4.0版本是,需要把MDB和MDW文件转换成4.0的数据库格式
iii)如果MDB需要一个数据库密码的话:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=somepathmydb.mdb;" & _
"Jet OLEDB:Database Password=MyDbPassword;", _
"admin", ""
c)OLE DB Provider for Excel Spreadsheet:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=somepathexpenses.xls;" & _
"Extended Properties=""Excel 8.0;HDR=Yes;"";"
注意: "HDR=Yes"表示在第一行中是行标题,在provider中将不把第一行包括入recordset中
d)OLE DB Provider for SQL Server
i)普通安全模式:
oConn.Open "Provider=sqloledb;" & _
"Network Library=DBMSSOCN;" & _
"Data Source=carl2;" & _
"Initial Catalog=pubs;" & _
"User Id=sa;" & _
"Password=;"
ii)使用信任安全模式:
oConn.Open "Provider=sqloledb;" & _
Network Library=DBMSSOCN;" & _
"Data Source=carl2;" & _
"Initial Catalog=pubs;" & _
"Trusted_Connection=yes;"
注意:"Network Library=DBMSSOCN"声明OLE DB使用TCP/IP替代Named Pipes.
e)OLE DB Provider for Oracle
oConn.Open "Provider=msdaora;" & _
"Data Source=OracleServer.world;" & _
"User Id=sa;" & _
"Password=;"
(VI)Remote OLE DB Provider Connections方式(就是我一直在研究的RDS方式哦,呵呵。):
a)MS Remote - Access (Jet)
i)ODBC DSN:
oConn.Open "Remote Server=http://carl2;" & _
"Remote Provider=MSDASQL;" & _
"DSN=AdvWorks;" & _
"Uid=admin" & _
"Pwd=;"
ii)OLE DB Provider:
oConn.Open "Provider=MS Remote;" & _
"Remote Server=http://carl2;" & _
"Remote Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=somepathmydb.mdb;", _
"admin", ""
iii)OLE DB Provider自定义事务对象
oConn.Open "Provider=MS Remote;" & _
"Remote Server=http://carl2;" & _
"Handler=MSDFMAP.Handler;" & _
"Data Source=MyAdvworksOLEDBConnectTag;"
b)MS Remote - SQL Server
i)ODBC DSN:
oConn.Open "Remote Server=http://carl2;" & _
"Remote Provider=MSDASQL;" & _
"Network Library=DBMSSOCN;" & _
"DSN=Pubs;" & _
"Uid=sa" & _
"Pwd=;"
ii)OLE DB Provider
oConn.Open "Provider=MS Remote;" & _
"Remote Server=http://carl2;" & _
"Remote Provider=SQLOLEDB;" & _
"Network Library=DBMSSOCN;" & _
"Data Source=carl2;" & _
"Initial Catalog=pubs;" & _
"User ID=sa;" & _
"Password=;"
OLE DB Provider,和"MS Remote" Provider.
1。ODBC DSN连接
I.DSN
oConn.Open "DSN=AdvWorks;" & _
"UID=Admin;" & _
"PWD=;"
注意:从MDAC2.1开始就不能够在使用这样的方式了,就是只把DSN文件名放在ConnectString中
你必须同时使用DSN,UID,PWD标志。例如下面的方式在MDAC 2.1中将会出错:
oConn.Open "AdvWorks"
II.File DSN
oConn.Open "FILEDSN=somepathmydb.dsn;" & _
"UID=Admin;" & _
"PWD=;"
III.ODBC DSN-Less Connections
a)ODBC Text Driver
oConn.Open _
"Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
"Dbq=somepath;" & _
"Extensions=asc,csv,tab,txt;" & _
"Persist Security Info=False"
注意:需要在SQL语句中指定使用到的文件名。例如:
oRs.Open "Select * From customer.csv", _
oConn, adOpenStatic, adLockReadOnly, adCmdText
b)ODBC Driver for Access
i)普通安全模式:
oConn.Open _
"Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=somepathmydb.mdb;" & _
"Uid=Admin;" & _
"Pwd=;"
ii)如果使用了System database:
oConn.Open _
"Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=somepathmydb.mdb;" & _
"SystemDB=somepathmydb.mdw;", _
"admin", ""
c)ODBC Driver for SQL Server
i)普通安全模式
oConn.Open "Driver={SQL Server};" & _
"Server=carl2;" & _
"Database=pubs;" & _
"Uid=sa;" & _
"Pwd=;"
ii)使用信任安全模式:
oConn.Open "Driver={SQL Server};" & _
"Server=carl2;" & _
"Database=pubs;" & _
"Uid=;" & _
"Pwd=;"
注意:要使用空白的Uid和Pwd
d)ODBC Driver for Oracle
i)使用现有的Oracle ODBC Driver from Microsoft:
oConn.Open _
"Driver={Microsoft ODBC for Oracle};" & _
"Server=OracleServer.world;" & _
"Uid=demo;" & _
"Pwd=demo;"
ii)使用老版本的Oracle ODBC Driver from Microsoft:
oConn.Open _
"Driver={Microsoft ODBC Driver for Oracle};" & _
"ConnectString=OracleServer.world;" & _
"Uid=demo;" & _
"Pwd=demo;"
IIII)使用微软的OLE DB Data Link Connections方式Data Link File (UDL)
a)使用绝对路径
oConn.Open "File Name=somepathpubs.udl;"
b)使用相对路径
oConn.Open "File Name=pubs.udl;"
V)OLE DB Provider Connections方式
a)OLE DB Provider for ODBC Databases
i)Access (Jet):
oConn.Open _
"Provider=MSDASQL;" & _
"Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=somepathmydb.mdb;" & _
"Uid=Admin;" & _
"Pwd=;"
ii)SQL Server:
oConn.Open _
"Provider=MSDASQL;" & _
"Driver={SQL Server};" & _
"Server=carl2;" & _
"Database=pubs;" & _
"Uid=sa;" & _
"Pwd=;"
b)OLE DB Provider for Microsoft Jet (Access)
i)普通安全模式:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=somepathmydb.mdb;" & _
"User Id=admin;" & _
"Password=;"
ii)如果使用了System database:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=somepathmydb.mdb;" & _
"Jet OLEDB:System Database=system.mdw;", _
"admin", ""
注意:当使用OLE DB Provider4.0版本是,需要把MDB和MDW文件转换成4.0的数据库格式
iii)如果MDB需要一个数据库密码的话:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=somepathmydb.mdb;" & _
"Jet OLEDB:Database Password=MyDbPassword;", _
"admin", ""
c)OLE DB Provider for Excel Spreadsheet:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=somepathexpenses.xls;" & _
"Extended Properties=""Excel 8.0;HDR=Yes;"";"
注意: "HDR=Yes"表示在第一行中是行标题,在provider中将不把第一行包括入recordset中
d)OLE DB Provider for SQL Server
i)普通安全模式:
oConn.Open "Provider=sqloledb;" & _
"Network Library=DBMSSOCN;" & _
"Data Source=carl2;" & _
"Initial Catalog=pubs;" & _
"User Id=sa;" & _
"Password=;"
ii)使用信任安全模式:
oConn.Open "Provider=sqloledb;" & _
Network Library=DBMSSOCN;" & _
"Data Source=carl2;" & _
"Initial Catalog=pubs;" & _
"Trusted_Connection=yes;"
注意:"Network Library=DBMSSOCN"声明OLE DB使用TCP/IP替代Named Pipes.
e)OLE DB Provider for Oracle
oConn.Open "Provider=msdaora;" & _
"Data Source=OracleServer.world;" & _
"User Id=sa;" & _
"Password=;"
(VI)Remote OLE DB Provider Connections方式(就是我一直在研究的RDS方式哦,呵呵。):
a)MS Remote - Access (Jet)
i)ODBC DSN:
oConn.Open "Remote Server=http://carl2;" & _
"Remote Provider=MSDASQL;" & _
"DSN=AdvWorks;" & _
"Uid=admin" & _
"Pwd=;"
ii)OLE DB Provider:
oConn.Open "Provider=MS Remote;" & _
"Remote Server=http://carl2;" & _
"Remote Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=somepathmydb.mdb;", _
"admin", ""
iii)OLE DB Provider自定义事务对象
oConn.Open "Provider=MS Remote;" & _
"Remote Server=http://carl2;" & _
"Handler=MSDFMAP.Handler;" & _
"Data Source=MyAdvworksOLEDBConnectTag;"
b)MS Remote - SQL Server
i)ODBC DSN:
oConn.Open "Remote Server=http://carl2;" & _
"Remote Provider=MSDASQL;" & _
"Network Library=DBMSSOCN;" & _
"DSN=Pubs;" & _
"Uid=sa" & _
"Pwd=;"
ii)OLE DB Provider
oConn.Open "Provider=MS Remote;" & _
"Remote Server=http://carl2;" & _
"Remote Provider=SQLOLEDB;" & _
"Network Library=DBMSSOCN;" & _
"Data Source=carl2;" & _
"Initial Catalog=pubs;" & _
"User ID=sa;" & _
"Password=;"
- 上一篇: ASP应用中心得回放
- 下一篇: ASP深度揭密(下)
-= 资 源 教 程 =-
文 章 搜 索