vb测试连接数据库
来源:百度知道 编辑:UC知道 时间:2024/05/20 06:46:14
Option Explicit
Dim conn As ADODB.Connection
Private Sub Command1_Click()
On Error Resume Next
conn.Open
MsgBox conn.State
End Sub
Private Sub Form_Load()
Set conn = New ADODB.Connection
conn.CursorLocation = adUseClient
conn.ConnectionString = ""
End Sub
如果conn.state 返回值为0,数据库连接失败,为1连接成功
连接数据库你知道了,就看检验连接状态State是否关闭或者开启就可以,不清楚连接的什么数据,你改改下面代码:
长时间未响应,你要根据你的服务器情况设置连接对象的ConnectionTimeout 属性。
Private Sub ComOK_Click()
Dim SQLstr As String,cnstr AS String
Dim cn AS New ADODB.Connection'连接对象
Dim rs As New ADODB.Recordset'记录集对象
On Error GoTo Show_ErrMsg'错误捕足
cnstr="Provider=SQLOLEDB;Driver={SQLServer};Server=服务器名;Uid=用户名;Pwd=密码;Database=数据库名"'连接字符串
cn.ConnectionTimeout=20'设置超时时间
cn.opencnstr'打开数据库连接
if cn.State=AdStateOpenthen
msgbox "数据库连接成功!!!"
e