SqlCommand コンストラクター

定義

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

オーバーロード

SqlCommand()

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

SqlCommand(String)

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

SqlCommand(String, SqlConnection)

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

SqlCommand(String, SqlConnection, SqlTransaction)

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

SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)

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

SqlCommand()

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表に示します。

Properties 初期値
CommandText 空の文字列 ("")
CommandTimeout 30
CommandType CommandType.Text
Connection [Null]

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

こちらもご覧ください

適用対象

SqlCommand(String)

クエリ テキストを指定して、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 インスタンスが作成されると、次の読み取り/書き込みプロパティが初期値に設定されます。

Properties 初期値
CommandText cmdText
CommandTimeout 30
CommandType CommandType.Text
Connection null

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

こちらもご覧ください

適用対象

SqlCommand(String, SqlConnection)

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

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表に示します。

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

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

こちらもご覧ください

適用対象

SqlCommand(String, SqlConnection, SqlTransaction)

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

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

SqlCommand が実行される SqlTransaction

注釈

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

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

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

こちらもご覧ください

適用対象

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

SqlCommand が実行される SqlTransaction

columnEncryptionSetting
SqlCommandColumnEncryptionSetting

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

適用対象