为什么错误地计算日期之间的差异?

时间:2015-10-30 11:32:42

标签: c# asp.net sql-server ado.net

我想从另一个日期减去一个日期来计算天数的差异。它会返回735907之类的差异。为什么?它应该已经返回6,7或10等。

情景:

我正在从另一个日期中通过SP从数据库中减去我在TEXTCHANGED事件中传入文本框的日期。

alter PROCEDURE [dbo].[SelectBillSubmissionDateByBilliD]

    @pk_BillsInfo_BillID int,
    @BillSubmissionDate date output 


AS
BEGIN

          Set @BillSubmissionDate= (Select BillsInfo.BillSubmissionDate
          from BillsInfo
          Where BillsInfo.pk_BillsInfo_BillID= @pk_BillsInfo_BillID)


END

然后

Bills.ManageBillsInfo MngBillsInfo = new Bills.ManageBillsInfo();

    protected void ddlBills_SelectedIndexChanged(object sender, EventArgs e)
    {
        Int32 BillID= Convert.ToInt32(ddlBills.SelectedValue);
        MngBillsInfo.BillSubmissionDate = MngBillsInfo.SelectBillSubmissionDateByBillID(BillID);     
    }

    protected void txtBoxBillPassingDate_TextChanged(object sender, EventArgs e)
    {
        DateTime BillPassingDate = Convert.ToDateTime(txtBoxBillPassingDate.Text);
        lblDelay.Visible = true;
        lblDelay.Text = Convert.ToString( (BillPassingDate - MngBillsInfo.BillSubmissionDate).Days);
    }
}

注意:MngBillsInfo.BillSubmissionDate是Datetime,在调试时返回1/1/0001。为什么?

更新:

我打电话的功能:

 public DateTime SelectBillSubmissionDateByBillID(int BillID)
        {

            SqlCommand SqlCom = new SqlCommand("SelectBillSubmissionDateByBilliD", DatabaseConnection.OpenConnection());
            SqlCom.CommandType = CommandType.StoredProcedure;
            SqlCom.Parameters.AddWithValue("@pk_BillsInfo_BillID", BillID);

            SqlParameter SqlParamBillSubmissionDate = new SqlParameter("@BillSubmissionDate", SqlDbType.Date);
            SqlCom.Parameters.Add(SqlParamBillSubmissionDate);
            SqlParamBillSubmissionDate.Direction = ParameterDirection.Output;

            SqlCom.ExecuteNonQuery();
            DatabaseConnection.CloseConnection();
            DateTime BillSubmissionDate = Convert.ToDateTime(SqlParamBillSubmissionDate.Value);

            return BillSubmissionDate;

        }

1 个答案:

答案 0 :(得分:0)

更改sp以返回变量

Alter PROCEDURE [dbo].[SelectBillSubmissionDateByBilliD]
    @pk_BillsInfo_BillID int,
    @BillSubmissionDate date output 
AS
BEGIN
      Set @BillSubmissionDate = BillsInfo.BillSubmissionDate
      from BillsInfo
      Where BillsInfo.pk_BillsInfo_BillID= @pk_BillsInfo_BillID
 END

我认为查询返回的是多个值,并且您之前编写的方式选择了其中的任何一个。我已从您的语句中删除了select,现在如果您的语句返回的值超过1,则应该给出错误。