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

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

  1. Function test()
  2. Dim cnn As ADODB.Connection
  3. Dim cmd As ADODB.Command
  4. Dim rst1 As ADODB.Recordset
  5. Dim rst2 As ADODB.Recordset
  6. Dim tmp As Variant
  7.  
  8. Set cnn = New ADODB.Connection
  9. Set cmd = New ADODB.Command
  10. With cnn
  11. .Provider = "SQLNCLI11"
  12. .Properties("Data Source").Value = "DB01\ISADB"
  13. .Properties("Initial Catalog").Value = "UsersAdmin"
  14. .Properties("Integrated Security").Value = "SSPI"
  15. .Properties("DataTypeCompatibility").Value = "80"
  16. .Properties("MARS Connection").Value = "True"
  17. .Open
  18. End With
  19. With cmd
  20. .ActiveConnection = cnn
  21. '1回目の処理
  22. .CommandText = "SELECT * FROM dbo.USERS;"
  23. Set rst1 = .Execute
  24. If Not rst1.EOF Then
  25. '文字列データ格納 (全データをカンマ区切りで)
  26. tmp = rst1.GetString(adClipString, , ",", vbNewLine)
  27. Debug.Print tmp
  28. End If
  29. End With

0 件のコメント :

コメントを投稿