将SQL字段的值从数字类型转换为double。 输入字符串的格式不正确。 有人可以帮我解决这个问题吗?在此先感谢!
新名单声明:
public class OTTotal
{
public double OTPLAN;
public double OTACT;
}
然后我有这个代码将数据库字段的值传递给那些公共变量。
mylist.Add(new OTTotal
{
OTAct = double.Parse(dr["OT"].ToString()) /*+ double.Parse(dr["SH"].ToString()) + double.Parse(dr["SHtwo"].ToString())*/,
OTPlan = double.Parse(dr["POT"].ToString())
});
当我运行程序并说错误的格式时,此代码也会突出显示。
存储过程
CREATE PROCEDURE [dbo].[spTotalOverTime]
@grpID as INT = 0,
@date as DATE = '2018/3/1'
AS
BEGIN
SELECT SUM(a.Overtime) as OT, SUM(a.PlanOt) as POT, SUM(a.SunHoliday) as SH, SUM(a.SunHoliday2) as SHtwo FROM tblTimesheet a JOIN userinfo b on a.empID = b.empID JOIN tblGroup c on b.groupNo = c.groupID
WHERE YEAR(a.tsDate) = YEAR(@Date) AND MONTH(a.tsDate) = MONTH(@date) AND c.groupID = CAST(@grpID AS VARCHAR)
END
答案 0 :(得分:0)
尝试Convert.ToDouble(dr["OT"]);
答案 1 :(得分:0)
我已经解决了我的问题。我认为主要的问题是我的列中有一个空值,这就是为什么它不能转换为double。 但是,谢谢大家给我一个提示来解决它:)
所以这是解决问题的对策。
mylist.Add(new OTTotal
{
OTAct = (dr["OT"].ToString() == ""? 0.0 : double.Parse(dr["OT"].ToString())) + (dr["SH"].ToString() == "" ? 0.0 : double.Parse(dr["SH"].ToString())) + (dr["SHtwo"].ToString() == "" ? 0.0 : double.Parse(dr["SHtwo"].ToString())),
OTPlan = (dr["POT"].ToString() == "" ? 0.0 : double.Parse(dr["POT"].ToString()))
});
谢谢大家:)