好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

c# – 当string为空时,将NULL插入SQL

尝试在我的组合框中没有选择任何内容时插入NULL但是当我这样做时:

prikaz.Parameters.AddWithValue("@odjezd", cb_odjezd.Text == string.Empty ?
                                               null : cb_odjezd.Text);

我收到以下异常:

The parameterized query expects the parameter @ odjezd

愿有人帮我解决这个问题吗?

尝试使用 DbNull.Value值.

所以它变成了

prikaz.Parameters.AddWithValue("@odjezd", 
         string.IsNullOrEmpty(cb_odjezd.Text) ? DbNull.Value : cb_odjezd.Text);

编辑

根据已接受的答案here,在您的情况下,对于(对象)DBNull.Value应该足够了

问题是C#在这里要求条件相同的类型值,但在一种情况下你在另一个字符串中设置DbNull.Value.所以将DbNull.Value强制转换为对象使“传递”该规则.

查看更多关于c# – 当string为空时,将NULL插入SQL的详细内容...

  阅读:50次