OleDbParameter name obsession

Since it OleDbParameterdoes not use named parameters (by nature), why does the .NET class OleDbParameterexpect a name?(string parametername ...)

All constructors require a parameter name, and I never know what name this will give; is my name okay? or the name of my grandmother?

+3
source share
3 answers

Although OleDb / Odbc providers use positional parameters instead of named parameters -

  • parameters must be identified somehow inside the OleDbParameter collection if you need to reference them.

  • , sql, . sql.

, :

string sql = "SELECT * FROM MyTable WHERE Field = ?";
OleDbCommand cmd = new OleDbCommmand(sql, sqlConnection);
cmd.Parameters.Add("@FieldValue", OleDbType.Int32, 42);
OleDbDataReader dr = cmd.ExecuteReader();

SQL - ( ):

DECLARE @FieldValue INT;
SET @FieldValue = 42;
SELECT * FROM MyTable WHERE Field = @FieldValue

, .

+3

, - ! (, , ..).

#:

OleDbCommand command = new OleDbCommand();
...//Add your parameters with names, then reference them later if you need to:
command.Parameters["name"].Value = "your name or your grandmothers name here";

, OUT :

OleDbParameter outParam = new OleDbParameter();
outParam.Direction = ParameterDirection.Output;
outParam.DbType = DbType.Date;
outParam.ParameterName = "outParam";
command.Parameters.Add(outParam);
command.ExecuteNonQuery();
DateTime outParam = Convert.ToDateTime(command.Parameters["outParam"].Value);
+1

There are interfaces and factories that provide the degree of independence of the basic provider of data access - IDataParameter, DbParameter, DbProviderFactory, etc.

To support this, all parameters can be named even if the name is not used by the underlying provider.

+1
source

All Articles