asp的数据库连接

asp里有专门操作数据库的对象嘛。其实也就是两个对象:Connection对象和Recordset对象。
对于数据库的操作,首先就是连到数据库,然后在用Recordset对象对数据库做操作
连到数据库你可以用下面的代码:
Set myConn=Server.CreateObject("ADODB.Connection")
myConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("mydb/thedb.mdb")
myConn.Open
这个代码你可以直接拷贝过去用,只要把数据库的路径和名字改改就可以了。
连接好数据库后,你就可以用Recordset对象操作数据库了:
Set myRs = Server.CreateObject("ADODB.Recordset")
mySql = "select * from 表名 where id=" & myID
myRs.open mySql,myConn,1,3
这样你myRs就相当于那张表了,你可以用下面的代码向页面中输出查到的内容:
<%=myRs("browses")%>
其中browses是数据库的一个字段。
最后别忘了关闭相关对象:
myRs.close
myconn.close
set myconn=nothing
set myRs=nothing


ASP中连接数据库(ADODB对象):

一、OLEDB连接方法

1.OLE DB Provider for AS/400  
connstr = "Provider=IBMDA400;" Data source=myAS400;User Id=myUsername;Password=myPassword;"

2.OLE DB Provider for Active Directory Service
connstr = "Provider=ADSDSOObject;User Id=myUsername;Password=myPassword;"

3.OLE DB Provider for DB2
connstr = "Provider=DB2OLEDB;Network Transport Library=TCPIP;Network Address=MyServer;Package Collection=MyPackage;Host CCSID=1142Initial Catalog=MyDB;User ID=MyUsername;Password=MyPassword;"

4.OLE DB Provider for Index Server
connstr = "Provider=msidxs;Data source=MyCatalog;"

5.OLE DB Provider for Internet Publishing
connstr = "Provider=MSDAIPP.DSO;Data Source=url;User Id=myUsername;Password=myPassword;"

6.OLE DB Provider for Microsoft Jet(access-this way will be best)
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=dbpath;User Id=admin;Password=;"

7.OLE DB Provider for Microsoft Jet(Excel)
connstr = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=dbpath;Extended Properties=""Excel 8.0;HDR=Yes;"";"

["HDR=Yes" means that there is a header row in the cell range (or named range), so the provider will not include the first row of the selection into the recordset. If "HDR=No", then the provider will include the first row of the cell range (or named ranged) into the recordset]

9.OLE DB Provider for Microsoft Jet(textfile)
connstr = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=dbpath;Extended Properties=""text;HDR=Yes;FMT=Delimited;"";"

10.OLE DB Provider for ODBC Databases(access)
connstr = "Provider=MSDASQL;Driver={Microsoft Access Driver (*.mdb)};Dbq=dbpath;Uid=myUsername;Pwd=myPassword;"

11.OLE DB Provider for ODBC Databases(SQL)
connstr = "Provider=MSDASQL;Driver={SQL Server};Server=myServerName;Database=myDatabaseName;Uid=myUsername;"Pwd=myPassword;"

12.OLE DB Provider for Oracle (from Microsoft)
connstr = "Provider=msdaora;Data Source=MyOracleDB;User Id=myUsername;Password=myPassword;"

13.OLE DB Provider for Simple Provider
connstr = "Provider=MSDAOSP;Data Source=MSXML2.DSOControl.2.6;"

14.OLE DB Provider for SQL Server
"Provider=sqloledb;Data Source=myServerName;Initial Catalog=myDatabaseName;User Id=myUsername;Password=myPassword;"

[if connection with ip address]
connstr = "Provider=sqloledb;Data Source=xxx.xxx.xxx.xxx,1433;Network Library=DBMSSOCN;Initial Catalog=myDatabaseName;User ID=myUsername;Password=myPassword;"

[1433 为SQL数据库默认接口]

二、ODBC 连接方法

1.ODBC - DSN
connstr = "DSN=AdvWorks;Uid=Admin;Pwd=;"

2.ODBC - File DSN
connstr = "FILEDSN=dbpath;Uid=Admin;Pwd=;"
[eg:connstr = "FILEDSN=c:\somepath\mydb.dsn;Uid=Admin;Pwd=;"]

3.ODBC DSN-Less Connections(As/400)
connstr = "Driver={Client Access ODBC Driver (32-bit)};System=myAS400;Uid=myUsername;Pwd=myPassword;"

4.ODBC Driver for Access
connstr = "Driver={Microsoft Access Driver (*.mdb)};Dbq=dbpath;Uid=Admin;Pwd=;"

[If you are using a Workgroup (System database)]
connstr = "Driver={Microsoft Access Driver (*.mdb)};Dbq=c:\somepath\mydb.mdb;SystemDB=c:\somepath\mydb.mdw;", admin", ""

[If MDB is located on a network share]
connstr = "Driver={Microsoft Access Driver (*.mdb)};Dbq=\\myServer\myShare\myPath\myDb.mdb;"

5.ODBC Driver for dBASE
connstr = "Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=dbpath;"

6.ODBC Driver for Excel
connstr =  "Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=dbpath;DefaultDir=c:\somepath;"

7.ODBC Driver for MySQL (via MyODBC)
[to local]
connstr = "Driver={mySQL};Server=MyServerName;Option=16834;Database=mydb;"

[to connect to a remote database]
connstr = "Driver={mySQL};Server=db1.database.com;Port=3306;Option=131072;Stmt=;Database=mydb;Uid=myUsername;Pwd=myPassword;"

8.ODBC Driver for Oracle
[For the current Oracle ODBC Driver from Microsoft]
connstr = "Driver={Microsoft ODBC for Oracle};Server=OracleServer.world;Uid=myUsername;Pwd=myPassword;"

[For the older Oracle ODBC Driver from Microsoft]
connstr = "Driver={Microsoft ODBC Driver for Oracle};ConnectString=OracleServer.world;Uid=myUsername;Pwd=myPassword;"

9.ODBC Driver for Paradox
connstr = "Driver={Microsoft Paradox Driver (*.db)};DriverID=538;Fil=Paradox 5.X;DefaultDir=c:\dbpath\;Dbq=c:\dbpath\;CollatingSequence=ASCII;"

10.ODBC Driver for SQL Server
connstr = "Driver={SQL Server};Server=MyServerName;Database=myDatabaseName;Uid=myUsername;Pwd=myPassword;"

11.ODBC Driver for Sybase
 
[If using the Sybase System 11 ODBC Driver]
connstr = "Driver={SYBASE SYSTEM 11};Srvr=myServerName;Uid=myUsername;Pwd=myPassword;"

[If using the Intersolv 3.10 Sybase ODBC Driver]
connstr = "Driver={INTERSOLV 3.10 32-BIT Sybase};Srvr=myServerName;Uid=myUsername;Pwd=myPassword;"

12.ODBC Driver for Sybase SQL Anywhere
connstr = "ODBC; Driver=Sybase SQL Anywhere 5.0;DefaultDir=c:\dbpath\;Dbf=c:\sqlany50\mydb.db;Uid=myUsername;Pwd=myPassword;Dsn="""";"

13.ODBC Driver for Teradata
connstr = "Provider=Teradata;DBCName=MyDbcName;Database=MyDatabaseName;Uid=myUsername;Pwd=myPassword;"

14.ODBC Driver for Text
connstr = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=c:\somepath\;Extensions=asc,csv,tab,txt;Persist Security Info=False"

14.ODBC Driver for Visual FoxPro
[With a database container]
connstr = "Driver={Microsoft Visual FoxPro Driver};SourceType=DBC;SourceDB=c:\somepath\mySourceDb.dbc;Exclusive=No;"

[Without a database container (Free Table Directory)]
connstr = "Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=c:\somepath\mySourceDbFolder;Exclusive=No;"