tags:

views:

451

answers:

2

I have add a null value to parameter list for inserting values to a table, which accepts some null values. Here is my example codes:

 bool sql = true;
 // ....
 List<IDbDataParameter> parameters = new List<IDbDataParmeter>();
 // adding values...
 object objVal = 1;
 parameters.Add(
    sql ? new SqlParameter("@colIntA", objVal) : 
          new OracleParamer(":colIntA", objVal));
 // this would not work, what should I add?
 objVal = string.Empty;
 parameters.Add(
    sql ? new SqlParameter("@colIntB", objVal) : 
          new OracleParamer(":colIntB", objVal));

Not sure if I have use db specific DbNull value and is that also SQL or Oracle specific?

+1  A: 

Use DbNull.Value. That will work for any ADO.NET-compatible data source (both SQL and Oracle providers), as it is the responsibility of the provider to know what to do when it encounters a DbNull value.

Rex M
+1  A: 

You're trying to assign an empty string ('') to an int parameter there, so yeah, that's not going to work.

To represent a database-independent null value, use DbNull.Value.

 new SqlParameter("colIntB", DbNull.Value)

(Note that I've left off the "@", which works in my experience with Sqlparameters. I'm not sure whether you can do the same with the ":" for Oracle.)

One extra tip: Use your connection to create the command, and the command to create the parameters. That will create instances of the right class depending on the type of the underlying connection:

IDbConnection conn = sql ? new SqlConnection(...) : new OracleConnection(...);

// this will give you either an SqlCommand or an OracleCommand
IDbCommand cmd = conn.CreateCommand();

// this will give you either an SqlParameter or an OracleParameter
IDbDataParameter param1 = cmd.CreateParameter();
param1.ParameterName = "colIntB";
param1.Value = objVal;

cmd.Parameters.Add(param1);
Matt Hamilton
Very interesting point on getting parameter. If I have a list of parameters, I may need to create a loop to create, set name, set value and set direction if needed. Thanks!
David.Chu.ca
David - also take a look at my AddInputParameters extension method. Makes things really easy: http://www.madprops.org/blog/adding-idbcommand-parameters-with-anonymous-types/
Matt Hamilton
actually in the class where parameter is created in my case, there is no IdbConnection nor IDbCommand is available. I have a SQLParameter class for creating collection of IDbDataParameter. I do have information about sql or oracle. Can I create a dummy connection and use it get parameters?
David.Chu.ca
OK. Both SqlConnection and OracleConnection have new() CTOR. I guess I can it as dummy connection to get IdbCommand and then IdbDataParameter, am I right or exception would be thrown?
David.Chu.ca
No, I wouldn't create a dummy connection. If you don't know the connection/command details at the time you're creating the parameters then your current method is probably safest. Can you not pass the command into the method that creates the parameters?
Matt Hamilton
Thanks for your information. I separate parameter and connection in two different classes. I could pass connection in for creating parameters, however, since I only deal with sql and oracle cases, it is ok as-they-are.
David.Chu.ca