SqlCommand 建構函式

定義

多載

名稱 Description
SqlCommand()

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

SqlCommand(String)

初始化一個新的類別實例 SqlCommand ,並使用查詢的文字。

SqlCommand(String, SqlConnection)

初始化一個新的類別實例 SqlCommand ,包含查詢文本與 SqlConnection

SqlCommand(String, SqlConnection, SqlTransaction)

初始化一個新的類別實例 SqlCommand ,包含查詢文本 a SqlConnection、 和 SqlTransaction

SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)

初始化一個新的類別實例 SqlCommand ,並指定命令文字、連線、交易及加密設定。

SqlCommand()

來源:
SqlCommand.cs
來源:
SqlCommand.cs
來源:
SqlCommand.cs
來源:
SqlCommand.cs

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

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

範例

以下範例建立 並 SqlCommand 設定屬性 CommandTimeout

using System;
using System.Xml;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;
using Microsoft.Data.SqlClient;

public class Form1 : Form
{
    protected DataSet DataSet1;
    protected DataGrid dataGrid1;

    public void CreateSqlCommand()
    {
        SqlCommand command = new SqlCommand();
        command.CommandTimeout = 15;
        command.CommandType = CommandType.Text;
    }
}

備註

基礎建構子會將所有欄位初始化為預設值。 下表顯示 SqlCommand實例的初始屬性值。

屬性初始值
CommandText 空字串(“”
CommandTimeout 30
CommandType Text
Connection null

你可以透過單獨呼叫這些屬性來更改這些屬性的值。

適用於

SqlCommand(String)

來源:
SqlCommand.cs
來源:
SqlCommand.cs
來源:
SqlCommand.cs
來源:
SqlCommand.cs

初始化一個新的類別實例 SqlCommand ,並使用查詢的文字。

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

參數

cmdText
String

查詢的文字。

範例

以下範例會產生 SqlCommand一個 ,通過命令文本。

using System;
using System.Xml;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;
using Microsoft.Data.SqlClient;

public class Form1 : Form
{
    protected DataSet DataSet1;
    protected DataGrid dataGrid1;

    public void CreateCommand()
    {
        string queryString = "SELECT * FROM Categories ORDER BY CategoryID";
        SqlCommand command = new SqlCommand(queryString);
        command.CommandTimeout = 15;
        command.CommandType = CommandType.Text;
    }
}

備註

當建立 的 SqlCommand 實例時,以下的讀寫屬性會被設定為初始值。

屬性初始值
CommandText cmdText
CommandTimeout 30
CommandType Text
Connection null

你可以透過單獨呼叫這些屬性來更改這些屬性的值。

適用於

SqlCommand(String, SqlConnection)

來源:
SqlCommand.cs
來源:
SqlCommand.cs
來源:
SqlCommand.cs
來源:
SqlCommand.cs

初始化一個新的類別實例 SqlCommand ,包含查詢文本與 SqlConnection

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

參數

cmdText
String

查詢的文字。

connection
SqlConnection

一個代表與 SQL Server 實例連結的 SqlConnection

範例

以下範例會建立 並 SqlCommand 設定其部分性質。

using System;
using System.Data;
using Microsoft.Data.SqlClient;

namespace SqlCommandCS
{
    class Program
    {
        static void Main()
        {
            string str = "Data Source=(local);Initial Catalog=Northwind;"
                + "Integrated Security=SSPI";
            string qs = "SELECT OrderID, CustomerID FROM dbo.Orders;";
            CreateCommand(qs, str);

        }

        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]));
                }
            }
        }
    }
}

備註

下表顯示 SqlCommand實例的初始屬性值。

屬性初始值
CommandText cmdText
CommandTimeout 30
CommandType Text
Connection 一個新的 SqlConnection ,就是參數的 connection 值。

你可以透過設定相關屬性來更改這些參數的值。

適用於

SqlCommand(String, SqlConnection, SqlTransaction)

來源:
SqlCommand.cs
來源:
SqlCommand.cs
來源:
SqlCommand.cs
來源:
SqlCommand.cs

初始化一個新的類別實例 SqlCommand ,包含查詢文本 a SqlConnection、 和 SqlTransaction

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

參數

cmdText
String

查詢的文字。

connection
SqlConnection

一個代表與 SQL Server 實例連結的 SqlConnection

transaction
SqlTransaction

執行死刑的那個SqlTransactionSqlCommand

備註

下表顯示 SqlCommand實例的初始屬性值。

屬性初始值
CommandText cmdText
CommandTimeout 30
CommandType Text
Connection 一個新的 SqlConnection ,就是參數的 connection 值。

你可以透過設定相關屬性來更改這些參數的值。

適用於

SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)

來源:
SqlCommand.cs
來源:
SqlCommand.cs
來源:
SqlCommand.cs
來源:
SqlCommand.cs

初始化一個新的類別實例 SqlCommand ,並指定命令文字、連線、交易及加密設定。

public:
 SqlCommand(System::String ^ cmdText, Microsoft::Data::SqlClient::SqlConnection ^ connection, Microsoft::Data::SqlClient::SqlTransaction ^ transaction, Microsoft::Data::SqlClient::SqlCommandColumnEncryptionSetting columnEncryptionSetting);
public SqlCommand(string cmdText, Microsoft.Data.SqlClient.SqlConnection connection, Microsoft.Data.SqlClient.SqlTransaction transaction, Microsoft.Data.SqlClient.SqlCommandColumnEncryptionSetting columnEncryptionSetting);
new Microsoft.Data.SqlClient.SqlCommand : string * Microsoft.Data.SqlClient.SqlConnection * Microsoft.Data.SqlClient.SqlTransaction * Microsoft.Data.SqlClient.SqlCommandColumnEncryptionSetting -> Microsoft.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

執行死刑的那個SqlTransactionSqlCommand

columnEncryptionSetting
SqlCommandColumnEncryptionSetting

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

適用於