asp 数据库连接问题
本帖最后由 jackai8493 于 2013-03-06 18:05:44 编辑 这两天弄到一个项目 是我们同学给我的 说能运行 让我放到我的服务器里试试
但是 总是出现 数据库连接错误
Microsoft OLE DB Provider for ODBC Drivers 错误 '80004005'
[Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序
D:\_DRAMA\NEWCHAMELEON\_HTML\../_Common/Lib/DBHelper.asp, 行 20
贴上数据库连接类
<%
Class clsDBHelper
Private DefaultConnString
Private DefaultConnection
private sub Class_Initialize()
'DefaultConnString =
DefaultConnString = Application("DBConnString")
Set DefaultConnection = Nothing
End Sub
Public Function ExecSPReturnRS(spName, params, connectionString)
If IsObject(connectionString) Then
If connectionString is Nothing Then
If DefaultConnection is Nothing Then
Set DefaultConnection = CreateObject("ADODB.Connection")
DefaultConnection.Open DefaultConnString
End If
Set connectionString = DefaultConnection
End If
End If
Set rs = CreateObject("ADODB.RecordSet")
Set cmd = CreateObject("ADODB.Command")
cmd.ActiveConnection = connectionString
cmd.CommandText = spName
cmd.CommandType = adCmdStoredProc
Set cmd = collectParams(cmd, params)
'cmd.Parameters.Refresh
rs.CursorLocation = adUseClient
rs.Open cmd, ,adOpenStatic, adLockReadOnly
For i = 0 To cmd.Parameters.Count - 1
If cmd.Parameters(i).Direction = adParamOutput OR cmd.Parameters(i).Direction = adParamInputOutput OR cmd.Parameters(i).Direction = adParamReturnValue Then
If IsObject(params) Then
If params is Nothing Then
Exit For
End If
Else
params(i)(4) = cmd.Parameters(i).Value
End If
End If
Next
Set cmd.ActiveConnection = Nothing
Set cmd = Nothing
Set rs.ActiveConnection = Nothing
Set ExecSPReturnRS = rs
End Function
Public Function ExecSQLReturnRS(strSQL, params, connectionString)
If IsObject(connectionString) Then
If connectionString is Nothing Then
If DefaultConnection is Nothing Then
Set DefaultConnection = CreateObject("ADODB.Connection")
DefaultConnection.Open DefaultConnString
End If
Set connectionString = DefaultConnection
End If
End If
Set rs = CreateObject("ADODB.RecordSet")
Set cmd = CreateObject("ADODB.Command")
cmd.ActiveConnection = connectionString
cmd.CommandText = strSQL
cmd.CommandType = adCmdText
Set cmd = collectParams(cmd, params)
rs.CursorLocation = adUseClient
rs.Open cmd, , adOpenStatic, adLockReadOnly
Set cmd.ActiveConnection = Nothing
Set cmd = Nothing
Set rs.ActiveConnection = Nothing
Set ExecSQLReturnRS = rs
End Function
Public Sub ExecSP(strSP,params,connectionString)
If IsObject(connectionString) Then
If connectionString is Nothing Then
If DefaultConnection is Nothing Then
Set DefaultConnection = CreateObject("ADODB.Connection")
DefaultConnection.Open DefaultConnString
End If
Set connectionString = DefaultConnection
End If
End If
Set cmd = CreateObject("ADODB.Command")
cmd.ActiveConnection = connectionString
cmd.CommandText = strSP
cmd.CommandType = adCmdStoredProc
Set cmd = collectParams(cmd, params)
cmd.Execute , , adExecuteNoRecords
For i = 0 To cmd.Parameters.Count - 1
If cmd.Parameters(i).Direction = adParamOutput OR cmd.Parameters(i).Direction = adParamInputOutput OR cmd.Parameters(i).Direction = adParamReturnValue Then
If IsObject(params) Then
If params is Nothing Then
Exit For
End If
Else
params(i)(4) = cmd.Parameters(i).Value
End If
End If
Next
Set cmd.ActiveConnection = Nothing
Set cmd = Nothing
End Sub
Public Sub ExecSQL(strSQL,params,connectionString)
If IsObject(connectionString) Then
If connectionString is Nothing Then
If DefaultConnection is Nothing Then
Set DefaultConnection = CreateObject("ADODB.Connection")
DefaultConnection.Open DefaultConnString
End If
Set connectionString = DefaultConnection
End If
End If
Set cmd = CreateObject("ADODB.Command")
cmd.ActiveConnection = connectionString
cmd.CommandText = strSQL
cmd.CommandType = adCmdText
Set cmd = collectParams(cmd, params)
cmd.Execute , , adExecuteNoRecords
Set cmd.ActiveConnection = Nothing
Set cmd = Nothing
End Sub
Public Function BeginTrans(connectionString)
If IsObject(connectionString) Then
If connectionString is Nothing Then
connectionString = DefaultConnString
End If
End If
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open connectionString
conn.BeginTrans
Set BeginTrans = conn
End Function
Public Sub CommitTrans(connectionObj)
If Not connectionObj Is Nothing Then
connectionObj.CommitTrans
connectionObj.Close
Set ConnectionObj = Nothing
End If
End Sub
Public Sub RollbackTrans(connectionObj)
If Not connectionObj Is Nothing Then
connectionObj.RollbackTrans
connectionObj.Close
Set ConnectionObj = Nothing
End If
End Sub
Public Function MakeParam(PName,PType,PDirection,PSize,PValue)
MakeParam = Array(PName, PType, PDirection, PSize, PValue)
End Function
Public Function GetValue(params, paramName)
For Each param in params
If param(0) = paramName Then
GetValue = param(4)
Exit Function
End If
Next
End Function
Public Sub Dispose
if (Not DefaultConnection is Nothing) Then
if (DefaultConnection.State = adStateOpen) Then DefaultConnection.Close
Set DefaultConnection = Nothing
End if
End Sub
Private Function collectParams(cmd,argparams)
If VarType(argparams) = 8192 or VarType(argparams) = 8204 or VarType(argparams) = 8209 then
params = argparams
For i = LBound(params) To UBound(params)
l = LBound(params(i))
u = UBound(params(i))
' Check for nulls.
If u - l = 4 Then
If VarType(params(i)(4)) = vbString Then
If params(i)(4) = "" Then
v = Null
Else
v = params(i)(4)
End If
Else
v = params(i)(4)
End If
cmd.Parameters.Append cmd.CreateParameter(params(i)(0), params(i)(1), params(i)(2), params(i)(3), v)
End If
Next
Set collectParams = cmd
Exit Function
Else
Set collectParams = cmd
End If
End Function
End Class
%>
<%
JOBSITE = Session("JOBSITE")
Response.Expires = 0
with response
.expires=-1
.addheader "pragma","no-cache"
.addheader "cache-control","no-cache"
end with
%>
private sub Class_Initialize()
'DefaultConnString =
DefaultConnString = Application("DBConnString") &""
if trim(DefaultConnString)="" then
set fso=server.CreateObject("scripting.filesystemobject")
set ts=fso.OpenTextFile(server.MapPath("DBConnString_NE7WCH45AME8LE3ON.dat"))'注意修改这里文件路径,如果文件不是放在根目录下,如server.MapPath("/data/DBConnString_NE7WCH45AME8LE3ON.dat")
DefaultConnString= ts.readall
ts.close
set ts=nothing
set fso=nothing
Application("DBConnString")=DefaultConnString
end if
Set DefaultConnection = Nothing
End Sub