问题:string sqlStr = “UPDATE Student SET Name =”+ Name+",Sex="+Sex+",Age"=+Age+",Class="+Class+" WHERE ID="+ID;
正确的Sql语句写法:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string ID = GridView1.DataKeys[e.RowIndex].Value.ToString();
string Name =((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();
string Sex = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
string Age = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();
string Class = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString();
string sqlStr = “UPDATE Student SET Name =’”+ Name+"’,Sex=’"+Sex+"’,Age=’"+Age+"’,Class=’"+Class+"’ WHERE ID="+ID;
SqlConnection myConn = new SqlConnection(“Server=(local);DataBase=School;User ID=sa;Password=sasa”);
myConn.Open();
SqlCommand myComm = new SqlCommand(sqlStr,myConn);
myComm.ExecuteNonQuery();
myComm.Dispose();
myConn.Close();
GridView1.EditIndex = -1;
GridViewBind();
}
注意字符串中带变量、带引号的的使用方法,如果字符串中带双引号需用单引号括起来