为了账号安全,请及时绑定邮箱和手机立即绑定

请问C#连接数据库报错,“=”附近有语法,该怎么解决?

请问C#连接数据库报错,“=”附近有语法,该怎么解决?

慕斯王 2021-12-02 11:07:39
DataSet ds;DataRow dr;string newsid;void Page_Load(Object sender, EventArgs e){SqlConnection MyConnection;string ConnStr, strCon;newsid = Request.Params["id"]; MyConnection = new SqlConnection("server=ADMIN-PC;database=news;uid=sa;pwd='123456'");MyConnection.Open();string strCom = "Select biaoti,zhaizi,neirong,img,counter,shijian from news where id=" + newsid;ds = new DataSet();SqlDataAdapter myCommand = new SqlDataAdapter(strCom, MyConnection);myCommand.Fill(ds, "news");dr = ds.Tables["news"].Rows[0];strCon = "Select counter from news where id=" + newsid;SqlCommand mycommand2 = new SqlCommand(strCon, MyConnection);SqlDataReader reader = mycommand2.ExecuteReader();reader.Read();int i = reader.GetInt32(0);i++;reader.Close(); strCon = "Update news set counter=" + i.ToString() + "where (id=" + newsid + ")";mycommand2.CommandText = strCon;mycommand2.ExecuteNonQuery(); MyConnection.Close(); }
查看完整描述

2 回答

?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

这个肯定是SQL语句的错误
Select biaoti,zhaizi,neirong,img,counter,shijian from news where id=' "+ newsid+"'
Select counter from news where id=' "+ newsid+"'
Update news set counter=" + i.ToString() + "where (id=" + newsid + ")
前两条错了 在sql2005修改后之后通过

查看完整回答
反对 回复 2021-12-06
?
蝴蝶不菲

TA贡献1810条经验 获得超4个赞

strCon = "Update news set counter=" + i.ToString() + "where (id=" + newsid + ")";
在where前加一个空格

查看完整回答
反对 回复 2021-12-06
  • 2 回答
  • 0 关注
  • 473 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信