连接access数据库,“update语句”出现语法错误,求正确语句。 10
前台页面如图所示,(连接上access数据库后可以进行修改,但是update语句语法错误,所以按下提交按钮时,会出现update语法错误)后台代码如下(update语句空...
前台页面如图所示,(连接上access数据库后可以进行修改,但是update语句语法错误,所以按下提交按钮时,会出现update语法错误)后台代码如下(update语句空着,没写。求教正确代码)
public partial class admin_editadmin2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserName"] != null && Session["UserName"].ToString() != "")
{
if (!Page.IsPostBack)
{
ShowUserInfo();
}
}
else
{
Response.Redirect("../login.aspx");
}
}
public void ShowUserInfo()
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("../db/member.mdb");
OleDbConnection conn = new OleDbConnection(strConn);
lblUserName.Text = Session["UserName"].ToString();
//从数据库读取用户信息
string sql = "select * from Users where UserName='" + lblUserName.Text + "'";
OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
OleDbDataReader reader = cmd.ExecuteReader();
reader.Read();
//把用户信息显示在页面上
txtpassword.Text = reader["password"].ToString();
txtAnswer.Text = reader["Answer"].ToString();
TextBox1.Text = reader["question"].ToString();
reader.Close();
conn.Close();
}
protected void btnSubmit_Click1(object sender, EventArgs e)
{
if (Page.IsValid)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("../db/member.mdb");
OleDbConnection conn = new OleDbConnection(strConn);
string name, password, question, answer;
//获取用户输入
name = Session["UserName"].ToString();
password = txtpassword.Text.Trim();
question = TextBox1.Text.Trim();
answer = txtAnswer.Text.Trim();
//构建UPDATE语句
string sql = @"UPDATE Users SET
OleDbCommand cmd = new OleDbCommand(sql, conn);
//加入Command的参数
cmd.Parameters.Add(new OleDbParameter("@password", OleDbType.WChar));
cmd.Parameters.Add(new OleDbParameter("@quesion", OleDbType.WChar));
cmd.Parameters.Add(new OleDbParameter("@answer", OleDbType.WChar));
//为Command的参数赋值
cmd.Parameters["@password"].Value = password;
cmd.Parameters["@quesion"].Value = question;
cmd.Parameters["@answer"].Value = answer;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Response.Redirect("houtai.aspx");
}
}
}
如图所示,要update的字段为密码(password),问题(question),答案(answer),要更新数据库中的users表。用户名所对应的是label控件,密码,问题,答案所对应的都是textbox。此页面改自于《基于c#的asp.net的程序设计》会员管理系统 展开
public partial class admin_editadmin2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserName"] != null && Session["UserName"].ToString() != "")
{
if (!Page.IsPostBack)
{
ShowUserInfo();
}
}
else
{
Response.Redirect("../login.aspx");
}
}
public void ShowUserInfo()
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("../db/member.mdb");
OleDbConnection conn = new OleDbConnection(strConn);
lblUserName.Text = Session["UserName"].ToString();
//从数据库读取用户信息
string sql = "select * from Users where UserName='" + lblUserName.Text + "'";
OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
OleDbDataReader reader = cmd.ExecuteReader();
reader.Read();
//把用户信息显示在页面上
txtpassword.Text = reader["password"].ToString();
txtAnswer.Text = reader["Answer"].ToString();
TextBox1.Text = reader["question"].ToString();
reader.Close();
conn.Close();
}
protected void btnSubmit_Click1(object sender, EventArgs e)
{
if (Page.IsValid)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("../db/member.mdb");
OleDbConnection conn = new OleDbConnection(strConn);
string name, password, question, answer;
//获取用户输入
name = Session["UserName"].ToString();
password = txtpassword.Text.Trim();
question = TextBox1.Text.Trim();
answer = txtAnswer.Text.Trim();
//构建UPDATE语句
string sql = @"UPDATE Users SET
OleDbCommand cmd = new OleDbCommand(sql, conn);
//加入Command的参数
cmd.Parameters.Add(new OleDbParameter("@password", OleDbType.WChar));
cmd.Parameters.Add(new OleDbParameter("@quesion", OleDbType.WChar));
cmd.Parameters.Add(new OleDbParameter("@answer", OleDbType.WChar));
//为Command的参数赋值
cmd.Parameters["@password"].Value = password;
cmd.Parameters["@quesion"].Value = question;
cmd.Parameters["@answer"].Value = answer;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Response.Redirect("houtai.aspx");
}
}
}
如图所示,要update的字段为密码(password),问题(question),答案(answer),要更新数据库中的users表。用户名所对应的是label控件,密码,问题,答案所对应的都是textbox。此页面改自于《基于c#的asp.net的程序设计》会员管理系统 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询