I want to create a new DbCommand, but get some errors:
DbCommand insertcommand = new DbCommand("insert into persons(firstname, lastname, email) values(?, ?, ?)", connection);
insertcommand.Parameters.Add(1, DbType.String, 50, "fistname");
insertcommand.Parameters.Add(2, DbType.String, 50, "lastname");
insertcommand.Parameters.Add(3, DbType.String, 100, "email");
dbAdapter.InsertCommand = insertcommand;
This yields the errors
Cannot create an instance of the abstract class Or interface
“System.Data.Common.DbCommand”
and
No overload for method ‘Add’ takes 4 arguments
How should I be creating these commands and populating the parameters?
You normally need to either create a concrete instance directly (
new SqlCommandetc) or useIDbConnection.CreateCommand.It’s not clear which is the most appropriate for your situation.
As for the
Addcalls – if you use a concrete command type, you may well have more overloads available to you. Otherwise you’ll probably want to useIDbCommand.CreateParameter, set properties, then add it to the parameter collection.