SqlCommand 建構函式

定義

初始化 SqlCommand 類別的新執行個體。

多載

SqlCommand()

初始化 SqlCommand 類別的新執行個體。

SqlCommand(String)

使用查詢的文字,初始化 SqlCommand 類別的新執行個體。

SqlCommand(String, SqlConnection)

使用查詢的文字和 SqlConnection 初始化 SqlCommand 類別的新執行個體。

SqlCommand(String, SqlConnection, SqlTransaction)

使用查詢的文字、SqlConnectionSqlTransaction,初始化 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的初始屬性值。

屬性 初始值
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 實例時,下列讀取/寫入屬性會設定為初始值。

屬性 初始值
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

SqlConnection,代表 SQL Server 執行個體的連接。

範例

下列範例會建立 , 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)

使用查詢的文字、SqlConnectionSqlTransaction,初始化 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

SqlConnection,代表 SQL Server 執行個體的連接。

transaction
SqlTransaction

SqlCommand 執行的所在 SqlTransaction

備註

下表顯示 實例 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

SqlConnection,代表 SQL Server 執行個體的連接。

transaction
SqlTransaction

SqlCommand 執行的所在 SqlTransaction

columnEncryptionSetting
SqlCommandColumnEncryptionSetting

加密設定。 如需詳細資訊,請參閱 Always Encrypted

適用於