任何人都可以查看我的代码并告诉我有什么问题吗?我正在尝试更新值,但Access数据库没有更新,即使我的方法正在解析查询。
protected void Page_Load(object sender, EventArgs e)
{
if ((string)Session["sFlag"] != "LI")
{
Response.Redirect("Default.aspx");
}
else
{
strUserId = (string)Session["sUserId"];
txtUserId.Text = strUserId;
txtName.Text = (string)Session["sName"];
txtAddress.Text = (string)Session["sAddress"];
txtEmail.Text = (string)Session["sEmail"];
txtTel.Text = (string)Session["sContactNo"];
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
strUserId = (string)Session["sUserId"];
if (txtName.Text != "")
{
String strFName = "cFullName";
String strFValue = txtName.Text;
UpdateCustomer(strFName, strFValue);
Session["sName"] = txtName.Text;
}
if (UFlag == "T")
{
Type strType = this.GetType();
ClientScript.RegisterStartupScript(strType, "Success", scriptSuccessUpdate);
}
}
public void UpdateCustomer(string strFName, string strFValue)
{
OleDbConnection mDB = new OleDbConnection();
mDB.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0;Data source="
+ Server.MapPath("~/App_Data/WMA_Proj.accdb");
mDB.Open();
OleDbCommand cmd;
String strSQL = "UPDATE Customers SET " + strFName +
" = @newValue WHERE cUserId = @userId";
cmd = new OleDbCommand(strSQL, mDB);
cmd.Parameters.Add("@newValue", OleDbType.Char).Value = strFValue;
cmd.Parameters.Add("@userId", OleDbType.Char).Value = strUserId;
cmd.ExecuteNonQuery();
UFlag = "T";
mDB.Close();
}
}
这也是我的Access数据库表(图像)。
答案 0 :(得分:0)
试试这个
public void UpdateCustomer(string strFName, string strFValue)
{
OleDbConnection mDB = new OleDbConnection();
mDB.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0;Data source="
+ Server.MapPath("~/App_Data/WMA_Proj.accdb");
mDB.Open();
OleDbCommand cmd;
String strSQL = "UPDATE Customers SET cFullname= @newValue WHERE cUserId = @userId";
cmd = new OleDbCommand(strSQL, mDB);
cmd.Parameters.AddWithValue("@newValue", strFName);
cmd.Parameters.AddWithValue("@userId",strFvalue);
cmd.ExecuteNonQuery();
UFlag = "T";
mDB.Close();
}