数据从 UI 移动到数据库时的舍入差异(C# 到 SQL 服务器,浮动?浮动)
Rouding discrepencies when data moves from UI to database (C# to SQL server, float? to float)
我有一个简单的 SQL 服务器存储过程,它根据作为查找提供的实用程序 ID 列更新我的实用程序成本。
当我从 C# 调用此存储过程时,我使用了以下代码。数据库最终得到的数据与实际成本略有不同。
比如成本是123,我可能得到123.000000048204
当我在代码中放置断点时,我发现 U.CostMonthCal[1]
具有正确的值(例如 123)。 U.CostMOnthCal[1]
属于 float?
.
类型
所以我将其转换为浮点数。代码运行良好。
但是123到数据库的时候会转换成123.00000000xxxx
我的所有专栏都这样做,但您不必查看整个代码。
只关注第一列,CostMonthCal1
。在数据库中,列类型是float
(虽然我想我可能可以将它转换为真实的,而且这也可以节省space ...我正要尝试,看看它是否修复我的问题)。
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
根据上面的评论,在代码中使用小数和在数据库中使用任何小数 (m, n) 类型似乎都可行。但是没有人发布作为答案,所以我走了....
我有一个简单的 SQL 服务器存储过程,它根据作为查找提供的实用程序 ID 列更新我的实用程序成本。
当我从 C# 调用此存储过程时,我使用了以下代码。数据库最终得到的数据与实际成本略有不同。
比如成本是123,我可能得到123.000000048204
当我在代码中放置断点时,我发现 U.CostMonthCal[1]
具有正确的值(例如 123)。 U.CostMOnthCal[1]
属于 float?
.
所以我将其转换为浮点数。代码运行良好。
但是123到数据库的时候会转换成123.00000000xxxx
我的所有专栏都这样做,但您不必查看整个代码。
只关注第一列,CostMonthCal1
。在数据库中,列类型是float
(虽然我想我可能可以将它转换为真实的,而且这也可以节省space ...我正要尝试,看看它是否修复我的问题)。
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
根据上面的评论,在代码中使用小数和在数据库中使用任何小数 (m, n) 类型似乎都可行。但是没有人发布作为答案,所以我走了....