本帖最后由 yu2n 于 2014-8-11 17:55 编辑
回复 3# CrLf
如果要容错做的比较好的话,可考虑RS记录集与ADO数据库连接的验证:- If (Not objRS Is Nothing) Then If (Not objRS.State = 1) Then ''Do Something
- If (Not objADOConn Is Nothing) Then If (Not objADOConn.State = 1) Then ''Do Something
复制代码 再贴一个正在用的,自行修改Conn连接、SQL语句、:- Function GET_Password(ByVal strPCName)
- On Error Resume Next
- Dim strConn, strSQL, objADOConn, objRS, str
- strConn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=DBUSER1;Password=Abcd1234;Initial Catalog=ADMIN_DB;Data Source=SQL01.ABC.COM"
- strSQL = "SELECT TOP 1 [pwd1] FROM [password] WHERE [name]='" & strPCName & "'"
- Set objRS = CreateObject("ADODB.RecordSet")
- Set objADOConn = CreateObject("ADODB.Connection")
- objADOConn.ConnectionString = strConn
- objADOConn.Open
- If (Not objADOConn Is Nothing) Then If (Not objADOConn.State = 1) Then Exit Function
- objRS.Open strSQL, objADOConn, 3, 3
- If (Not objRS Is Nothing) Then If Not objRS.State = 1 Then objADOConn.Close : Exit Function
- If objRS.Recordcount = 1 Then str = objRS("pwd")
- If (Not objRS Is Nothing) Then If (Not objRS.State = 1) Then objRS.Close
- If (Not objADOConn Is Nothing) Then If (Not objADOConn.State = 1) Then objADOConn.Close
- Set objRS = Nothing
- Set objADOConn = Nothing
- GET_Password = str
- End Function
复制代码
|