我的代码:
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)在param
为空时抛出异常。数据库允许collumn aaa
中的值为null。如何在表xxx中插入null?
例外:
Parameterized query 'INSERT INTO xxx(aaa) VALUES(@aaa)' expects a parameter value which was not supplied.
答案 0 :(得分:4)
请尝试使用DBNull.Value
:
var param = DBNull.Value;
答案 1 :(得分:3)
使用
var param = DBNull.Value;
null与DBNull
不同