次の方法で共有


コマンドの実行

.NET Framework に含まれる各 .NET Framework データ プロバイダーには、 DbCommandから継承する独自のコマンド オブジェクトがあります。 .NET Framework Data Provider for OLE DB には OleDbCommand オブジェクトが含まれています。.NET Framework Data Provider for SQL Server には SqlCommand オブジェクトが含まれており、.NET Framework Data Provider for ODBC には OdbcCommand オブジェクトが含まれており、.NET Framework Data Provider for Oracle には OracleCommand オブジェクトが含まれています。 これらの各オブジェクトは、次の表に示すように、コマンドの種類と必要な戻り値に基づいてコマンドを実行するためのメソッドを公開します。

コマンド 戻り値
ExecuteReader DataReader オブジェクトを返します。
ExecuteScalar 単一のスカラー値を返します。
ExecuteNonQuery 行を一切返さないコマンドを実行します。
ExecuteXMLReader XmlReader を返します。 SqlCommand オブジェクトでのみ使用できます。

厳密に型指定された各コマンド オブジェクトは、次の表に示す CommandType 列挙値をサポートしています。これらの列挙値を使用してコマンド文字列の解釈方法を指定できます。

コマンドタイプ 説明
Text データ ソースで実行するステートメントを定義する SQL コマンド。
StoredProcedure ストアド プロシージャの名前。 呼び出す Parameters メソッドに関係なく、コマンドの Execute プロパティを使用して入力パラメーターと出力パラメーターにアクセスし、戻り値を取得できます。 ExecuteReader を使用した場合は、DataReader を閉じるまで戻り値および出力パラメーターにアクセスすることはできません。
TableDirect テーブルの名前。

SqlCommand オブジェクトを作成し、そのプロパティを設定することによってストアド プロシージャを実行するコード サンプルを次に示します。 ストアド プロシージャへの入力パラメーターは、SqlParameter オブジェクトを使って指定します。 このコマンドを ExecuteReader メソッドで実行し、SqlDataReader からの出力をコンソール ウィンドウに表示します。

static void GetSalesByCategory(string connectionString,
    string categoryName)
{
    using (SqlConnection connection = new(connectionString))
    {
        // Create the command and set its properties.
        SqlCommand command = new()
        {
            Connection = connection,
            CommandText = "SalesByCategory",
            CommandType = CommandType.StoredProcedure
        };

        // Add the input parameter and set its properties.
        SqlParameter parameter = new()
        {
            ParameterName = "@CategoryName",
            SqlDbType = SqlDbType.NVarChar,
            Direction = ParameterDirection.Input,
            Value = categoryName
        };

        // Add the parameter to the Parameters collection.
        command.Parameters.Add(parameter);

        // Open the connection and execute the reader.
        connection.Open();
        using (SqlDataReader reader = command.ExecuteReader())
        {
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    Console.WriteLine($"{reader[0]}: {reader[1]:C}");
                }
            }
            else
            {
                Console.WriteLine("No rows found.");
            }
            reader.Close();
        }
    }
}
Shared Sub GetSalesByCategory(ByVal connectionString As String, _
    ByVal categoryName As String)

    Using connection As New SqlConnection(connectionString)

        ' Create the command and set its properties.
        Dim command As SqlCommand = New SqlCommand()
        command.Connection = connection
        command.CommandText = "SalesByCategory"
        command.CommandType = CommandType.StoredProcedure

        ' Add the input parameter and set its properties.
        Dim parameter As New SqlParameter()
        parameter.ParameterName = "@CategoryName"
        parameter.SqlDbType = SqlDbType.NVarChar
        parameter.Direction = ParameterDirection.Input
        parameter.Value = categoryName

        ' Add the parameter to the Parameters collection.
        command.Parameters.Add(parameter)

        ' Open the connection and execute the reader.
        connection.Open()
        Using reader As SqlDataReader = command.ExecuteReader()

            If reader.HasRows Then
                Do While reader.Read()
                    Console.WriteLine("{0}: {1:C}", _
                      reader(0), reader(1))
                Loop
            Else
                Console.WriteLine("No rows returned.")
            End If
        End Using
    End Using
End Sub

トラブルシューティングコマンド

.NET Framework Data Provider for SQL Server では、失敗したコマンド実行に関連する断続的な問題を検出できるように、パフォーマンス カウンターが追加されます。 詳細については、「 パフォーマンス カウンター」を参照してください。

こちらも参照ください