0

我正在开发一个将数据库中的数据导入属性网格的应用程序,通常它应该能够编辑该数据库。

这是我班的一个小原型:

public partial class MonthlyData
{
    public MonthlyData(string month, string year)
    {
        this.key = string.Format("{0} {1}", month, year);

        _IncomeMarketing = GetEntry(EntryType.Income, "Comercializarea produselor");
    }

    private string key = "";

    #region Income Properties
    [CategoryAttribute("Venituri"),
     DisplayName("Comercializare de produse"),
     Description("Venituri din comercializrea de produse")]
    public double Income_Marketing
    {
        get { return _IncomeMarketing; }
        set { _IncomeMarketing = SetEntry(EntryType.Income, "Comercializarea produselor", value); }
    }
    #endregion

    private double _IncomeMarketing;
}

以下是 my 的定义SetEntry()

private double SetEntry(EntryType type, string entryName, double entryNewValue)
{
    string tableName = (type == EntryType.Income) ? "Venituri" : "Cheltuieli";

    OleDbConnection connection = new OleDbConnection(Properties.Settings.Default.AccountingServicesConnectionString);
    connection.Open();

    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = connection;
    cmd.CommandText = string.Format("UPDATE {0} SET {1} = {2} WHERE Luna = \"{3}\"", tableName, entryName, entryNewValue, this.key);

    cmd.ExecuteNonQuery(); // I think here is my problem!

    return entryNewValue;
}

下图中是我的应用程序的两个不同步骤的打印屏幕:

  1. 属性网格从数据库中获取正确的值
  2. 当我更改该属性并按 ENTER 时,我收到该错误...

在此处输入图像描述

我不知道问题出在哪里。因为数据库数据类型没问题(它们是double

告诉我任何我能提供给你的帮助我,因为我会的。提前致谢!

编辑 在这里,我可以提供有关我的项目的更多信息。.accdb我的项目使用的文件。

4

2 回答 2

1

首先要做的是捕获异常,以便您可以调查它;也许同时添加一些using

try {
    using(var connection = new OleDbConnection(Properties.Settings.Default.AccountingServicesConnectionString))
    using(var cmd = connection.CreateCommand())
    {
        connection.Open();
        cmd.CommandText = string.Format(
            "UPDATE {0} SET {1} = {2} WHERE Luna = \"{3}\"",
            tableName, entryName, entryNewValue, this.key);

        cmd.ExecuteNonQuery(); // I think here is my problem!

        return entryNewValue;
    }
} catch(Exception ex) {
     SomeGlobalLogging(ex); // <=== put breakpoint here
     throw;
}

然后我们可以看看:

cmd.CommandText = string.Format(
    "UPDATE {0} SET {1} = {2} WHERE Luna = \"{3}\"",
    tableName, entryName, entryNewValue, this.key);

这可能有很多问题,尤其是:十进制说明符的格式问题(逗号与句点)。您应该使用参数,至少对于值(您不能参数化表/列名称)。IMO、{2} 和 {3} 应成为参数:

cmd.CommandText = string.Format(
    "UPDATE {0} SET {1} = :newVal WHERE Luna = :key", tableName, entryName);
cmd.Parameters.AddWithValue("newVal", entryNewValue);
cmd.Parameters.AddWithValue("key", this.key);
于 2013-08-12T08:45:34.707 回答
0

此代码块使我的查询工作:

我必须将表名和列名括起来:

字符串表名 =(类型 == EntryType.Income)?“Venituri”:“Celtuieli”;

try
{
    using (var connection = new OleDbConnection(Properties.Settings.Default.AccountingServicesConnectionString))
    using (var cmd = connection.CreateCommand())
    {
        connection.Open();
        cmd.CommandText = string.Format("UPDATE [{0}] SET [{1}] = ? WHERE [Luna] = ?", tableName, entryName);
        cmd.Parameters.AddWithValue("?", entryNewValue);
        cmd.Parameters.AddWithValue("?", this.key);

        cmd.ExecuteNonQuery();

        return entryNewValue;
    }
}
catch (Exception ex)
{
    Debug.WriteLine(ex); // <=== put breakpoint here
    throw;
}
于 2013-08-12T09:50:00.397 回答