tags:

views:

30

answers:

2

Hi, I'm using a method in the EnterpriseLibrary.Data.Database namespace called

public virtual object ExecuteScalar(DbCommand command)

I'm wanting to use this method to execute a scalar-valued function. However the I'm not sure how to even create the DbCommand.

Could someone please tell me how to create it because it won't let me instantiate it.

Thanks!

+3  A: 

They're using the factory pattern, (one of the major benefits from using the enterprise library data access application block). The database needs to be correctly configured in app.config (or web.config). You can see an example of that here.

Here is an example of how you create a command:

Database db = DatabaseFactory.CreateDatabase();
DbCommand command = db.GetSqlStringCommand("SELECT Name FROM Customers");
...

(Remember to dispose.)

steinar
A: 

You cannot instantiate a DbCommand because it is an abstract base class.

You must instead create an instance of a concrete child class. You pick the child class based on what type of database you're talking to.

For example, if you're talking to SQL Server, you would create an instance of SQLCommand.

SqlConnection connection = new SqlConnection(connectionString);
DbCommand command = new System.Data.SqlClient.SqlCommand(commandText, connection);
Andrew Shepherd
Fair enough, but this is not the idea when using the data access application block, which he's doing. The major idea is using an abstract class (or interface) and configure in config (see my answer).
steinar