単純なコマンドは、パラメーター化されておらず、永続化を必要としないコマンドです。 簡単なコマンドを作成して実行するには、3 つの方法があります。
Command のオブジェクトの使用
接続 オブジェクトの使用
Recordset オブジェクトの使用
Command オブジェクトの使用
Command オブジェクトを使用して単純なコマンドを作成するには、Command オブジェクトの CommandText プロパティに命令を割り当て、CommandType プロパティに適切な値を設定する必要があります。 コマンドを実行するには、Command オブジェクトの ActiveConnection プロパティに開いている接続を割り当て、その後、Command オブジェクトの Execute メソッドを呼び出す必要があります。
次のコード スニペットは、Command オブジェクトを使用してデータ ソースに対してコマンドを実行する基本的な方法を示しています。 次の使用例は、行を返すコマンドを使用し、コマンド実行の結果を recordset オブジェクトとして 返します。
'BeginBasicCmd
On Error GoTo ErrHandler:
Dim objConn As New ADODB.Connection
Dim objCmd As New ADODB.Command
Dim objRs As New ADODB.Recordset
objCmd.CommandText = "SELECT OrderID, OrderDate, " & _
"RequiredDate, ShippedDate " & _
"FROM Orders " & _
"WHERE CustomerID = 'ALFKI' " & _
"ORDER BY OrderID"
objCmd.CommandType = adCmdText
' Connect to the data source.
Set objConn = GetNewConnection
objCmd.ActiveConnection = objConn
' Execute once and display...
Set objRs = objCmd.Execute
Debug.Print "ALFKI"
Do While Not objRs.EOF
Debug.Print vbTab & objRs(0) & vbTab & objRs(1) & vbTab & _
objRs(2) & vbTab & objRs(3)
objRs.MoveNext
Loop
'clean up
objRs.Close
objConn.Close
Set objRs = Nothing
Set objConn = Nothing
Set objCmd = Nothing
Exit Sub
ErrHandler:
'clean up
If objRs.State = adStateOpen Then
objRs.Close
End If
If objConn.State = adStateOpen Then
objConn.Close
End If
Set objRs = Nothing
Set objConn = Nothing
Set objCmd = Nothing
If Err <> 0 Then
MsgBox Err.Source & "-->" & Err.Description, , "Error"
End If
'EndBasicCmd
'BeginNewConnection
Private Function GetNewConnection() As ADODB.Connection
Dim oCn As New ADODB.Connection
Dim sCnStr As String
sCnStr = "Provider='SQLOLEDB';Data Source='MySqlServer';" & _
"Integrated Security='SSPI';Initial Catalog='Northwind';"
oCn.Open sCnStr
If oCn.State = adStateOpen Then
Set GetNewConnection = oCn
End If
End Function
'EndNewConnection
Recordset オブジェクトの使用
また、コマンドをテキスト文字列として作成し、Recordset オブジェクトの Open メソッドに渡し、コマンドの種類 (adCmdText) と共に実行することもできます。 次のコード スニペットは、これを示しています。
Const DS = "MySqlServer"
Const DB = "Northwind"
Const DP = "SQLOLEDB"
Dim objRs As New ADODB.Recordset
Dim CommandText As String
Dim ConnectionString As String
CommandText = "SELECT OrderID, OrderDate, " & _
"RequiredDate, ShippedDate " & _
"FROM Orders " & _
"WHERE CustomerID = 'ALFKI' " & _
"ORDER BY OrderID"
ConnectionString = "Provider=" & DP & _
";Data Source=" & DS & _
";Initial Catalog=" & DB & _
";Integrated Security=SSPI;"
' Connect to data source and execute the SQL command.
objRs.Open CommandText, ConnectionString, _
adOpenStatic, adLockReadOnly, adCmdText
Debug.Print "ALFKI"
Do While Not objRs.EOF
Debug.Print vbTab & objRs(0) & vbTab & objRs(1) & vbTab & _
objRs(2) & vbTab & objRs(3)
objRs.MoveNext
Loop
'Clean up.
objRs.Close
Set objRs = Nothing
Connection オブジェクトの使用
開いている Connection オブジェクトでコマンドを実行することもできます。 前のコード例は次のようになります。
Const DS = "MySqlServer"
Const DB = "Northwind"
Const DP = "SQLOLEDB"
Dim objConn As New ADODB.Connection
Dim objRs As New ADODB.Recordset
CommandText = "SELECT OrderID, OrderDate, " & _
"RequiredDate, ShippedDate " & _
"FROM Orders " & _
"WHERE CustomerID = 'ALFKI' " & _
"ORDER BY OrderID"
ConnectionString = "Provider=" & DP & _
";Data Source=" & DS & _
";Initial Catalog=" & DB & _
";Integrated Security=SSPI;"
' Connect to the data source.
objConn.Open ConnectionString
' Execute command through the connection and display...
Set objRs = objConn.Execute(CommandText)
Debug.Print "ALFKI"
Do While Not objRs.EOF
Debug.Print vbTab & objRs(0) & vbTab & objRs(1) & vbTab & _
objRs(2) & vbTab & objRs(3)
objRs.MoveNext
Loop
'Clean up.
objRs.Close
objConn.Close
Set objRs = Nothing
Set objConn = Nothing