RowUpdating代码哪里出错了提示成功但是没有更新数据库
try
{
SqlConnection conn = new SqlConnection();
string connStr = "server=.;uid=sa;database=db_user;pwd=123;MultipleActiveResultSets=true;Integrated Security=true;";
conn.ConnectionString = connStr;
conn.Open();
SqlCommand cmd = new SqlCommand();
string xingming = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
string riqi = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
string chuqinjilu = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text;
string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
string sql = "update tb_chuqin set username='" + xingming + "',date='" + riqi + "',attendance='" + chuqinjilu + "'where id=" + id;
cmd.CommandText = sql;
cmd.Connection = conn;
if (cmd.ExecuteNonQuery() > 0)
{
Response.Write("<script>alert('更新成功');</script>");
}
else
{
Response.Write("<script>alert('更新失败');</script>");
}
GridView1.EditIndex = -1;
show();
conn.Close();
}
catch (Exception e1)
{
Response.Write(e1.Message);
}
这种测试你可以考虑手动修改数据库里的信息,然后看看显示的信息有没有更改。要知道,有时候没有更新的意思是:update student set StudentName=@StudentName where studentID=@studentID"根据你的这条指令,没有产生更新的意思。你手动修改下studentID=@studentID 的StudentName项,改成随便的名字。再运行这个按钮,你看变不变。