标题:VB与SQL连接,朋友给的系统,她能打开,我却出错
取消只看楼主
butupaopao
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-6-23
 问题点数:0 回复次数:1 
VB与SQL连接,朋友给的系统,她能打开,我却出错

大家帮我看看好不,要毕业的,着急!!谢谢

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 出错

搜索更多相关主题的帖子: SQL 系统 朋友 
2007-06-23 10:05
butupaopao
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-6-23
得分:0 
帮帮忙啊,没人理我啊

[此贴子已经被作者于2007-6-23 10:31:31编辑过]

2007-06-23 10:12



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-149657-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.063449 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved