SqlCeCommand 可为空的值

本文关键字:SqlCeCommand | 更新日期: 2023-09-27 17:56:08

我的代码:

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 为 null 时引发异常。数据库允许列aaa中的值为空。如何将空插入表 xxx ?

例外:
Parameterized query 'INSERT INTO xxx(aaa) VALUES(@aaa)' expects a parameter value which was not supplied.

SqlCeCommand 可为空的值

尝试改用DBNull.Value

var param = DBNull.Value;

使用

var param = DBNull.Value;

null 与 DBNull 不是一回事