2

我正在开发一个使用 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();
        } 
4

2 回答 2

0

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

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

感谢您的提示和时间。

于 2012-11-22T09:51:01.757 回答
0

我认为这应该可行:-

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();
            } 
于 2012-11-19T16:23:47.120 回答