简体   繁体   English

无法使用C#更新到.mdb文件

[英]Cannot UPDATE to an .mdb file using C#

At first I tried this: 一开始我尝试了这个:

    string set = "";
    for (int i = 1; i < result.Count; i++)
    {
        if ((fieldtypes[i] == "System.Int32"))
        {
            set += fields[i] + "=" + result[i] + ", ";
        }
        else if (fieldtypes[i] == "System.String")
        {
            set += fields[i] + "='" + result[i] + "', ";
        }
        else if (fieldtypes[i] == "System.Boolean")
        {
            set += fields[i] + "=" + result[i] + ", ";
        }
        else if (fieldtypes[i] == "System.DateTime")
        {
            set += fields[i] + "='#" + System.DateTime.Now + "#', ";
        }
    }
    set = set.Substring(0, set.Length - 2);
    string sql11 = "UPDATE [Contacts] SET " + set + " WHERE pkContactID=" + cKey;
    OleDbCommand myCommand11 = new OleDbCommand(sql11, myConnection);
    myCommand11.ExecuteNonQuery();

Now this WORKED when I omitted the string and datetime conditions so that it only updated the int and boolean. 现在,当我省略了字符串和日期时间条件时,此方法就起作用了,因此它仅更新了int和boolean。 So it has something to do with a syntax error when I try to update a field where the type is a string. 因此,当我尝试更新类型为字符串的字段时,它与语法错误有关。


Then I heard that you have to use parameters when writing to an .mdb file, so I tried this: 然后,我听说在写入.mdb文件时必须使用参数,因此我尝试了以下操作:

        string sql11 = "UPDATE [Contacts] SET ";
        for (int i = 1; i < result.Count; i++)
        {
            sql11 += fields[i] + " = ?, ";
        }
        sql11 = sql11.Substring(0, sql11.Length - 2);
        sql11 += " WHERE pkContactID = " + cKey;
        using (myConnection)
        {
            using (OleDbCommand myCommand11 = new OleDbCommand(sql11, myConnection))
            {
                myCommand11.CommandType = CommandType.Text;
                for (int j = 1; j < result.Count; j++)
                {
                    if (fieldtypes[j] == "System.Int32")
                    {
                        myCommand11.Parameters.AddWithValue(fields[j], int.Parse(result[j]));
                    }
                    else if (fieldtypes[j] == "System.String")
                    {
                        myCommand11.Parameters.AddWithValue(fields[j], result[j]);
                    }
                    else if (fieldtypes[j] == "System.Boolean")
                    {
                        myCommand11.Parameters.AddWithValue(fields[j], Boolean.Parse(result[j]));
                    }
                    else if (fieldtypes[j] == "System.DateTime")
                    {
                        myCommand11.Parameters.AddWithValue(fields[j], DateTime.Now);
                    }
                }
                Console.WriteLine(sql11);
                myCommand11.ExecuteNonQuery();
            }
        }
    }

Which did not work either. 哪个也不起作用。 I don't think the ?'s are being replaced properly. 我不认为?已被正确替换。

Anyway, please help me fix it so that I can update properly. 无论如何,请帮助我修复它,以便可以正确更新。

Instead of having to mess around with the UPDATE query string for Access, which is easily prone to syntax errors, I just created a DataTable object and SELECTed the row I wanted to UPDATE. 不必弄乱Access的UPDATE查询字符串(它很容易出现语法错误),我只需要创建一个DataTable对象并选择要更新的行即可。 I then updated the table via an array of the elements that I wanted to change, then updated the table back to the server using an adapter. 然后,我通过要更改的元素数组更新了表,然后使用适配器将表更新回了服务器。 This worked out well without me worrying about the syntax! 这很顺利,无需担心语法! :) :)

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM