大家帮我看看好不,要毕业的,着急!!谢谢
Option Explicit
Public DbFinance As New ADODB.Connection '设置ADODB连接对象的公共变量
Public Function DbConnection() As Boolean '打开数据库
Dim e As ADODB.Error
Dim strUserid, strPassword, strInitialCatalog, strDataSource As String
Dim fso As New FileSystemObject
Dim ts '用于取得登录服务器的信息
On Error GoTo errorhandle '出现任何错误将执行errorhandle进行错误处理
DbConnection = True 'ConnectDB为真时表示打开数据库无误
DbFinance.Errors.Clear
DbFinance.CommandTimeout = 30 '执行SQL命令的缺省等待时间
DbFinance.ConnectionTimeout = 30 '连接数据库的缺省时间
Set ts = fso.OpenTextFile(App.Path & "\zjhConnectionDataBase.ini", ForReading, True, TristateFalse)
strUserid = ts.ReadLine '从ini文件中得到连接SqlServer的用户名
strPassword = ts.ReadLine '得到密码
strInitialCatalog = ts.ReadLine '得到数据库名
strDataSource = ts.ReadLine '得到服务器名
ts.Close
Set ts = Nothing
Set fso = Nothing
strUserid = Trim(Mid(strUserid, 8)) '从ini文件中得到连接SqlServer的用户名
strPassword = Trim(Mid(strPassword, 10)) '得到密码
strInitialCatalog = Trim(Mid(strInitialCatalog, 14)) '得到数据库名
strDataSource = Trim(Mid(strDataSource, 12)) '得到服务器名
If strUserid = "" And strPassword = "" And strInitialCatalog = "" And strDataSource = "" Then
MsgBox "请确认数据库资源连接文件正确!", vbOKOnly, "数据库连接错误-"
Set DbFinance = Nothing
Exit Function
End If
If strPassword = "" Then 'sqlserver无密码
DbFinance.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False" _
& ";User ID=" & strUserid _
& ";Initial Catalog=" & strInitialCatalog _
& ";Data Source=" & strDataSource
Else 'sqlserver有密码
DbFinance.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False" _
& ";User ID=" & strUserid _
& ";password=" & strPassword _
& ";Initial Catalog=" & strInitialCatalog _
& ";Data Source=" & strDataSource
End If
DbFinance.Open '打开数据库
Exit Function
errorhandle:
If DbFinance.Errors.Count < 1 Then
MsgBox Err.Description, vbOKOnly + vbExclamation '显示错误描述
Else
For Each e In DbFinance.Errors
MsgBox e.Description '显示错误描述
Next e
End If
DbConnection = False 'DbConnection为假(False)时表示打开数据库有误
Set DbFinance = Nothing '关闭错误的数据库
End Function
Public Sub DbClose() '关闭数据库
On Error Resume Next
DbFinance.Close
Set DbFinance = Nothing
End Sub
Public Function resultcount(rs As ADODB.Recordset) As Long '返回结果集中的记录数
On Error Resume Next
resultcount = 0
Do While rs.EOF = False
resultcount = resultcount + 1
rs.MoveNext
Loop
rs.MoveFirst
End Function
提示是
DbFinance As New ADODB.Connection 出错