从C#到SQL Server的精度损失

时间:2021-11-13 16:55:26

This is an issue I am facing which is causing loss of precision when storing in SQL Server database from C# Entity Framework.

这是我面临的一个问题,当从C#Entity Framework存储在SQL Server数据库中时会导致精度损失。

  1. SQL Server Data Type is decimal(20, 15)
  2. SQL Server数据类型为十进制(20,15)
  3. In C# Property is defined as public decimal AssignedGrossBudget { get; set; }
  4. 在C#中,Property被定义为公共小数AssignedGrossBudget {get;组; }
  5. in C# value in variable (AssignedGrossBudget) is 34.09090909090909
  6. 在C#中的变量值(AssignedGrossBudget)是34.09090909090909
  7. But in SQL Server table it is 34.090000000000000
  8. 但在SQL Server表中它是34.090000000000000

What could be wrong? (I am using Entity Framework db.SaveChanges(); and SQLBulkCopy to store data from c# to SQL Server)

可能有什么不对? (我使用实体框架db.SaveChanges();和SQLBulkCopy将数据从c#存储到SQL Server)

I want to store 34.09090909090909 instead of 34.090000000000000.

我想存储34.09090909090909而不是34.090000000000000。

I checked by directly inserting in the table and it works.

我通过直接插入表中检查它是否有效。

2 个解决方案

#1


1  

A simple example shows that no such loss of precision occurs with correctly written code:

一个简单的例子表明,正确编写的代码不会出现这种精度损失:

    static void Main(string[] args)
    {
        decimal d = 34.09090909090909M;
        Console.WriteLine(d);

        SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
        scsb.IntegratedSecurity = true;
        scsb.DataSource = @".\sql2012";

        using (SqlConnection conn = new SqlConnection(scsb.ConnectionString)) {
            conn.Open();

            using (SqlCommand cmd = new SqlCommand(
               "select cast(@d as DECIMAL(20,15))", conn))
            {
                cmd.Parameters.AddWithValue("@d", d);
                decimal rd = (decimal) cmd.ExecuteScalar();
                Console.WriteLine(rd);
            }
        }
    }

Therefore I must conclude the problem is with your code, which is not posted.

因此,我必须总结问题是您的代码,而不是发布。

#2


2  

Thanks Urril and Remus.

谢谢Urril和Remus。

I make changes in Entity Framework as below:

我在Entity Framework中进行了如下更改:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<StationMapping>().Property(x => x.AssignedGrossBudget).HasPrecision(35, 15);

        }

And for SQL BulkCopy I added Data Type as per Remus suggestion.

对于SQL BulkCopy,我根据Remus建议添加了数据类型。

SpotLookupTable.Columns.Add(new DataColumn("GrossBudget",typeof(decimal)));

Its is working now and there is no loss (or Negligible).

它现在正在工作,没有任何损失(或可忽略不计)。

Cheers

干杯

#1


1  

A simple example shows that no such loss of precision occurs with correctly written code:

一个简单的例子表明,正确编写的代码不会出现这种精度损失:

    static void Main(string[] args)
    {
        decimal d = 34.09090909090909M;
        Console.WriteLine(d);

        SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
        scsb.IntegratedSecurity = true;
        scsb.DataSource = @".\sql2012";

        using (SqlConnection conn = new SqlConnection(scsb.ConnectionString)) {
            conn.Open();

            using (SqlCommand cmd = new SqlCommand(
               "select cast(@d as DECIMAL(20,15))", conn))
            {
                cmd.Parameters.AddWithValue("@d", d);
                decimal rd = (decimal) cmd.ExecuteScalar();
                Console.WriteLine(rd);
            }
        }
    }

Therefore I must conclude the problem is with your code, which is not posted.

因此,我必须总结问题是您的代码,而不是发布。

#2


2  

Thanks Urril and Remus.

谢谢Urril和Remus。

I make changes in Entity Framework as below:

我在Entity Framework中进行了如下更改:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<StationMapping>().Property(x => x.AssignedGrossBudget).HasPrecision(35, 15);

        }

And for SQL BulkCopy I added Data Type as per Remus suggestion.

对于SQL BulkCopy,我根据Remus建议添加了数据类型。

SpotLookupTable.Columns.Add(new DataColumn("GrossBudget",typeof(decimal)));

Its is working now and there is no loss (or Negligible).

它现在正在工作,没有任何损失(或可忽略不计)。

Cheers

干杯