编程william hill官网
直播中

电子工程师

21年用户 16604经验值
擅长:可编程逻辑 模拟技术 测量仪表 控制/MCU RF/无线 MEMS/传感技术
私信 关注

ConnectionString、ConnectionTimeout 和 State 属性范例 (VB)

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

更多回帖

发帖
×
20
完善资料,
赚取积分