我的C# 在调用update的时候报错说 where附近有语法错误。高手帮我

来源:百度知道 编辑:UC知道 时间:2024/06/06 08:00:59
sbFriends.Groups[1].Items.Add(sbFriends.SeletedItem);
sbFriends.Groups[0].Items.Remove(sbFriends.SeletedItem); //循环判断出来
string sqla = string.Format("update friend set stranger={0},friendid={1} where hostid={2} and friendid={0}",id,null,Idinformation.id);
SqlCommand commandd = new SqlCommand(sqla, DBHelper.connection);
DBHelper.connection.Open();
int result = commandd.ExecuteNonQuery();
if (!(result < 1))
{
MessageBox.Show("添加成功");
}
DBHelper.connection.Close();

难道不能使用null?????? 如果是,那为什么拿到 数据库里面又可以呢 。郁闷啊。。帮忙啊。

null那个改成双引号括起来的空""试试。

一定要更新为空吗?这个字段不进行更新行不行?

...C#中的NULL何数据库中的null是两回事,你不要混淆了。如果你想设置某个字段为null的话,可以像下面
string sqla = string.Format("update friend set stranger={0},friendid={1} where hostid={2} and friendid={0}",id,"null",Idinformation.id);
或者是
string sqla = string.Format("update friend set stranger={0},friendid=null where hostid={1} and friendid={0}",id,Idinformation.id);

肯定SQL语句有问题啊....语句呢?
`
PS:还在用ADO.NET的基础类库手动访问数据库吗?过时了....尝试下LINQ to Entity吧...

判断是否为空时应该使用 isnull 或 isnotnull (每个单词中间可能有空格,自己尝试),不能使用等号判断是否为空。

如果真的要空,一个是空串"",不过建意使用DBNull