Public SQLCN As New ADODB.ConnectionPublic Function InitSQLConnect() As Boolean'初始化SQL连接
On Error GoTo ErrHandle
InitSQLConnect = True
SQLCN.Provider = "SQLOLEDB"
SQLCN.Properties("Data Source") = Server '(或IP) SQLCN.Properties("Initial Catalog") = DataBase
If SSPI = 0 Then '验证模式,0为混合模式,1为NT模式 SQLCN.Properties("User ID") = UserName SQLCN.Properties("Password") = PassWord Else SQLCN.Properties("Integrated Security") = "SSPI" End If
非常感谢!
On Error GoTo ErrHandle
InitSQLConnect = True
SQLCN.Provider = "SQLOLEDB"
SQLCN.Properties("Data Source") = Server '(或IP)
SQLCN.Properties("Initial Catalog") = DataBase
If SSPI = 0 Then '验证模式,0为混合模式,1为NT模式
SQLCN.Properties("User ID") = UserName
SQLCN.Properties("Password") = PassWord
Else
SQLCN.Properties("Integrated Security") = "SSPI"
End If
SQLCN.Open
Exit Function
ErrHandle:
InitSQLConnect = False
MsgBox Err.Description, vbCritical, "连接SQL Server失败"End Function在互联网跑数据,建议使用VPN安全,数据库连接方法还是一样
If SSPI = 0 Then '验证模式,0为混合模式,1为NT模式
SQLCN.Properties("User ID") = UserName
SQLCN.Properties("Password") = PassWord
Else
SQLCN.Properties("Integrated Security") = "SSPI"
End If
二者选一即可