-2

为什么以下存储过程会在.net c#中导致问题,它基本上用于屏幕上的收益报告的性质只读当我勾选传递开始日期和结束日期的空值但尝试执行时,它在 sql server 中工作正常在我的适配器填充下面相同,它会引发异常

程序 :

    public virtual EmployeeBenefitDataset GetBenefitsRecords(int id)
            {
                try
                {

                    EmployeeBenefitDataset ds = new EmployeeBenefitDataset();
                    using (BenefitTableAdapter adpt = new BenefitTableAdapter())
                    {
                        adpt.Connection.ConnectionString = DataAccessLogicHelper.PamsConnectionString;

                        adpt.Fill(ds.BenefitRecords, id, 4, null,null);
                    }
                    return ds;
                }
                catch (Exception ex)
                {
                    Logger.ErrorEntry(ex);
                    throw ex;
                }

存储过程

    GO
    /****** Object:  StoredProcedure [dbo].[hms_GetBenefitRecordsDetails]    Script Date: 09/09/2013 14:09:42 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[hms_GetBenefitRecordsDetails]
    (
        @empno int,
        @type int,
        @stardate datetime,
        @enddate datetime

    )
    AS
        SET NOCOUNT ON;
      IF NOT @stardate IS NULL
     begin 
     SELECT     benefit.emp_no, benefit.record_id, benefit.contract_id, benefit.career_id, benefit.date_from, benefit.date_to, benefit.benefit_type, benefit.monthly_value, 
                          benefit.benefit_provider, benefit.level_of_cover, benefit.previous_monthly_cost, benefit.benefit_change_details, benefit.current_benefit, benefit.notes, 
                          benefit.level_description, benefit.monthly_annual, benefit.cover_level, benefit.qualifying_reason, benefit_cover_level.desc_ AS benefitcoverdescription, 
                          benefit_provider.desc_ AS benefitproviderdescription,benefit_type.desc_ as benefittypedescription ,employee.benefit_annual_amount,employee.benefit_salary_option,employee.benefit_value
    FROM         benefit INNER JOIN                      
                          benefit_cover_level ON benefit.cover_level = benefit_cover_level.code INNER JOIN
                          benefit_provider ON benefit.benefit_provider = benefit_provider.code INNER JOIN
                           benefit_type ON benefit.benefit_type = benefit_type.code
                           INNER JOIN employee on benefit.emp_no = @empno
                           where benefit.emp_no= @empno   or employee.emp_no= benefit.emp_no
                          and  benefit.benefit_type = @type
     end 
     else
     begin 
     SELECT     benefit.emp_no, benefit.record_id, benefit.contract_id, benefit.career_id, benefit.date_from, benefit.date_to, benefit.benefit_type, benefit.monthly_value, 
                          benefit.benefit_provider, benefit.level_of_cover, benefit.previous_monthly_cost, benefit.benefit_change_details, benefit.current_benefit, benefit.notes, 
                          benefit.level_description, benefit.monthly_annual, benefit.cover_level, benefit.qualifying_reason, benefit_cover_level.desc_ AS benefitcoverdescription, 
                          benefit_provider.desc_ AS benefitproviderdescription,benefit_type.desc_ as benefittypedescription ,employee.benefit_annual_amount,employee.benefit_salary_option,employee.benefit_value
    FROM         benefit INNER JOIN                      
                          benefit_cover_level ON benefit.cover_level = benefit_cover_level.code INNER JOIN
                          benefit_provider ON benefit.benefit_provider = benefit_provider.code INNER JOIN
                           benefit_type ON benefit.benefit_type = benefit_type.code
                           INNER JOIN employee on benefit.emp_no = @empno
                           where benefit.emp_no= @empno   and employee.emp_no= benefit.emp_no


     end 

      IF NOT @stardate IS NULL
     begin 
     SELECT     benefit.emp_no, benefit.record_id, benefit.contract_id, benefit.career_id, benefit.date_from, benefit.date_to, benefit.benefit_type, benefit.monthly_value, 
                          benefit.benefit_provider, benefit.level_of_cover, benefit.previous_monthly_cost, benefit.benefit_change_details, benefit.current_benefit, benefit.notes, 
                          benefit.level_description, benefit.monthly_annual, benefit.cover_level, benefit.qualifying_reason, benefit_cover_level.desc_ AS benefitcoverdescription, 
                          benefit_provider.desc_ AS benefitproviderdescription,benefit_type.desc_ as benefittypedescription ,employee.benefit_annual_amount,employee.benefit_salary_option,employee.benefit_value
    FROM         benefit INNER JOIN                      
                          benefit_cover_level ON benefit.cover_level = benefit_cover_level.code INNER JOIN
                          benefit_provider ON benefit.benefit_provider = benefit_provider.code INNER JOIN
                           benefit_type ON benefit.benefit_type = benefit_type.code
                           INNER JOIN employee on benefit.emp_no = @empno
                           where benefit.emp_no= @empno   and employee.emp_no= benefit.emp_no and
                          benefit.benefit_type = @type and 
    benefit.date_from >= @stardate and benefit.date_to <= @enddate
    end 
4

3 回答 3

1

使用的最小和最大日期SQL不同C#......这可能会导致您出现“仅 .NET”错误。这些是可以处理的最小值和最大值SQL

private DateTime minSqlValue = new DateTime(1753, 1, 1, 0, 0, 0);
private DateTime maxSqlValue = DateTime.MaxValue.AddMilliseconds(-3);

请检查正在使用的日期,如果它们超出此范围,请更新它们。

于 2013-09-09T13:21:47.420 回答
1

当您选中复选框以传递空值时,它会显式传递NULL值和参数。在您当前的代码中,您甚至没有传递参数,这就是异常的原因。

在当前形式下,您的存储过程还没有准备好接受NULLstartdate 和 enddate 的值,您应该将您的过程修改为:

@stardate datetime  = NULL, 
@enddate datetime = NULL

对于NULL DateTime从 C# 传递使用DateTime?or Nullable<DateTime>,它支持Null值。

于 2013-09-09T13:17:33.393 回答
0

这是正确的,问题是 ADO.NET 看到您传递了一个空值,它具有System.Object,来调用 SQL Server DateTime 参数的过程,它将尝试映射到System.DateTime,而不是System.Nullable<System.DateTime>。因为System.DateTime不可为空,所以需要default(DateTime)发送到程序。default(DateTime)在 .NET 中是“01/01/0001 00:00:00.00”,它与 SQL Server 的最小 DateTime 值不匹配

更改您的存储过程以使用 DateTime2。

于 2013-09-09T13:59:12.247 回答