SqlBatchCommand Konstruktory
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.
Przeciążenia
SqlBatchCommand() |
Inicjuje nowy SqlBatchCommandelement . |
SqlBatchCommand(String, CommandType, IEnumerable<SqlParameter>, SqlCommandColumnEncryptionSetting) |
Inicjuje nowy SqlBatchCommandelement . |
SqlBatchCommand()
Inicjuje nowy SqlBatchCommandelement .
public:
SqlBatchCommand();
public SqlBatchCommand ();
Public Sub New ()
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 SqlConnectionusing
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));
}
}
Dotyczy
SqlBatchCommand(String, CommandType, IEnumerable<SqlParameter>, SqlCommandColumnEncryptionSetting)
Inicjuje nowy SqlBatchCommandelement .
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)
Parametry
- commandText
- String
Tekst elementu SqlBatchCommand.
- commandType
- CommandType
Wskazuje sposób CommandText interpretowania właściwości.
- parameters
- IEnumerable<SqlParameter>
Kolekcja SqlParameter obiektów służy do tworzenia obiektu SqlParameterCollection.
- columnEncryptionSetting
- SqlCommandColumnEncryptionSetting
Ustawienie szyfrowania. Aby uzyskać więcej informacji, zobacz Always Encrypted.