SqlCeCommand可以为空的值

时间:2011-05-11 00:42:28

标签: c# visual-studio-2010 sql-server-ce

我的代码:

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.

2 个答案:

答案 0 :(得分:4)

请尝试使用DBNull.Value

var param = DBNull.Value;

答案 1 :(得分:3)

使用

var param = DBNull.Value;

null与DBNull

不同