日期:2010-03-26 浏览次数:20980 次
<%
'==========================================================================
'文件名称:clsDbCtrl.asp
'功 能:数据库操作类
'作 者:coldstone (coldstone[在]qq.com)
'程序版本:v1.0.5
'完成时间:2005.09.23
'修改时间:2007.10.30
'版权声明:可以在任意作品中使用本程序代码,但请保留此版权信息。
' 如果你修改了程序中的代码并得到更好的应用,请发送一份给我,谢谢。
'==========================================================================
Dim a : a = CreatConn(0, "master", "localhost", "sa", "") 'MSSQL数据库
'Dim a : a = CreatConn(1, "Data/%TestDB%.mdb", "", "", "") 'Access数据库
'Dim a : a = CreatConn(1, "E:\MyWeb\Data\%TestDB%.mdb", "", "", "mdbpassword")
Dim Conn
'OpenConn() '在加载时就建立的默认连接对象Conn,默认使用数据库a
Sub OpenConn : Set Conn = Oc(a) : End Sub
Sub CloseConn : Co(Conn) : End Sub
Function Oc(ByVal Connstr)
On Error Resume Next
Dim objConn
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open Connstr
If Err.number <> 0 Then
Response.Write("<div id=""DBError"">数据库服务器端连接错误,请与网站管理员联系。</div>")
'Response.Write("错误信息:" & Err.Description)
objConn.Close
Set objConn = Nothing
Response.End
End If
Set Oc = objConn
End Function
Sub Co(obj)
On Error Resume Next
Set obj = Nothing
End Sub
Function CreatConn(ByVal dbType, ByVal strDB, ByVal strServer, ByVal strUid, ByVal strPwd)
Dim TempStr
Select Case dbType
Case "0","MSSQL"
TempStr = "driver={sql server};server="&strServer&";uid="&strUid&";pwd="&strPwd&";database="&strDB
Case "1","ACCESS"
Dim tDb : If Instr(strDB,":")>0 Then : tDb = strDB : Else : tDb = Server.MapPath(strDB) : End If
TempStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&tDb&";Jet OLEDB:Database Password="&strPwd&";"
Case "3","MYSQL"
TempStr = "Driver={mySQL};Server="&strServer&";Port=3306;Option=131072;Stmt=; Database="&strDB&";Uid="&strUid&";Pwd="&strPwd&";"
Case "4","ORACLE"
TempStr = "Driver={Microsoft ODBC for Oracle};Server="&strServer&";Uid="&strUid&";Pwd="&strPwd&";"
End Select
CreatConn = TempStr
End Function
Class dbCtrl
Private debug
Private idbConn
Private idbErr
Private Sub Class_Initialize()
debug = true '调试模式是否开启