开发者

SqlCeCommand nullable values

开发者 https://www.devze.com 2023-03-05 10:57 出处:网络
My code: SqlCeConnection sql = new SqlCeConnection(@\"Data Source=C:\\DB.sdf\"); sql.Open(); cmd = new SqlCeCommand(\"INSERT INTO xxx(aaa) VALUES(@aaa)\", sql);

My code:

SqlCeConnection sql = new SqlCeConnection(@"Data Source=C:\DB.sdf");
sql.Open();
cmd = new SqlCeCommand("INSERT INTO xxx(aaa) VALUES(@aaa)", sql);
String param = null;  //doesn't work
//String param = "blah" //works
cmd.Parameters.AddWithValue("@aaa", param);
cmd.ExecuteNonQuery(); //(1)
sql.Close();

(1) throws an exception when param is null. Database allows value in collumn aaa to be null. How can I insert null into table xxx ?

Exception:

Parameterized query 'INSERT INTO xxx(aaa) VALUES(@aaa)' expects a parameter value which w开发者_JAVA技巧as not supplied.


Try using DBNull.Value instead:

var param = DBNull.Value;


Use

var param = DBNull.Value;

null isn't the same thing as DBNull

0

精彩评论

暂无评论...
验证码 换一张
取 消