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

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(); 
}


慕斯王
浏览 481回答 2
2回答

阿波罗的战车

这个肯定是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修改后之后通过

蝴蝶不菲

strCon = "Update news set counter=" + i.ToString() + "where (id=" + newsid + ")";在where前加一个空格
打开App,查看更多内容
随时随地看视频慕课网APP