SqlBatch 類別

定義

public ref class SqlBatch : System::Data::Common::DbBatch
public class SqlBatch : System.Data.Common.DbBatch
type SqlBatch = class
    inherit DbBatch
Public Class SqlBatch
Inherits DbBatch
繼承
SqlBatch

範例

下列範例會 SqlConnection 建立 和 SqlBatch,然後將多個 SqlBatchCommand 物件新增至批次。 然後它會執行批次,並建立 SqlDataReader。 此範例會讀取批次命令的結果,並將其寫入主控台。 最後,此範例會SqlDataReader關閉 ,然後在SqlConnectionusing區塊落在範圍外時關閉 。

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

建構函式

SqlBatch()

初始化新的 SqlBatch

SqlBatch(SqlConnection, SqlTransaction)

初始化新的 SqlBatch

屬性

BatchCommands

包含在批次中的 SqlBatchCommandCollection命令清單。

Commands

物件中批次IListSqlBatchCommand中包含的命令清單。

Connection

取得或設定 SqlConnection 這個 實體 SqlBatch 所使用的 。

DbBatchCommands

取得 SqlBatchCommand 物件的集合。

DbConnection

取得或設定這個 DbConnection 所使用的 SqlBatch

DbTransaction

取得或設定 SqlTransaction,此 SqlBatch 物件會在其中執行。

Timeout

取得或設定在終止嘗試執行批次併產生錯誤之前,) 秒為單位的等候時間 (。

Transaction

取得或設定 SqlTransaction 命令執行所在的 SqlBatch

方法

Cancel()

嘗試取消的執行 SqlBatch

CreateDbBatchCommand()

建立 SqlBatchCommand 物件的新執行個體。

Dispose()

執行與釋放 (Free)、釋放 (Release) 或重設 Unmanaged 資源相關聯之應用程式定義的工作。

ExecuteDbDataReader(CommandBehavior)

針對其連線執行批次,傳 SqlDataReader 回可用來存取結果的 。

ExecuteDbDataReaderAsync(CommandBehavior, CancellationToken)

這個實作會 ExecuteReaderAsync() 叫用 方法,並傳回已完成的工作。 如果傳遞一個已經被取消的取消語彙基元,預設實作會傳回已取消的工作。

這個方法會接受可以用來要求提早取消作業的取消語彙基元。

ExecuteNonQuery()

針對其連接物件執行批次,傳回所有批次命令中受影響的數據列總數。

ExecuteNonQueryAsync(CancellationToken)

這是 ExecuteNonQuery() 的非同步版本。

實作會 ExecuteNonQueryAsync(CancellationToken) 叫用 方法,並傳回已完成的工作。 如果傳遞一個已經被取消的取消語彙基元,預設實作會傳回已取消的工作。

在傳回的工作完成之前,不叫用 DbCommand 物件的其他方法及屬性。

ExecuteReader()

Commands傳送至 Connection ,並建置 SqlDataReader

ExecuteReaderAsync(CancellationToken)

ExecuteReader() 異步版本,它會將 傳送 CommandsConnection ,並建置 SqlDataReader。 例外狀況將經由傳回的 Task 物件回報。

ExecuteScalar()

執行批次,並傳回第一個傳回結果集中第一個數據列的第一個數據行。 所有其他數據行、數據列和結果集都會被忽略。

ExecuteScalarAsync(CancellationToken)

ExecuteScalar()異步版本,它會執行批次,並傳回第一個傳回結果集中第一個數據列的第一個數據行。 所有其他數據行、數據列和結果集都會被忽略。

Prepare()

在數據源上建立已備妥的 (或已編譯) 版本的批次或其每個命令。

PrepareAsync(CancellationToken)

在數據源上,以異步方式建立已備妥的 (或已編譯) 的批次版本,或其每個命令的版本。

適用於