SqlBatchCommand 构造函数

定义

重载

SqlBatchCommand()

初始化新的 SqlBatchCommand

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

初始化新的 SqlBatchCommand

SqlBatchCommand()

初始化新的 SqlBatchCommand

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

示例

以下示例创建 SqlConnection 和 SqlBatch,然后将多个 SqlBatchCommand 对象添加到批处理。 然后,它执行批处理,创建 SqlDataReader。 该示例通读批处理命令的结果,并将其写入控制台。 最后,该示例关闭 ,然后在SqlConnection块退出范围时using关闭 SqlDataReader

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

适用于