3、VB连接数据库可不可以写段代码测试,如果没有连接成功则给出提示?
//参考下面
Option Explicit
Public Rs As New ADODB.Recordset
Public Conn As New ADODB.Connection
Private Sub Command1_Click()
If Make_Conn = True Then
MsgBox "连接成功"
Else
MsgBox "连接失败"
End If
End Sub
Public Function Make_Conn() As Double
Dim strConn As String
On Error GoTo err
strConn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Northwind;Data Source=192.168.0.15"
Conn.CursorLocation = adUseClient
Conn.CommandTimeout = 10
Conn.Open strConn
Make_Conn = True
Exit Function
err:
Make_Conn = False
End Function