无法更新访问数据库扔给asp.net [英] Can't update access database threw asp.net

查看:60
本文介绍了无法更新访问数据库扔给asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的访问数据库不会用此代码更新.似乎是什么问题? 我尝试了很多方法来更新我的访问数据库而没有成功 请大家帮忙.

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();
                }
            }
        }
    }

推荐答案

只需使用? SQL中的样式参数.

Just use the ? style parameters in your SQL.

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

然后只需确保按与SQL中出现的代码相同的顺序添加参数即可.

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));

您需要确保在C#中添加的变量类型与数据库中的类型匹配(以文本或数字表示).然后可以根据需要正确引用它.

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.

这篇关于无法更新访问数据库扔给asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆