首页 > 解决方案 > 数据从 UI 移动到数据库时的路由差异(C# 到 SQL 服务器,浮动?到浮动)

问题描述

我有一个简单的 SQL Server 存储过程,它根据作为查找提供的实用程序 ID 列更新我的实用程序成本。

当我从 C# 调用此存储过程时,我使用的是以下代码。数据库最终得到的数据与实际成本略有不同。

例如,如果成本是 123,我可能会得到 123.000000048204

当我在代码中放置断点时,我发现它U.CostMonthCal[1]具有正确的值(例如 123)。U.CostMOnthCal[1]是类型float?

所以我把它当作浮动。代码运行良好。

但是 123 在到达数据库时会转换为 123.00000000xxxx。

我的所有专栏都是这样做的,但您不必查看整个代码。

只关注第一列,CostMonthCal1. 在数据库中,列类型是float(尽管我认为我可能可以将其转换为真实的,这也将节省空间......我将尝试这样做,看看它是否能解决我的问题)。

    public static void UpdateUtilityCosts(clsUtility U)
    {
        SqlConnection connection = null;

        try
        {
            connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConn"].ToString());
            connection.Open();
            SqlCommand com;

            com = new SqlCommand("dbo.UpdateUtilityCosts", connection);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.Add(new SqlParameter("@UtilityId", U.UtilityId));

            if (U.CostMonthCal[1] is null) { }  else { com.Parameters.Add(new SqlParameter("@CostMonthCal1", (float) U.CostMonthCal[1])); }
            if (U.CostMonthCal[2] is null) { } else { com.Parameters.Add(new SqlParameter("@CostMonthCal2", (float)U.CostMonthCal[2])); }
            if (U.CostMonthCal[3] is null) { } else { com.Parameters.Add(new SqlParameter("@CostMonthCal3", (float)U.CostMonthCal[3])); }
            if (U.CostMonthCal[4] is null) { } else { com.Parameters.Add(new SqlParameter("@CostMonthCal4", (float)U.CostMonthCal[4])); }
            if (U.CostMonthCal[5] is null) { } else { com.Parameters.Add(new SqlParameter("@CostMonthCal5", (float)U.CostMonthCal[5])); }
            if (U.CostMonthCal[6] is null) { } else { com.Parameters.Add(new SqlParameter("@CostMonthCal6", (float)U.CostMonthCal[6])); }
            if (U.CostMonthCal[7] is null) { } else { com.Parameters.Add(new SqlParameter("@CostMonthCal7", (float)U.CostMonthCal[7])); }
            if (U.CostMonthCal[8] is null) { } else { com.Parameters.Add(new SqlParameter("@CostMonthCal8", (float)U.CostMonthCal[8])); }
            if (U.CostMonthCal[9] is null) { } else { com.Parameters.Add(new SqlParameter("@CostMonthCal9", (float)U.CostMonthCal[9])); }
            if (U.CostMonthCal[10] is null) { } else { com.Parameters.Add(new SqlParameter("@CostMonthCal10", (float)U.CostMonthCal[10])); }
            if (U.CostMonthCal[11] is null) { } else { com.Parameters.Add(new SqlParameter("@CostMonthCal11", (float)U.CostMonthCal[11])); }
            if (U.CostMonthCal[12] is null) { } else { com.Parameters.Add(new SqlParameter("@CostMonthCal12", (float)U.CostMonthCal[12])); }
            if (U.LineLossPct is null) { } else { com.Parameters.Add(new SqlParameter("@LineLossPct", (float)U.LineLossPct)); }
            if (U.GRT is null) { } else { com.Parameters.Add(new SqlParameter("@GRT", (float)U.GRT)); }
            if (U.POR is null) { } else { com.Parameters.Add(new SqlParameter("@POR", (float)U.POR)); }

            if (U._12MonthCaps is null) { } else { com.Parameters.Add(new SqlParameter("@12MonthCaps", (float)U._12MonthCaps)); }
            if (U._12MonthNits is null) { } else { com.Parameters.Add(new SqlParameter("@12MonthNits", (float)U._12MonthNits)); }
            if (U._12MonthRate is null) { } else { com.Parameters.Add(new SqlParameter("@12MonthRate", (float)U._12MonthRate)); }

            if (U.NonPolarCTA is null) { } else { com.Parameters.Add(new SqlParameter("@NonPolarCTA", (float)U.NonPolarCTA)); }
            if (U.PolarCTA is null) { } else { com.Parameters.Add(new SqlParameter("@PolarCTA", (float)U.PolarCTA)); }

            if (com.Connection.State == ConnectionState.Closed) 
                com.Connection.Open();

            com.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            string Msg;
            Msg = "Encountered unexpected program issue.  Please contact your program administator.  Error details...";
            Msg = Msg + System.Environment.NewLine;
            Msg = Msg + System.Environment.NewLine;
            Msg = Msg + e.ToString();
            Msg = Msg + System.Environment.NewLine;
            Msg = Msg + System.Environment.NewLine;
            Msg = Msg + e.Message;

            throw new Exception("Error Saving Cost Data.  Contact Program Administrator " + Msg);
        }
        finally
        {
            if (connection == null) { } else connection.Dispose();
        }
    }

这是存储过程:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[UpdateUtilityCosts] 
    (@UtilityId int = null, 
     @CostMonthCal1 float = null, 
     @CostMonthCal2 float = null, 
     @CostMonthCal3 float = null, 
     @CostMonthCal4 float = null, 
     @CostMonthCal5 float = null, 
     @CostMonthCal6 float = null, 
     @CostMonthCal7 float = null, 
     @CostMonthCal8 float = null, 
     @CostMonthCal9 float = null, 
     @CostMonthCal10 float = null, 
     @CostMonthCal11 float = null, 
     @CostMonthCal12 float = null,
     @LineLossPct float = null,
     @GRT float = null,
     @POR float = null,
     @12MonthCaps float = null,
     @12MonthNits float = null,
     @12MonthRate float = null,
     @NonPolarCTA float = null,
     @PolarCTA float = null)
as
begin
    update Utilities 
    set CostMonth1 = @CostMonthCal1, 
        CostMonth2 = @CostMonthCal2, 
        CostMonth3 = @CostMonthCal3, 
        CostMonth4 = @CostMonthCal4, 
        CostMonth5 = @CostMonthCal5, 
        CostMonth6 = @CostMonthCal6, 
        CostMonth7 = @CostMonthCal7, 
        CostMonth8 = @CostMonthCal8, 
        CostMonth9 = @CostMonthCal9, 
        CostMonth10 = @CostMonthCal10, 
        CostMonth11 = @CostMonthCal11, 
        CostMonth12 = @CostMonthCal12,
        LineLossPct = @LineLossPct,
        GRT = @GRT,
        POR = @POR,
        [12MonthCaps] = @12MonthCaps,
        [12MonthNits] = @12MonthNits,
        [12MonthRate] = @12MonthRate,
        NonPolarCTA = @NonPolarCTA,
        PolarCTA = @PolarCTA
    where UtilityId =  @UtilityId
end
GO

标签: c#sql-servercastingrounding-error

解决方案


根据上面的评论,在代码中使用小数点和数据库中的任何小数点 (m, n) 类型似乎都有效。但是没有人发布作为答案,所以我走了....


推荐阅读