ADOでSQLServerへ接続 (OLEDB使用)

※ODBC設定必要なし。Microsoft ActiveX DataAccess Objectsの参照設定が必要

  1. Function test2()
  2.  
  3. Dim cnn As ADODB.Connection
  4. Dim cmd As ADODB.Command
  5. Dim rst 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 = "SQLOLEDB"
  12. .Properties("Data Source").Value = "DB01\ISADB"
  13. .Properties("Initial Catalog").Value = "UsersAdmin"
  14. .Properties("Integrated Security").Value = "SSPI"
  15. .Open
  16. End With
  17. With cmd
  18. .ActiveConnection = cnn
  19. .CommandText = "SELECT * FROM dbo.Users;"
  20. Set rst = .Execute
  21. If Not rst.EOF Then
  22. '文字列データ格納 (全データをカンマ区切りで)
  23. tmp = rst.GetString(adClipString, , ",", vbNewLine)
  24. Debug.Print tmp
  25. End If
  26. End With
  27. '終了処理
  28. Set rst = Nothing: Set cmd = Nothing
  29. cnn.Close: Set cnn = Nothing
  30.  
  31. End Function

0 件のコメント :

コメントを投稿