蓝派网(www.lan27.com)-精选网络资源,分享和交流! 文章首页站内搜索在线手册广告代码酷站欣赏万年历
您现在的位置: 蓝派网 >> 文章中心 >> 网络编程 >> ASP >> 正文

ASP连接Access或SQLServer数据库的终极代码

作者:佚名    文章来源:网络    更新时间:2008-10-29 19:14:53

<%
On Error Resume Next

Dim Data_base
Data_base = 0 '1为MSSQLServer2000 其它为Access

Set Conn=Server.CreateObject("Adodb.Connection")

IF Data_base=1 Then
'-------------------------------------------------------------------------------------
Dim dbconn,dbip,dbdns,dbname,dbuid,dbpwd
dbconn = 1       '链接方式 1为OLEDB链接 2为驱动链接 3为DNS链接
dbip = "(local)"     '数据库所在的IP地址,如果是本地数据库则为:"(local)"
dbdsn = "数据源DSN名称"   '数据库DSN名
dbname = "MSOW"     '数据库名称
dbuid = "sa"      '数据库用户名
dbpwd = "********"    '数据库密码
'-------------------------------------------------------------------------------------
Select CASE dbconn
  Case 1
   Conn.Open "Provider=SQLOLEDB;User ;Password="&dbpwd&";Initial Catalog="&dbname&";Data Source="&dbip&";"
  Case 2
   Conn.open "DRIVER={SQL Server};SERVER="&dbip&";UID="&dbuid&";PWD="&dbpwd&";DATABASE="&dbname&""
  Case 3
   Conn.open "DSN="&dbdns&";UID="&dbuid&";PWD="&dbpwd&";DATABASE="&dbname&""
End Select
Else
'-------------------------------------------------------------------------------------
Dim DB_Name,DB_Pwd
DB_Name = "#MSOW#Data.mdb"    '数据库名字
DB_Pwd = ""       '数据库密码,如无密码可不填写,代码自动判断选择语句。
'-------------------------------------------------------------------------------------
IF DB_Pwd="" Then
  Conn.Open "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = "&Server.MapPath(DB_Name)
Else
  Conn.Open "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = "&Server.MapPath(DB_Name)&";Jet OLEDB:Database Password="&DB_Pwd&";"
End IF
End IF

IF Err Then
Err.Clear
Conn.Close
Set Conn = Nothing
Response.Write "<Center><font color=#FF6600><strong>数据库连接出错,请检查连接设置...</strong></font></Center>"
Response.End
End IF

'只读数据
Function GetRSForRead(StrSQL)
Set RS = Server.CreateObject("Adodb.RecordSet")
RS.Source = StrSQL
RS.Open RS.Source,Conn,1,1
Set GetRSForRead = RS
End Function
'只读调用 Set RS = GetRSForRead("Select * From 数据表")

'可写数据
Function GetRSForWrite(StrSQL)
Set RS = Server.CreateObject("Adodb.RecordSet")
RS.Source = StrSQL
RS.Open RS.Source,Conn,3,3
Set GetRSForWrite = RS
End Function
'可写调用 Set RS = GetRSForWrite("Select * From 数据表")
%>

发表评论】【打印此文】【关闭窗口】【点击数:
★好玩的休闲小游戏★