IDataParameter (OUTPUT) not working without size!

  • Thread starter Thread starter mawi
  • Start date Start date
M

mawi

Hi there,

(re-post since I never got any reply some weeks ago)

IDataParameter does not have a size property (for
string/varchar parameters), and without setting it it will
not return a value!

If I use SqlParameter (by temporarily casting the
idataparameter to a sqlparameter)
and set the size, all works well.

My code is like so (sketch):
IDbConnection [...] // create a sql connection, sql server
for example
IDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "aStoredProcedureName";
IDataParameter para = cmd.CreateParameter();
para.ParameterName = "@someOutParameter";
// (para as SqlParameter).Size = 20; // <--- works if included
para.Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery;
Console.Write( Convert.ToString( para.Value );


Now, this actually exists in a certain layer so this code
does not convey the design, obviously.

What is the solution to this? The IData/Db interfaces have
worked fine so far as an abstraction layer, it does not
seem logical that this would be unworkable.

Help greatly appreciated!!

/mawi
 
mawi said:
Hi there,

(re-post since I never got any reply some weeks ago)

IDataParameter does not have a size property (for
string/varchar parameters), and without setting it it will
not return a value!


IDataParameter
|
|__IDbDataParameter
|
|__SqlParameter

The IDbDataParameter interface adds the .size, .precision and .scale
properties.
If I use SqlParameter (by temporarily casting the
idataparameter to a sqlparameter)
and set the size, all works well.

My code is like so (sketch):
IDbConnection [...] // create a sql connection, sql server
for example
IDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "aStoredProcedureName";

Should be:
IDbDataParameter para = cmd.CreateParameter();
para.ParameterName = "@someOutParameter";
para.Size = 20; // <--- works if included
para.Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery;
Console.Write( Convert.ToString( para.Value );

David
 
Back
Top