Can't update access database threw asp.net

爷,独闯天下 提交于 2019-12-25 03:29:33

问题


my access database wont update with this code. what seems to be the problem? i have tried a lot of methods for updating my access database with no sucsess please guys some help.

protected void Btnupdate_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem RI in rptEdit.Items)
        {
            Label id = RI.FindControl("Pid") as Label;
            TextBox prdname = RI.FindControl("prdname") as TextBox;
            TextBox prdprice = RI.FindControl("prdprice") as TextBox;
            TextBox prdshortdesc = RI.FindControl("prdshortdesc") as TextBox;
            TextBox prdtype = RI.FindControl("prdtype") as TextBox;
            TextBox prdbrand = RI.FindControl("prdbrand") as TextBox;

            int updated;
            string connection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\Table.accdb";
            using (var conn = new OleDbConnection(connection))
            {
                conn.Open();
                string updSql = @"UPDATE ProductList SET
                    Pname = '" + prdname.Text + "' WHERE Pid = ?";
                using (var cmd = new OleDbCommand(updSql, conn))
                {
                    cmd.Parameters.Add("@Pname", OleDbType.VarChar).Value = prdname.Text;
                    updated = cmd.ExecuteNonQuery();
                    conn.Dispose();
                    conn.Close();
                }
            }
        }
    }

回答1:


Just use the ? style parameters in your SQL.

string sql = @"UPDATE ProductList SET Pname = ? WHERE Pid = ?";

Then just make sure you add your parameters in the same order in your code that they appear in the SQL.

cmd.Parameters.Add(prdName.Text);
cmd.Parameters.Add(int.Parse(id.Text));

You need to make sure the type of the variable being added in C# matches the type in the DB (in terms of text or number). Then it can be properly quoted or not as needed.



来源:https://stackoverflow.com/questions/50532972/cant-update-access-database-threw-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!