数据库UPDATE有问题,用第二个strSQL语句是正常的 第一个不提示错误 但是数据没有写到数据库中~~
第二个也是UPDATE也无效
string id = Request.QueryString["id"];
string content = tb1.Text.Replace("\r\n", "<br>");
string user = tb2.Text;
//string strIp = "a124";
string strIp = Page.Request.UserHostAddress.ToString();
string strDate = DateTime.Now.ToLongTimeString();
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;data source=" + Server.MapPath("db.mdb");
//用这个更新 调试的时候不报错 但是数据其实没有更新到
string strSql = "update guestbook set g_Replyuser = @user, g_replycontent = @content, g_Replyip=@strIp, g_Replydate = @strDate where g_id = @id";
//用下面一句SQL语句更新数据库是正常的
// string strSql = "update guestbook set g_Replyuser = @user, g_replycontent = @content where g_id = @id";
OleDbConnection objConn = new OleDbConnection(strConn);
OleDbCommand cmd = new OleDbCommand(strSql, objConn);
cmd.Parameters.AddWithValue("@user", user);
cmd.Parameters.AddWithValue("@content", content);
cmd.Parameters.AddWithValue("@id",id);
cmd.Parameters.AddWithValue("@strIp", strIp);
cmd.Parameters.AddWithValue("@strDate", strDate);
objConn.Open();
cmd.ExecuteNonQuery();
objConn.Close();
string content = tb1.Text.Replace("\r\n", "<br>");
string user = tb2.Text;
//string strIp = "a124";
string strIp = Page.Request.UserHostAddress.ToString();
string strDate = DateTime.Now.ToLongTimeString();
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;data source=" + Server.MapPath("db.mdb");
//用这个更新 调试的时候不报错 但是数据其实没有更新到
string strSql = "update guestbook set g_Replyuser = @user, g_replycontent = @content, g_Replyip=@strIp, g_Replydate = @strDate where g_id = @id";
//用下面一句SQL语句更新数据库是正常的
// string strSql = "update guestbook set g_Replyuser = @user, g_replycontent = @content where g_id = @id";
OleDbConnection objConn = new OleDbConnection(strConn);
OleDbCommand cmd = new OleDbCommand(strSql, objConn);
cmd.Parameters.AddWithValue("@user", user);
cmd.Parameters.AddWithValue("@content", content);
cmd.Parameters.AddWithValue("@id",id);
cmd.Parameters.AddWithValue("@strIp", strIp);
cmd.Parameters.AddWithValue("@strDate", strDate);
objConn.Open();
cmd.ExecuteNonQuery();
objConn.Close();
第二个也是UPDATE也无效
string name = "test";
string pwd_new = this.tb_new.Text.Trim();
string strSql_update = "update [user] set [password] = @pwd_new where [name] = @name";
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;data source=" + Server.MapPath("db.mdb");
OleDbConnection objConn2 = new OleDbConnection(strConn);
OleDbCommand cmd2 = new OleDbCommand(strSql_update, objConn2);
cmd2.Parameters.AddWithValue("@name", name);
cmd2.Parameters.AddWithValue("@pwd_new", pwd_new);
objConn2.Open();
cmd2.ExecuteNonQuery();
objConn2.Close();
string pwd_new = this.tb_new.Text.Trim();
string strSql_update = "update [user] set [password] = @pwd_new where [name] = @name";
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;data source=" + Server.MapPath("db.mdb");
OleDbConnection objConn2 = new OleDbConnection(strConn);
OleDbCommand cmd2 = new OleDbCommand(strSql_update, objConn2);
cmd2.Parameters.AddWithValue("@name", name);
cmd2.Parameters.AddWithValue("@pwd_new", pwd_new);
objConn2.Open();
cmd2.ExecuteNonQuery();
objConn2.Close();
=====
结果处理是
把cmd.Parameters.AddWithValue("@id", id);放到后面
第二个也是
=====
自己的看法是 UPDATE里的cmd.Parameters.AddWithValue()和顺序是相关的。