SqlBatch.BatchCommands Właściwość
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Lista poleceń zawartych w partii w elemecie SqlBatchCommandCollection.
public:
property Microsoft::Data::SqlClient::SqlBatchCommandCollection ^ BatchCommands { Microsoft::Data::SqlClient::SqlBatchCommandCollection ^ get(); };
public Microsoft.Data.SqlClient.SqlBatchCommandCollection BatchCommands { get; }
member this.BatchCommands : Microsoft.Data.SqlClient.SqlBatchCommandCollection
Public ReadOnly Property BatchCommands As SqlBatchCommandCollection
Wartość właściwości
Przykłady
Poniższy przykład tworzy obiekt SqlConnection i SqlBatch, a następnie dodaje wiele SqlBatchCommand obiektów do partii. Następnie wykonuje partię, tworząc element SqlDataReader. Przykład odczytuje wyniki poleceń wsadowych, zapisuj je w konsoli. Na koniec przykład zamyka element SqlDataReader , a następnie SqlConnection element , ponieważ using
bloki wypadają poza zakres.
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));
}
}