将十进制值插入货币访问字段

Insert decimal value into Currency Access field

简短背景: 我正在用基于 C# 的程序替换基于 Visual 的过时程序。该程序附带一个基于 Access (JET) 的数据库,该数据库已在使用中。现在,出于实际原因,我想连接到当前数据库。但是在可预见的将来,我想用 SQL 服务器替换它。

问题: 数据库使用 Access 'Currency' 数据类型存储财务信息。在 C# 中,我使用 'Decimal' 来表示财务信息。当然,当我尝试插入或更新查询以将十进制存储在货币字段中时,我会收到错误消息(实际上,我收到一次错误消息,它会自动将该特定记录的数据类型更改为十进制)。我不确定解决此问题的最佳方法是什么:

  1. 在我的程序中将十进制转换为货币并在之后插入。这甚至可能吗(C# 说字段中的值是 DBNull 类型,因为它不知道货币)?如果是,我该怎么做?
  2. 将字段的数据类型更改为十进制。 这样做会不会有损坏财务信息的风险?
  3. 有什么other/better建议吗?

提前致谢!


确切的错误信息:

System.Data.Odbc.OdbcException (0x80131937): ERROR [07006] 
[Microsoft][ODBC Microsoft Access-stuurprogramma]Inbreuk op kenmerk van beperkt gegevenstype

这是荷兰语,翻译成 Restricted data type attribute violation

我的更新代码:

public Boolean setFuelCosts(int rentID, Decimal fuelcosts)
        {
            string conString = lem2;
            string queryString = "UPDATE rental SET fuel = ? WHERE rentid = ?";
            OdbcCommand command = new OdbcCommand(queryString);
            command.Parameters.Add("@fuel", OdbcType.Decimal).Value = fuelcosts;
            command.Parameters.Add("@rentid", OdbcType.Int).Value = rentID;
            return factory.executeUpdateCommand(conString, command);
        }

public Boolean executeUpdateCommand(String conString, OdbcCommand command)
        {
            bool result = false;
            using (OdbcConnection connection = new OdbcConnection(conString))
            {
                try
                {
                    command.Connection = connection;
                    command.CommandType = CommandType.Text;
                    connection.Open();
                    int i = command.ExecuteNonQuery();
                    result = (i >= 1);
                    connection.Close();
                }
                catch (Exception exc)
                {
                    System.Windows.Forms.MessageBox.Show(exc.Message);
                    System.Windows.Forms.MessageBox.Show(exc.StackTrace);
                }
            }
            return result;
        }

您的问题似乎是在 .NET 中使用 System.Data.Odbc 处理 Currency 字段时 Access ODBC 驱动程序的限制。 OdbcDataReader 将 return 这些字段作为 System.Decimal (如果值不为 NULL),但 System.Data.Odbc 显然不会接受 System.Decimal Currency 字段的参数。

作为解决方法,您可以替换

command.Parameters.Add("@fuel", OdbcType.Decimal).Value = fuelcosts;

command.Parameters.Add("@fuel", OdbcType.NVarChar).Value = fuelcosts.ToString("0.0000");

我刚刚从 C# 2010 对 Access 2000 数据库文件进行了测试,它对我有用。