天天看点

asp中的各种数据库连接方法

本文收集和整理了关于Asp中数据库的各种连接方法。

(1)SQL Server数据库的连接方法

<%

url=Request.ServerVariables("QUERY_STRING") 

if instr(url,";")>=1 then 

url=Replace(url,";",";") : Response.Redirect("?" & url) 

end if 

'屏蔽通过表单攻击 

for each item in request.form 

stritem=lcase(server.HTMLEncode(Request.form(item))) 

if instr(stritem,"select ")>=1 or instr(stritem,"insert ")>=1 or instr(stritem,"update ")>=1 or instr(stritem,"delete ")>=1 or instr(stritem,"exec ")>=1 or instr(stritem,"declare ")>=1 then 

response.write ("对不起,请不要输入非法字符!") 

response.end 

next

Set conn = Server.CreateObject("ADODB.Connection")

MM_conn_STRING = "Driver={sql server};server=127.0.0.1;database=userdatabase;uid=userid;pwd=password;"

conn.open MM_conn_STRING

%>

(2)dvbbs的数据库连接方法

<%@ LANGUAGE = VBScript CodePage = 936%>

Option Explicit

Response.Buffer = True

Dim Startime

Dim SqlNowString,Dvbbs,template,MyBoardOnline

Dim Conn,Plus_Conn,Db,MyDbPath

Const fversion="7.1.0 Sp1"

Const EnabledSession= True

Startime = Timer()

'系统采用XML版本设置

'最高版本为.4.0 依次为: Const MsxmlVersion=".3.0" Const MsxmlVersion=".2.6" 最低版本Const MsxmlVersion=""

Const MsxmlVersion=".3.0"

'可修改设置一:========================定义数据库类别,1为SQL数据库,0为Access数据库=============================

Const IsSqlDataBase = 0

MyDbPath = ""

'================================================================================================================

If IsSqlDataBase = 1 Then

'必修改设置二:========================SQL数据库设置=============================================================

'sql数据库连接参数:数据库名(SqlDatabaseName)、用户密码(SqlPassword)、用户名(SqlUsername)、

'连接名(SqlLocalName)(本地用local,外地用IP)

Const SqlDatabaseName = "dvbbs"

Const SqlPassword = "dvbbs"

Const SqlUsername = "dvbbs"

Const SqlLocalName = "(local)"

SqlNowString = "GetDate()"

Else

'必修改设置三:========================Access数据库设置==========================================================

'免费用户第一次使用请修改本处数据库地址并相应修改data目录中数据库名称,如:将dvbbs6.mdb修改为dvbbs6.asp

Db = "mydatabase.asp"

SqlNowString = "Now()"

End If

Const IsDeBug = 1

Set Dvbbs = New Cls_Forum

Set template = New cls_templates

Sub ConnectionDatabase

 Dim ConnStr

 If IsSqlDataBase = 1 Then

  ConnStr = "Provider = Sqloledb; User ID = " & SqlUsername & "; Password = " & SqlPassword & "; Initial Catalog = " & SqlDatabaseName & "; Data Source = " & SqlLocalName & ";"

 Else

  ConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath(MyDbPath & db)

 End If

 On Error Resume Next

 Set conn = Server.CreateObject("ADODB.Connection")

 conn.open ConnStr

 If Err Then

  err.Clear

  Set Conn = Nothing

  Response.Write "数据库连接出错,请检查连接字串。"'注释,需要把这几个字翻译成英文。

  Response.End

End Sub

'-----------------------------------------------------------------------------------------------------

'独立道具库连接设置

Sub Plus_ConnectionDatabase

  'sql数据库连接参数:数据库名、用户密码、用户名、连接名(本地用local,外地用IP)

  Dim SqlDatabaseName,SqlPassword,SqlUsername,SqlLocalName

  SqlDatabaseName = "dvbbs7"

  SqlPassword = "dvbbs"

  SqlUsername = "dvbbs"

  SqlLocalName = "(local)"

  Dim Db

  '免费用户第一次使用请修改本处数据库地址并相应修改data目录中数据库名称,如将dvbbs6.mdb修改为dvbbs6.asp

  Db = MyDbPath & "data/Dv_Plus_Tools.mdb"

  ConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath(db)

 Set Plus_Conn = Server.CreateObject("ADODB.Connection")

 Plus_Conn.open ConnStr

  Set Plus_Conn = Nothing

  Response.Write "插件数据库连接出错,请检查连接字串。"'注释,需要把这几个字翻译成英文。

(3)acess数据库连接方法

<% 

dim conn,mdbfile 

mdbfile=server.mappath("数据库名称.mdb") 

set conn=server.createobject("adodb.connection") 

conn.open "driver={microsoft access driver (*.mdb)};uid=admin;pwd=数据库密码;dbq="&mdbfile 

(4)ASP与SQL数据库连接:

dim conn 

set conn=server.createobject("ADODB.connection") 

con.open "PROVIDER=SQLOLEDB;DATA SOURCE=SQL服务器名称或IP地址;UID=sa;PWD=数据库密码;DATABASE=数据库名称 

建立记录集对象:

set rs=server.createobject("adodb.recordset") 

rs.open SQL语句,conn,3,2

(5)Access数据库的DSN-less连接方法:

set adocon=Server.Createobject("adodb.connection")

adoconn.Open"Driver={Microsoft Access Driver(*.mdb)};DBQ="& _

Server.MapPath("数据库所在路径")

(6)Access OLE DB连接方法:

adocon.open"Provider=Microsoft.Jet.OLEDB.4.0;"& _

"Data Source=" & Server.MapPath("数据库所在路径")

(7)SQL server OLE DB连接方法:

adocon.open"provider=SQLOLEDB.1;Data Source=RITANT4;"& _

"user ID=***;Password=***;"& _

"inital Catalog=数据库名"

(8)Oracle 连接方法:

adocon.open"Driver={microsoft odbc for oracle};server=oraclesever.world;uid=admin;pwd=pass;"

(9)Oracle OLE DB 连接方法:

adocon.open"Provider=OraOLEDB.Oracle;data source=dbname;user id=admin;password=pass;"

(10)dBase 连接方法:

adocon.open"Driver={microsoft dbase driver(*.dbf)};driverid=277;dbq=------------;"

(11)mySQL 连接方法:

adocon.open"Driver={mysql};database=yourdatabase;uid=username;pwd=yourpassword;option=16386;"

(12)Visual Foxpro 连接方法:

adocon.open"Driver={microsoft Visual Foxpro driver};sourcetype=DBC;sourceDB=*.dbc;Exclusive=No;"

(13)MS text 连接方法:

adocon.open"Driver={microsoft text driver(*.txt; *.csv)};dbq=-----;"&_

"extensions=asc,csv,tab,txt;Persist SecurityInfo=false;"

(14).MS text OLE DB 连接方法:

adocon.open"Provider=microsof.jet.oledb.4.0;data source=your_path;"&_

"Extended Properties'text;FMT=Delimited'"

 本文转自 simeon2005 51CTO博客,原文链接:http://blog.51cto.com/simeon/9667