📄 在asp中操作数据库的方法.txt
字号:
在ASP中操作数据库的方法
一、存取数据库的原理
在ASP中,用来存取数据库的对象统称ADO对象(Active Data Objects),主要含有三种对象:Connection、Recordset和Command,其中Connection负责打开或连接数据库,Recordset负责存取数据表,Command负责对数据库执行行动查询(Action Query)命令和执行SQL Server的Stored Procedure。只依靠这三个对象还是无法存取数据库的,还必须具有数据库存取的驱动程序:OLE DB驱动程序和ODBC驱动程序。对于任何一种数据库都必须有相对应的OLE DB驱动程序和ODBC驱动程序,ADO对象才能对数据库进行存取。
ADO对象必须与各种驱动程序相结合才能存取各种类型数据库,不同的数据库需要不同的驱动程序。在Windows 9x/NT的“开始”→“设置”→“控制面板” →“ODBC Data Source(32Bit)”中的“驱动程序”标签页,可以查证机器上究竟装了哪些驱动程序。
二、连接数据库和打开数据表
不同数据库的连接方法有所不同(即建立Connection实例的方法不一样),但建立Connection实例后,利用Recordset对象进行存取数据的方法却大同小异。下面对于不同的数据类型,编写了相对应的连接函数,在程序中直接引用即可。
程序用VB Script脚本语言编写。
1.建立MdbRecordset对象。MDB数据库是一个完整的数据库,内部可能含有若干个数据表,在此函数中,Connection的作用是连接数据库,Recordset的作用是打开数据表。
Function CreateMdbRecordset(数据库文件名, 数据表名或Select语句 )
Dim conn,Provider,DBPath
’建立Connection 对象
Set conn = Server.CreateObject(“ADODB.Connection”)
Provider=“Provider=Microsoft.Jet.OLEDB.4.0;”
DBPath = “Data Source=” & Server.MapPath(“数据库文件名”)
’打开数据库
conn.Open Provider & DBPath
Set CreateMdbRecordset = Server.CreateObject(“ADODB.Recordset”)
’打开数据表
CreateMdbRecordset.Open “数据表名”, conn, 2, 2
End Function
2.建立带密码的MDB数据库的Recordset对象。它的建立方式与建立不带密码的MDB数据库的Recordset对象类似,只是多了一个密码参数,即在与数据库连接时,必须给出密码信息。
Function CreateSecuredMdbRecordset( 数据库文件名, 数据表名或Select语句,password )
Dim conn,Provider,DBPath
’建立Connection 对象
Set conn = Server.CreateObject(“ADODB.Connection”)
Provider = “Provider=Microsof.Jet.OLEDB.4.0;”
DBPath = “Data Source=”& Server.MapPath(“数据库文件名”)
’连接数据库,注意要带有密码参数
conn.Open Provider & DBPath&“Jet OLEDB:Database Password=”&assword
Set CreateSecuredMdbRecordset = Server.
CreateObject(“ADODB.Recordset”)
’打开数据表
CreateSecuredMdbRecordset.Open “数据表名”, conn, 2, 2
End Function
3.DBF文件不是一个标准的数据库文件,只相当于标准数据库文件中的一个数据表,所以为了使用DBF文件,可以把所有的DBF文件放在一个目录下,这样把目录名看成标准数据库,每一个DBF文件相当于标准数据库中的数据表。下面函数中的Directory是DBF所在的目录名。
Function CreateDbfRecordset( 目录名, DBF文件名或Select语句 )
Dim conn,Driver,SourceType,DBPath
’建立Connection 对象
Set conn = Server.CreateObject(“ADODB.Connection”)
Driver=“Driver={Microsoft Visual FoxProDriver};” SourceType = “SourceType=DBF;”
DBPath=“SourceDB=” & Server.MapPath(“目录名”)
’调用Open 方法打开数据库
conn.Open Driver & SourceType & DBPath
Set CreateDbfRecordset = Server.CreateObject(“ADODB.Recordset”)
’打开DBF文件
CreateDbfRecordset.Open “DBF文件名或Select语句”, conn, 2, 2
End Function
4.由FoxPro生成的DBC数据库与MDB数据库相似,都是一个数据库包含几个数据表的形式,所以对DBC数据库的存取方法与MDB数据库相似。
Function CreateDbcRecordset( DBC数据库文件名, 数据表名或Select语句 )
Dim conn,Driver,SourceType,DBPath
’建立Connection 对象
Set conn = Server.CreateObject(“ADODB.Connection”)
Driver=“Driver={Microsoft Visual FoxPro Driver};”
SourceType = “SourceType=DBC;”
DBPath = “SourceDB=” & Server.MapPath(“DBC数据库文件名”)
’连接数据库
conn.Open Driver & SourceType & DBPath
Set CreateDbcRecordset = Server.CreateObject(“ADODB.Recordset”)
’打开数据表
CreateDbcRecordset.Open“数据表名或Select语句”, conn, 2, 2
End Function
5.将Excel生成的XLS文件(book)看成一个数据库,其中的每一个工作表(sheet)看成一个数据库表。
Function CreateExcelRecordset(XLS文件名,Sheet名)
Dim conn.Driver,DBPath
’建立Connection对象
Set conn = Server.CreateObject(“ADODB.Connection”)
Driver=“Driver={Microsoft Excel Driver (*.xls)};”
DBPath = “DBQ=” & Server.MapPath(“XLS文件名”)
’调用Open 方法打开数据库
conn.Open Driver & DBPath
Set CreateExcelRecordset = Server.CreateObject(“ADODB.Recordset”)
’打开Sheet
CreateExcelRecordset.Open “Select * From [”&sheet&“$]”, conn, 2, 2
End Function
6.SQL Server属于Server级的数据库,使用时要求比较严格,必须要输入用户名及密码才能使用。
Function CreateSQLServerRecordset(计算机名称,用户ID, 用户密码,数据库名称 数据表或查看表或Select指令 )
Dim Params, conn
Set CreatSQLServerConnection = Nothing
Set conn = Server.CreateObject (“ADODB.Connection”)
Params = “Provider=SQLOLEDB.1”
Params = Params & “;Data Source=” & Computer
Params = Params & “;User ID=” & UserID
Params = Params & “;Password=” & Password
Params = Params & “.Initial Catalog=”&数据库名称
Conn open Paras
Set CreateSQLServerRecordset = Server. CreateObject(“ADODB.Recordset")
CreateSQLServerRecordset.Open source, conn, 2, 2
End Function
ASP访问数据库大全
This page contains sample ADO connection strings for ODBC DSN / DSN-Less,
OLE DB Providers, Remote Data Services (RDS), MS Remote, MS DataShape.
Also included are ADO.NET connection strings for each .NET Managed Provider
(SQLClient, OLEDB, and ODBC).
These sample connection strings are compiled by Carl Prothman, a Microsoft Visual Basic MVP
Enjoy!
Table of Contents
ODBC DSN Connections DSN
File DSN
ODBC DSN-Less Connections ODBC Driver for AS/400
ODBC Driver for Access
ODBC Driver for dBASE
ODBC Driver for Excel
ODBC Driver for MySQL
ODBC Driver for Oracle
ODBC Driver for Paradox
ODBC Driver for SQL Server
ODBC Driver for Sybase
ODBC Driver for Sybase SQL Anywhere
ODBC Driver for Text
ODBC Driver for Teradata
ODBC Driver for Visual FoxPro
OLE DB Data Link Connections Data Link File (UDL)
OLE DB Data Provider Connections OLE DB Provider for AS/400
OLE DB Provider for Active Directory Service
OLE DB Provider for DB2
OLD DB Provider for Internet Publishing
OLE DB Provider for Index Server
OLE DB Provider for Microsoft Jet
OLE DB Provider for ODBC Databases
OLE DB Provider for Oracle (From Microsoft)
OLE DB Provider for Oracle (From Oracle)
OLE DB Provider for Simple Provider
OLE DB Provider for SQL Server
Remote Data Service (RDS) Connections RDS Data Control - Connect Property
RDS Data Control - URL Property
ADO URL Connections ADO Recordset
MS Remote Provider Connections MS Remote - Access (Jet)
MS Remote - SQL Server
Data Shape Provider Connections MS DataShape - SQL Server
.NET Managed Provider Connections SQL Client .NET Managed Provider (System.Data.SqlClient)
OLE DB .NET Managed Provider (System.Data.OleDb)
ODBC .NET Managed Provider (System.Data.ODBC)
1.ODBC DSN Connections
Using an ODBC DSN (Data Source Name) is a two step process.
1) You must first create the DSN via the "ODBC Data Source Administrator" program
found in your computer's Control Panel (or Administrative Tools menu in Windows 2000).
Make sure to create a SYSTEM DSN (not a USER DSN) when using ASP.
Note: You can also create the DSN via VB code.
2) Then use the following connection string - with your own DSN name of course. ;-)
****************
ODBC - DSN
oConn.Open "DSN=AdvWorks;" & _
"Uid=Admin;" & _
"Pwd=;
You can also create and use a File DSN. Then use the following ADO Connection string:
*******************
ODBC - File DSN
oConn.Open "FILEDSN=c:\somepath\mydb.dsn;" & _
"Uid=Admin;" & _
"Pwd=;"
For more information, see: About ODBC data sources and How to Use File DSNs and DSN-less Connections
Note: The problem with DSN is that Users can (and will) modify them (or delete by mistake),
then your program won't work so well... So it's better to use a DSN-Less or OLE DB Provider
connection string with a Trusted Connection if possible!
*************************
ODBC DSN-Less Connections
ODBC Driver for AS/400
oConn.Open "Driver={Client Access ODBC Driver (32-bit)};" & _
"System=myAS400;" & _
"Uid=myUsername;" & _
"Pwd=myPassword;"
For more information, see: A Fast Path to AS/400 Client/Server
*************************
ODBC Driver for Access
For Standard Security:
oConn.Open "Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=c:\somepath\mydb.mdb;" & _
"Uid=Admin;" & _
"Pwd=;"
If you are using a Workgroup (System database):
***************************
oConn.Open "Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=c:\somepath\mydb.mdb;" & _
"SystemDB=c:\somepath\mydb.mdw;", _
"admin", ""
If MDB is located on a network share:
*************************
oConn.Open "Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=\\myServer\myShare\myPath\myDb.mdb;"
For more information, see: Microsoft Access Driver Programming Considerations
***********************
ODBC Driver for dBASE
oConn.Open "Driver={Microsoft dBASE Driver (*.dbf)};" & _
"DriverID=277;" & _
"Dbq=c:\somepath;"
Note: Specify the filename in the SQL statement. For example:
oRs.Open "Select * From user.dbf", oConn, , ,adCmdText
Note: MDAC 2.1 (or greater) requires the Borland Database Engine (BDE) to update dBase DBF files. (Q238431).
For more information, see: dBASE Driver Programming Considerations
**********************
ODBC Driver for Excel
oConn.Open "Driver={Microsoft Excel Driver (*.xls)};" & _
"DriverId=790;" & _
"Dbq=c:\somepath\mySpreadsheet.xls;" & _
"DefaultDir=c:\somepath;"
For more information, see: Microsoft Excel Driver Programming Considerations
*********************
ODBC Driver for MySQL (via MyODBC)
To connect to a local database
oConn.Open "Driver={mySQL};" & _
"Server=MyServerName;" & _
"Option=16834;" & _
"Database=mydb;"
*********************
To connect to a remote database
oConn.Open "Driver={mySQL};" & _
"Server=db1.database.com;" & _
"Port=3306;" & _
"Option=131072;" & _
"Stmt=;" & _
"Database=mydb;" & _
"Uid=myUsername;" & _
"Pwd=myPassword;"
For more information, see: Programs Known to Work with MyODBC
**************************
ODBC Driver for Oracle
For the current Oracle ODBC Driver from Microsoft:
oConn.Open "Driver={Microsoft ODBC for Oracle};" & _
"Server=OracleServer.world;" & _
"Uid=myUsername;" & _
"Pwd=myPassword;"
**************************
For the older Oracle ODBC Driver from Microsoft:
oConn.Open "Driver={Microsoft ODBC Driver for Oracle};" & _
"ConnectString=OracleServer.world;" & _
"Uid=myUsername;" & _
"Pwd=myPassword;"
For more information, see: Connection String Format and Attributes
*************************
ODBC Driver for Paradox
oConn.Open "Driver={Microsoft Paradox Driver (*.db)};" & _
"DriverID=538;" & _
"Fil=Paradox 5.X;" & _
"DefaultDir=c:\dbpath\;" & _
"Dbq=c:\dbpath\;" & _
"CollatingSequence=ASCII;"
Note: MDAC 2.1 (or greater) requires the Borland Database Engine (BDE) to update Paradox ISAM fDBF files. (Q230126).
For more information, see: Paradox Driver Programming Considerations
**************************
ODBC Driver for SQL Server
For Standard Security:
oConn.Open "Driver={SQL Server};" & _
"Server=MyServerName;" & _
"Database=myDatabaseName;" & _
"Uid=myUsername;" & _
"Pwd=myPassword;"
**********************
For Trusted Connection security:
oConn.Open "Driver={SQL Server};" & _
"Server=MyServerName;" & _
"Database=myDatabaseName;" & _
"Uid=;" & _
"Pwd=;"
' or
oConn.Open "Driver={SQL Server};" & _
"Server=MyServerName;" & _
"Database=myDatabaseName;" & _
"Trusted_Connection=yes;"
*******************
To Prompt user for username and password
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -