ADOでSQLServerへ接続 (Native Client使用)

※ODBC設定必要なし。Microsoft ActiveX DataAccess Objectsの参照設定が必要
※NativeClientはバージョン指定があるので、クライアントのインストールに合わせて記述の変更が必要(もしくは指定バージョンのNativeClientをクライアントにインストール)

Function test()
    
    Dim cnn     As ADODB.Connection
    Dim cmd     As ADODB.Command
    Dim rst1    As ADODB.Recordset
    Dim rst2    As ADODB.Recordset
    Dim tmp     As Variant

    Set cnn = New ADODB.Connection
    Set cmd = New ADODB.Command
    
    With cnn
        .Provider = "SQLNCLI11"
        .Properties("Data Source").Value = "DB01\ISADB"
        .Properties("Initial Catalog").Value = "UsersAdmin"
        .Properties("Integrated Security").Value = "SSPI"
        .Properties("DataTypeCompatibility").Value = "80"
        .Properties("MARS Connection").Value = "True"
        .Open
    End With
    
    With cmd
        .ActiveConnection = cnn
        
        '1回目の処理
        .CommandText = "SELECT * FROM dbo.USERS;"
        Set rst1 = .Execute
        If Not rst1.EOF Then
            '文字列データ格納 (全データをカンマ区切りで)
            tmp = rst1.GetString(adClipString, , ",", vbNewLine)
            Debug.Print tmp
        End If
        
   End With

0 件のコメント :

コメントを投稿