SqlCommand コンストラクター

定義

SqlCommand クラスの新しいインスタンスを初期化します。

オーバーロード

名前 説明
SqlCommand()

SqlCommand クラスの新しいインスタンスを初期化します。

SqlCommand(String)

クエリのテキストを使用して、 SqlCommand クラスの新しいインスタンスを初期化します。

SqlCommand(String, SqlConnection)

クエリのテキストとSqlCommandを使用して、SqlConnection クラスの新しいインスタンスを初期化します。

SqlCommand(String, SqlConnection, SqlTransaction)

クエリ、SqlCommand、およびSqlConnectionのテキストを使用して、SqlTransaction クラスの新しいインスタンスを初期化します。

SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)

指定したコマンド テキスト、接続、トランザクション、および暗号化設定を使用して、 SqlCommand クラスの新しいインスタンスを初期化します。

SqlCommand()

ソース:
System.Data.SqlClient.notsupported.cs

SqlCommand クラスの新しいインスタンスを初期化します。

public:
 SqlCommand();
public SqlCommand();
Public Sub New ()

次の例では、 SqlCommand を作成し、 CommandTimeout プロパティを設定します。

public void CreateSqlCommand()
{
   SqlCommand command = new SqlCommand();
   command.CommandTimeout = 15;
   command.CommandType = CommandType.Text;
}
Public Sub CreateSqlCommand()
    Dim command As New SqlCommand()
    command.CommandTimeout = 15
    command.CommandType = CommandType.Text
End Sub

注釈

基本コンストラクターは、すべてのフィールドを既定値に初期化します。 次の表に、 SqlCommandのインスタンスの初期プロパティ値を示します。

プロパティ 初期値
CommandText 空の文字列 ("")
CommandTimeout 30
CommandType CommandType.Text
Connection Null

これらのプロパティの値は、プロパティを個別に呼び出して変更できます。

こちらもご覧ください

適用対象

SqlCommand(String)

ソース:
System.Data.SqlClient.notsupported.cs

クエリのテキストを使用して、 SqlCommand クラスの新しいインスタンスを初期化します。

public:
 SqlCommand(System::String ^ cmdText);
public SqlCommand(string cmdText);
new System.Data.SqlClient.SqlCommand : string -> System.Data.SqlClient.SqlCommand
Public Sub New (cmdText As String)

パラメーター

cmdText
String

クエリのテキスト。

次の例では、コマンド テキストを渡して SqlCommandを作成します。

public void CreateCommand()
 {
    string queryString = "SELECT * FROM Categories ORDER BY CategoryID";
    SqlCommand command = new SqlCommand(queryString);
    command.CommandTimeout = 15;
    command.CommandType = CommandType.Text;
 }
Public Sub CreateCommand()
    Dim queryString As String = "SELECT * FROM Categories ORDER BY CategoryID"
    Dim command As New SqlCommand(queryString)
    command.CommandTimeout = 15
    command.CommandType = CommandType.Text
End Sub

注釈

SqlCommandのインスタンスが作成されると、次の読み取り/書き込みプロパティが初期値に設定されます。

プロパティ 初期値
CommandText cmdText
CommandTimeout 30
CommandType CommandType.Text
Connection null 値

これらのプロパティの値は、プロパティを個別に呼び出して変更できます。

こちらもご覧ください

適用対象

SqlCommand(String, SqlConnection)

ソース:
System.Data.SqlClient.notsupported.cs

クエリのテキストとSqlCommandを使用して、SqlConnection クラスの新しいインスタンスを初期化します。

public:
 SqlCommand(System::String ^ cmdText, System::Data::SqlClient::SqlConnection ^ connection);
public SqlCommand(string cmdText, System.Data.SqlClient.SqlConnection connection);
new System.Data.SqlClient.SqlCommand : string * System.Data.SqlClient.SqlConnection -> System.Data.SqlClient.SqlCommand
Public Sub New (cmdText As String, connection As SqlConnection)

パラメーター

cmdText
String

クエリのテキスト。

connection
SqlConnection

SQL Serverのインスタンスへの接続を表す SqlConnection

次の例では、 SqlCommand を作成し、そのプロパティの一部を設定します。

private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(
            queryString, connection);
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            Console.WriteLine(String.Format("{0}, {1}",
                reader[0], reader[1]));
        }
    }
}
Public Sub CreateCommand(ByVal queryString As String, _
  ByVal connectionString As String)
    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)

        connection.Open()
        Dim reader As SqlDataReader = command.ExecuteReader()
            While reader.Read()
            Console.WriteLine(String.Format("{0}, {1}", _
                reader(0), reader(1)))
        End While
    End Using
End Sub

注釈

次の表に、 SqlCommandのインスタンスの初期プロパティ値を示します。

プロパティ 初期値
CommandText cmdText
CommandTimeout 30
CommandType CommandType.Text
Connection SqlConnection パラメーターの値である新しいconnection

これらのパラメーターの値は、関連するプロパティを設定することで変更できます。

こちらもご覧ください

適用対象

SqlCommand(String, SqlConnection, SqlTransaction)

ソース:
System.Data.SqlClient.notsupported.cs

クエリ、SqlCommand、およびSqlConnectionのテキストを使用して、SqlTransaction クラスの新しいインスタンスを初期化します。

public:
 SqlCommand(System::String ^ cmdText, System::Data::SqlClient::SqlConnection ^ connection, System::Data::SqlClient::SqlTransaction ^ transaction);
public SqlCommand(string cmdText, System.Data.SqlClient.SqlConnection connection, System.Data.SqlClient.SqlTransaction transaction);
new System.Data.SqlClient.SqlCommand : string * System.Data.SqlClient.SqlConnection * System.Data.SqlClient.SqlTransaction -> System.Data.SqlClient.SqlCommand
Public Sub New (cmdText As String, connection As SqlConnection, transaction As SqlTransaction)

パラメーター

cmdText
String

クエリのテキスト。

connection
SqlConnection

SQL Serverのインスタンスへの接続を表す SqlConnection

transaction
SqlTransaction

SqlTransactionが実行されるSqlCommand

注釈

次の表に、 SqlCommandのインスタンスの初期プロパティ値を示します。

プロパティ 初期値
CommandText cmdText
CommandTimeout 30
CommandType CommandType.Text
Connection SqlConnection パラメーターの値である新しいconnection

これらのパラメーターの値は、関連するプロパティを設定することで変更できます。

こちらもご覧ください

適用対象

SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)

指定したコマンド テキスト、接続、トランザクション、および暗号化設定を使用して、 SqlCommand クラスの新しいインスタンスを初期化します。

public:
 SqlCommand(System::String ^ cmdText, System::Data::SqlClient::SqlConnection ^ connection, System::Data::SqlClient::SqlTransaction ^ transaction, System::Data::SqlClient::SqlCommandColumnEncryptionSetting columnEncryptionSetting);
public SqlCommand(string cmdText, System.Data.SqlClient.SqlConnection connection, System.Data.SqlClient.SqlTransaction transaction, System.Data.SqlClient.SqlCommandColumnEncryptionSetting columnEncryptionSetting);
new System.Data.SqlClient.SqlCommand : string * System.Data.SqlClient.SqlConnection * System.Data.SqlClient.SqlTransaction * System.Data.SqlClient.SqlCommandColumnEncryptionSetting -> System.Data.SqlClient.SqlCommand
Public Sub New (cmdText As String, connection As SqlConnection, transaction As SqlTransaction, columnEncryptionSetting As SqlCommandColumnEncryptionSetting)

パラメーター

cmdText
String

クエリのテキスト。

connection
SqlConnection

SQL Serverのインスタンスへの接続を表す SqlConnection

transaction
SqlTransaction

SqlTransactionが実行されるSqlCommand

columnEncryptionSetting
SqlCommandColumnEncryptionSetting

暗号化設定。 詳細については、「 Always Encrypted」を参照してください。

適用対象