【问题标题】:Access 2007 database is not updatingAccess 2007 数据库未更新
【发布时间】:2012-11-07 14:21:31
【问题描述】:

我正在开发一个使用 Access 数据库的 .Net 桌面应用程序。我正在使用联系人表单并尝试更改类别字段,该字段在组合框中有多个选择值。我试图设置的值在选项列表中,但它没有做任何事情。这是我的代码。请说明发生了什么。该代码似乎适用于 DELETE 命令。

        string list = string.Join(", ", f);

        string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtDB.Text + "";

        string ComStr = "UPDATE Contacts SET Category = ? where [E-mail Address] in (?)";
        using (OleDbConnection con = new OleDbConnection(ConnStr))
        {
            con.Open();
            using (OleDbCommand com = new OleDbCommand(ComStr, con))
            {
                com.Parameters.AddWithValue("List", list);
                com.Parameters.AddWithValue("Category", "Не получава мейли");
                com.ExecuteNonQuery();
            }
            con.Close();
        } 

【问题讨论】:

  • 您必须按照它们在查询中出现的顺序添加参数。

标签: c# ms-access-2007 updating


【解决方案1】:

我认为这应该可行:-

string ComStr = "UPDATE Contacts SET Category = @Category where [E-mail Address] in @List";
            using (OleDbConnection con = new OleDbConnection(ConnStr))
            {
                con.Open();
                using (OleDbCommand com = new OleDbCommand(ComStr, con))
                {

                    com.Parameters.AddWithValue("@Category", "Не получава мейли");
                    com.Parameters.AddWithValue("@List", list);
                    com.ExecuteNonQuery();
                }
                con.Close();
            } 

【讨论】:

  • 它仍然没有对数据库做任何事情。
【解决方案2】:

我找到了答案。列表中的每个项目都必须在...好吧我不知道这个词,但这是我的工作代码:

    string list = string.Join("', '", f);
    string l = "'" + list + "'";

    string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtDB.Text + "";

    string ComStr = "UPDATE Contacts SET Category = @Category where [E-mail Address] in (" + l + ")";
    using (OleDbConnection con = new OleDbConnection(ConnStr))
    {
        con.Open();
        using (OleDbCommand com = new OleDbCommand(ComStr, con))
        {

            com.Parameters.AddWithValue("Category", "Не получава мейли");
            com.ExecuteNonQuery();
        }
        con.Close();
    } 

感谢您的提示和时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    相关资源
    最近更新 更多