asp.net access数据库更新问题

来源:百度知道 编辑:UC知道 时间:2024/06/22 16:53:30
OleDbConnection myConn = new OleDbConnection();
myConn.ConnectionString = ConfigurationSettings.AppSettings["ConnStr2"];

string myExecuteQuery = "update [artres] set [artname]='" + artnTB.Text + "',[autname]='" + autnTB.Text + "',[artcontent]='" + artnTB.Text + "',[artfrom]='" + artfTB.Text + "',[artinstime]='" + System.DateTime.Now + "' WHERE ID=" + GridView1.Rows[id].Cells[1].Text + "";
OleDbCommand comm = new OleDbCommand(myExecuteQuery, myConn);
myConn.Open();
comm.ExecuteNonQuery();
myConn.Close();

代码如上,待更新时往往只更新最上面的一行,更新其他行时,最上面一行就被更新了,被更新那一行没没变,急!!!!到底怎么回事?谁能告诉我

你的代码有点问题。
GridView1.Rows[id].Cells[1].Text 只是获取都第一行的索引
所以只更新第一行
改写:
1.你吧更新按钮转换为模板,在CommandName属性中添加Update
2.选中GridView1控件添加RowUpdating事件(属性栏里面的那个闪电图标找到双击)
3.把下面这段代码写到RowUpdating事件里面
OleDbConnection myConn = new OleDbConnection();
myConn.ConnectionString = ConfigurationSettings.AppSettings["ConnStr2"];

string myExecuteQuery = "update [artres] set [artname]='" + artnTB.Text + "',[autname]='" + autnTB.Text + "',[artcontent]='" + artnTB.Text + "',[artfrom]='" + artfTB.Text + "',[artinstime]='" + System.DateTime.Now + "' WHERE ID=" + GridView1.Rows[e.RowIndex].Cells[1].Text + "";
OleDbCommand comm = new OleDbCommand(myExecuteQuery, myConn);
myConn.Open();
comm.ExecuteNonQuery();
myConn.Close();