ConnectionString、ConnectionTimeout 和 State 属性范例 (VB)
本范例演示使用 ConnectionString 属性打开 Connection 对象的不同方式。范例中还使用 ConnectionTimeout 属性来设置连接超时的时间段,并使用 State 属性来检查连接的状态。运行此过程需要 GetState 函数。
Public Sub ConnectionStringX()
Dim cnn1 As ADODB.Connection
Dim cnn2 As ADODB.Connection
Dim cnn3 As ADODB.Connection
Dim cnn4 As ADODB.Connection
' Open a connection without using a Data Source Name (DSN).
Set cnn1 = New ADODB.Connection
cnn1.C & _
"server=srv;uid=sa;pwd=pwd;database=Pubs"
cnn1.ConnectionTimeout = 30
cnn1.Open
' Open a connection using a DSN and ODBC tags.
Set cnn2 = New ADODB.Connection
cnn2.C
cnn2.Open
' Open a connection using a DSN and OLE DB tags.
Set cnn3 = New ADODB.Connection
cnn3.C
cnn3.Open
' Open a connection using a DSN and individual
' arguments instead of a connection string.
Set cnn4 = New ADODB.Connection
cnn4.Open "Pubs", "sa", "pwd"
' Display the state of the connections.
MsgBox "cnn1 state: " & GetState(cnn1.State) & vbCr & _
"cnn2 state: " & GetState(cnn2.State) & vbCr & _
"cnn3 state: " & GetState(cnn3.State) & vbCr & _
"cnn4 state: " & GetState(cnn4.State)
cnn4.Close
cnn3.Close
cnn2.Close
cnn1.Close
End Sub
Public Function GetState(intState As Integer) As String
Select Case intState
Case adStateClosed
GetState = "adStateClosed"
Case adStateOpen
GetState = "adStateOpen"
End Select
End Function
更多回帖