今天做一个恢复数据库的程序时,突然想起 SqlServer 有一个功能强大的组件 sqldmo ,与是查阅了一下它的相关帮助,发现它刚好能够满足我的需求:
下面是我的程序的部代码,在这里共享组大家分享。
以下代码在 VB6中文版中编译通过!
启动数据服务:
Public Sub Start_server()
Dim svr As New SQLDMO.sqlServer
On Error GoTo errHwnd
svr.Start False, MyServer
svr.Close
Exit Sub
errHwnd:
If Err.Number <> -2147023840 Then
MsgBox Err.Number & vbCrLf & Err.Description, vbExclamation, "错误"
End If
End Sub
联接并验证密码:
Public Function ConnServer(ByVal sqlServer As String, ByVal uid As String, ByVal pwd As String) As Boolean
Dim st As SQLDMO_SVCSTATUS_TYPE
Dim svr As New SQLDMO.sqlServer
On Error GoTo errHwnd
svr.LoginTimeout = 10
svr.Connect sqlServer, uid, pwd
ConnServer = True
Exit Function
errHwnd:
If Err.Number = -2147203048 Then
MsgBox "登录失败密码不正确,请重新输入!", vbInformation, "提示"
Else
Resume Next