A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/dotnet/runtime/issues/82326 below:

Add CreateParameter() method (and CanCreateParameter) to DbBatchCommand · Issue #82326 · dotnet/runtime · GitHub

Thanks to @SoftStoneDevelop who posted the original issue; I've moved the original proposal to the bottom - it's very similar.

The DbBatch abstraction was added to System.Data in .NET 6.0 (issue). In a nutshell, it allows sending multiple SQL statements to the database in a single roundtrip through the use of multiple DbBatchCommands, each representing a statement. This is superior in various ways to the classical way of implementing batching in ADO.NET, which is to insert multiple semicolon-separated SQL statements into DbCommand.CommandText.

Unfortunately, due to an oversight, an API is missing on this abstraction for creating a a DbParameters, which are needed for parameterizing SQL.

API proposal
class DbBatchCommand
{
    public virtual DbParameter CreateParameter()
        => throw new NotSupportedException();

    public virtual bool CanCreateParameter
        => false;
}
Notes Provider issues: Original proposal by @SoftStoneDevelop Background and motivation

It is not possible to create a parameter for a DbBatchCommand without knowing the specific parameter of a specific provider (for example, NpgsqlParameter). This makes it difficult to use Batch abstracted from the provider.

With the non batch query, we are all right:

public void Method(DbConnection connection)
{
    DbCommand command = connection.CreateCommand();
    DbParameter commandParametr = command.CreateParameter();
    //...
    command.Parameters.Add(commandParametr);
    //...
}
API Proposal
public abstract class DbBatchCommand
{
    public abstract string CommandText { get; set; }

    public abstract CommandType CommandType { get; set; }

    public abstract int RecordsAffected { get; }

    public DbParameterCollection Parameters => DbParameterCollection;

    protected abstract DbParameterCollection DbParameterCollection { get; }

    public abstract DbParameter CreateParameter();
}
API Usage
public void Method(DbConnection connection)
{
    DbBatch batch = connection.CreateBatch();
    DbBatchCommand batchCommand = batch.CreateBatchCommand();
    //set batchCommand properties
    //...
    DbParametr batchParametr = batchCommand.CreateParameter();//!New Method!
    //set batchParametr properties
    //...
    batchCommand.Parameters.Add(batchParametr);
    batch.BatchCommands.Add(batchCommand);
    //execute batch
    //...
}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4