2

I create a wrapper for System.Data.Common.DbProviderServices class. I need to edit DbCommand.CommandText in this wrapper. How can I get CommandText in CreateDbCommandDefinition method?

0

1 Answer 1

4

DbCommandDefinition class provides a single CreateCommand method returning DbCommand, hence works like a DbCommand factory.

Assuming you have a DbCommandDefinition instance obtained from the class you wrapping:

DbCommandDefinition commandDefinition = ...;

you can create DbCommand from it and examine all its properties, including the CommandText:

var command = commandDefinition.CreateCommand();
var commandText = command.CommandText;

However you cannot change command properties because every call creates a new command. So you need to create and return a DbCommandDefinition wrapper.

The simplest is like this (works for all DbCommand derived classes that implement ICloneable, which means all "normal" db provider DbCommand implementations, but not for EntityCommand):

class DbCommandDefinitionWrapper : DbCommandDefinition
{
    public DbCommandDefinitionWrapper(DbCommand prototype) : base(prototype) { }
}

and use something like this:

DbCommandDefinition commandDefinition = ...;
var command = commandDefinition.CreateCommand();
command.CommandText = ...;
commandDefinition = new DbCommandDefinitionWrapper(command);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.