Partilhar via


SqlBatchCommand Construtores

Definição

Sobrecargas

SqlBatchCommand()

Inicializa um novo SqlBatchCommand.

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

Inicializa um novo SqlBatchCommand.

SqlBatchCommand()

Inicializa um novo SqlBatchCommand.

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

Exemplos

O exemplo a seguir cria um SqlConnection e um SqlBatch e adiciona vários SqlBatchCommand objetos ao lote. Em seguida, ele executa o lote, criando um SqlDataReader. O exemplo lê os resultados dos comandos do lote, gravando-os no console. Por fim, o exemplo fecha o SqlDataReader e, em seguida, o SqlConnection como os using blocos ficam fora do escopo.

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

Aplica-se a

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

Inicializa um novo 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)

Parâmetros

commandText
String

O texto do SqlBatchCommand.

commandType
CommandType

Indica como a CommandText propriedade deve ser interpretada.

parameters
IEnumerable<SqlParameter>

Uma coleção de SqlParameter objetos é usada para criar o SqlParameterCollection.

columnEncryptionSetting
SqlCommandColumnEncryptionSetting

A configuração de criptografia. Para obter mais informações, consulte Always Encrypted.

Aplica-se a