ASP.NET C# 的数据更新(update)问题
第一个select 按钮是根据条件搜索数据库,并且把数据库中对应的值显示到下面四个textbox中。(这个运行成功)
第二个update的按钮是将新输入在下面四个textbox 中的数据更新到数据库中。我内个代码我输入的数据进去不了数据库,求大神改正!
void Button1_Click(object sender, EventArgs e) {
string strconnect;
strconnect = "Provider=microsoft.jet.oledb.4.0;";
strconnect += "data source=" + MapPath("uktrip.mdb");
strconnect += ";persist security info=false";
OleDbConnection DBconnection = new OleDbConnection(strconnect);
OleDbCommand DBcommand = new OleDbCommand("select hot_city, hot_name, hot_star, hot_link FROM hotels WHERE hot_city like '%"+TextBox1.Text.Trim()+"%' ", DBconnection);
DBconnection.Open();
OleDbDataReader reader;
reader = DBcommand.ExecuteReader();
if (reader.Read())
{
TextBox2.Text = reader["hot_name"].ToString();
TextBox3.Text = reader["hot_star"].ToString();
TextBox4.Text = reader["hot_city"].ToString();
TextBox5.Text = reader["hot_link"].ToString();
}
Button2.Enabled = true;
reader.Close();
DBconnection.Close();
}
void Button2_Click(object sender, EventArgs e) {
string strconnect;
strconnect = "Provider=microsoft.jet.oledb.4.0;";
strconnect += "data source=" + MapPath("uktrip.mdb");
strconnect += ";persist security info=false";
OleDbConnection DBconnection = new OleDbConnection(strconnect);
OleDbCommand DBcommand = new OleDbCommand("update hotels set hot_city='"+TextBox4.Text+"', hot_name='"+TextBox2.Text+"' , hot_star='"+TextBox2.Text+"', hot_link='"+TextBox4.Text+"' WHERE hot_city=@hot_city ", DBconnection);
DBconnection.Open();
DBconnection.Close();
} 展开
void Button2_Click(object sender, EventArgs e)
{
string city = TextBox4.Text.Trim().Replace(",", "");
if (city == "") { Response.Write("<script>alert('city is not null ');</script>"); return; }
string strconnect;
strconnect = "Provider=microsoft.jet.oledb.4.0;";
strconnect += "data source=" + MapPath("uktrip.mdb");
strconnect += ";persist security info=false";
OleDbConnection DBconnection = new OleDbConnection(strconnect);
string sql = "";
sql = " SELECT COUNT(*) FROM hotels WHERE hot_city='" + city + "' ";
OleDbCommand DBcommand = new OleDbCommand();
DBcommand.CommandText = sql;
DBcommand.Connection = DBconnection;
DBconnection.Open();
int count = Convert.ToInt32(DBcommand.ExecuteScalar());
if (count == 0) { Response.Write("<script>alert('city is null ');</script>"); return; }
DBconnection.Close();
sql = "update hotels set hot_city=@hot_city'" + TextBox4.Text + "', hot_name=@hot_name'" + TextBox2.Text + "' , hot_star=@hot_star'" + TextBox2.Text + "', hot_link=@hot_link'" + TextBox4.Text + "' WHERE hot_city=@hot_city ";
OleDbParameter[] parm = {new OleDbParameter("@hot_city",city) };
DBcommand.CommandText = sql;
DBcommand.Connection = DBconnection;
DBconnection.Open();
DBcommand.ExecuteNonQuery();
DBconnection.Close();
}
//写了半天也没搞明白你具体想干嘛~~~
谢谢你的回答,就是只设置第二个按钮让他可以update就好了。。。我内个update有点毛病,你这个我用着 if (city == "") { Response.Write("alert('city is not null ');"); return; }说常量有换行符。。。
这个是判断文本框里有没有数据的
分享电脑常用小技巧,电脑知识,电脑技巧,电脑常见问题解决方法等,今天分享的是,4步方法解决Windows Update无法更新或更新报错,感谢粉丝们一直以来的支持
。。。那需要怎么改?求正确的改法,谢谢~
建议where条件 后 用ID 做指定。 如果非要用hot_city的话,那么 city的值就不能修改,要保持不变,这样数据库才会找到这条数据的。