Share via


SqlBatchCommand 建構函式

定義

多載

SqlBatchCommand()

初始化新的 SqlBatchCommand

SqlBatchCommand(String, CommandType, IEnumerable<SqlParameter>, SqlCommandColumnEncryptionSetting)

初始化新的 SqlBatchCommand

SqlBatchCommand()

初始化新的 SqlBatchCommand

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

範例

下列範例會 SqlConnection 建立 和 SqlBatch,然後將多個 SqlBatchCommand 物件新增至批次。 然後它會執行批次,並建立 SqlDataReader。 此範例會讀取批次命令的結果,並將其寫入主控台。 最後,範例會 SqlDataReader 關閉 ,然後在 SqlConnectionusing 區塊落在範圍外時關閉 。

using Microsoft.Data.SqlClient;

class Program
{
    static void Main()
    {
        string str = "Data Source=(local);Initial Catalog=Northwind;"
        + "Integrated Security=SSPI;Encrypt=False";
        RunBatch(str);
    }

    static void RunBatch(string connString)
    {
        using var connection = new SqlConnection(connString);
        connection.Open();

        var batch = new SqlBatch(connection);

        const int count = 10;
        const string parameterName = "parameter";
        for (int i = 0; i < count; i++)
        {
            var batchCommand = new SqlBatchCommand($"SELECT @{parameterName} as value");
            batchCommand.Parameters.Add(new SqlParameter(parameterName, i));
            batch.BatchCommands.Add(batchCommand);
        }

        // Optionally Prepare
        batch.Prepare();

        var results = new List<int>(count);
        using (SqlDataReader reader = batch.ExecuteReader())
        {
            do
            {
                while (reader.Read())
                {
                    results.Add(reader.GetFieldValue<int>(0));
                }
            } while (reader.NextResult());
        }
        Console.WriteLine(string.Join(", ", results));
    }
}

適用於

SqlBatchCommand(String, CommandType, IEnumerable<SqlParameter>, SqlCommandColumnEncryptionSetting)

初始化新的 SqlBatchCommand

public SqlBatchCommand (string commandText, System.Data.CommandType commandType = System.Data.CommandType.Text, System.Collections.Generic.IEnumerable<Microsoft.Data.SqlClient.SqlParameter> parameters = default, Microsoft.Data.SqlClient.SqlCommandColumnEncryptionSetting columnEncryptionSetting = Microsoft.Data.SqlClient.SqlCommandColumnEncryptionSetting.UseConnectionSetting);
new Microsoft.Data.SqlClient.SqlBatchCommand : string * System.Data.CommandType * seq<Microsoft.Data.SqlClient.SqlParameter> * Microsoft.Data.SqlClient.SqlCommandColumnEncryptionSetting -> Microsoft.Data.SqlClient.SqlBatchCommand
Public Sub New (commandText As String, Optional commandType As CommandType = System.Data.CommandType.Text, Optional parameters As IEnumerable(Of SqlParameter) = Nothing, Optional columnEncryptionSetting As SqlCommandColumnEncryptionSetting = Microsoft.Data.SqlClient.SqlCommandColumnEncryptionSetting.UseConnectionSetting)

參數

commandText
String

SqlBatchCommand 的文字。

commandType
CommandType

指出如何 CommandText 解譯屬性。

parameters
IEnumerable<SqlParameter>

物件的集合 SqlParameter 可用來建立 SqlParameterCollection

columnEncryptionSetting
SqlCommandColumnEncryptionSetting

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

適用於