Reputation: 35505
I have a table in a MySql database that stores user accounts. One of the columns, expires, stores an expiration date but defaults to NULL. I need to be able to remove an expiration date and set it back to the default value.
Currently, all of my CRUD routines are written using MySqlCommand with parameters. Can this be done directly with a MySqlParameter, or do I have to create an alternate command object to handle this eventuality?
Upvotes: 2
Views: 1447
Reputation: 36512
I usually set values that are intended to be default/blank to null in code, then before executing the query, run the following loop:
foreach(MySqlParameter param in cmd.Parameters)
if (param.Value == null) param.Value = DBNull.Value;
Upvotes: 0
Reputation: 35505
The problem was DBNull, doing:
command.Parameters.AddWithValue("@parameter", null);
compiles OK.
Upvotes: 3
Reputation: 123974
It's not clear what conditions you're talking about. If you want to set column to default value, you can use DbNull.Value;
command.AddWithValue("@param", DbNull.Value);
or
command.Parameters.Add("@param", <data type>).Value = DBNull.Value;
Upvotes: 0