Bagikan melalui


SqlBatchCommand Konstruktor

Definisi

Overload

SqlBatchCommand()

Menginisialisasi baru SqlBatchCommand.

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

Menginisialisasi baru SqlBatchCommand.

SqlBatchCommand()

Menginisialisasi baru SqlBatchCommand.

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

Contoh

Contoh berikut membuat SqlConnection dan SqlBatch, lalu menambahkan beberapa SqlBatchCommand objek ke batch. Kemudian menjalankan batch, membuat SqlDataReader. Contohnya membaca hasil perintah batch, menulisnya ke konsol. Akhirnya, contoh menutup SqlDataReader dan kemudian saat SqlConnectionusing blok jatuh dari cakupan.

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

Berlaku untuk

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

Menginisialisasi baru 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)

Parameter

commandText
String

Teks dari SqlBatchCommand.

commandType
CommandType

Menunjukkan bagaimana CommandText properti akan ditafsirkan.

parameters
IEnumerable<SqlParameter>

Kumpulan SqlParameter objek digunakan untuk membuat SqlParameterCollection.

columnEncryptionSetting
SqlCommandColumnEncryptionSetting

Pengaturan enkripsi. Untuk informasi selengkapnya, lihat Always Encrypted.

Berlaku untuk