SqlBatchCommand Constructors
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
SqlBatchCommand() |
Initializes a new SqlBatchCommand. |
SqlBatchCommand(String, CommandType, IEnumerable<SqlParameter>, SqlCommandColumnEncryptionSetting) |
Initializes a new SqlBatchCommand. |
SqlBatchCommand()
Initializes a new SqlBatchCommand.
public:
SqlBatchCommand();
public SqlBatchCommand ();
Public Sub New ()
Examples
The following example creates a SqlConnection and a SqlBatch, then adds multiple SqlBatchCommand objects to the batch. It then executes the batch, creating a SqlDataReader. The example reads through the results of the batch commands, writing them to the console. Finally, the example closes the SqlDataReader and then the SqlConnection as the using
blocks fall out of scope.
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();
using 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);
}
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));
}
}
Applies to
SqlBatchCommand(String, CommandType, IEnumerable<SqlParameter>, SqlCommandColumnEncryptionSetting)
Initializes a new 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)
Parameters
- commandText
- String
The text of the SqlBatchCommand.
- commandType
- CommandType
Indicates how the CommandText property is to be interpreted.
- parameters
- IEnumerable<SqlParameter>
A collection of SqlParameter objects is used to create the SqlParameterCollection.
- columnEncryptionSetting
- SqlCommandColumnEncryptionSetting
The encryption setting. For more information, see Always Encrypted.