一样的连接语句,为什么一条能连上,另一条却不行?
都是放在同一个工程里,同一个窗体里面,却出现不同的情况。而且是早上还好好的,下午出现这种情况了,这是什么问题
这条能连上:
Set db = New ADODB.Connection
Set rs = New ADODB.Recordset
db.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=weryuk;Data Source=" & "192.168.10.2"
db.Open
用下面的却连接不上:
private sub main_load()
Dim IPstr As String, DSstr As String
IPstr = "192.168.10.2"
DSstr = "weryuk"
Dim flag As Boolean
flag = testconn(Trim(IPstr), Trim(DSstr))
If Not flag Then
MsgBox "数据库连接失败!", vbExclamation
Exit Sub
End If
end sub
Public Function testconn(IPstr As String, datastr As String) As Boolean
connstr = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=" & datastr & ";Data Source=" & IPstr '全局变量connstr 、conn
Set conn = New ADODB.Connection
On Error GoTo err:
conn.ConnectionTimeout = 1
conn.CommandTimeout = 0
conn.Open connstr
If conn.State = adStateOpen Then
testconn = True
Exit Function
End If
err:
testconn = False
End Function
[解决办法]
如果早上可以下午却不可以,那可能你有服务关闭了或者改动了,又或者是用于启动的服务账号刚好下午密码过期了,启动不了,你多从这方面去检查一下。
[解决办法]