3

我使用存储过程来获取分页列表,这是我的方法:

using(SqlConnection conn = new SqlConnection(_connectionString)) {
   using(SqlCommand cmd = new SqlCommand("[GetPagedSP]", conn)) {
      cmd.CommandType = System.Data.CommandType.StoredProcedure;

       //Passing Parameters 
       **Update**
       SqlParameter spCreationDate = new SqlParameter("@CreationDate_9", CreationDate);
       spCreationDate.IsNullable = true;
       cmd.Parameters.Add(spCreationDate);

       // ........
       //Finished Passing Parameters
       conn.Open();

       SqlDataReader dr = cmd.ExecuteReader();

       while(dr.Read()) {
          //Get Values
       }
       conn.Close();
   }
}

这是我的存储过程命令:

CREATE TABLE #PagingTemp  (
[RowId]           [bigint]    IDENTITY(1,1)    NOT NULL,
[RecordId]        [bigint]  
);

INSERT INTO [#PagingTemp] ([RecordId])
SELECT  [CAR].[Id]      
FROM [Article] AS [CAR] ;

SELECT [CAR].*
FROM [Collections].[Article] AS [CAR]
INNER JOIN [#PagingTemp] AS [PT] ON [CAR].[Id] = [PT].[RecordId]
WHERE [PT].[RowId] BETWEEN 1 AND 50;

当我在 SQL 中运行查询时,一切都很好,但在 .NET 中,我在这一行有一个例外:

SqlDataReader dr = cmd.ExecuteReader();

例外是:

System.Data.SqlTypes.SqlTypeException 未被用户代码
Message=SqlDateTime 溢出处理。必须介于 1753 年 1 月 1 日上午 12:00:00 和 9999 年 12 月 31 日晚上 11:59:59 之间。
源=系统.数据

更新

在 SQL 中运行 Query 的示例: 在此处输入图像描述

太奇怪了,不明白怎么回事?

我没有任何datetime大于12/31/9999或小于的值1/1/1753

我在数据库中只有一些具有空值的可空日期时间值。

你怎么看?问题出在哪里?

4

3 回答 3

0

如果可能,请在此处粘贴 SP,建议修复。目前,您可以考虑以下可能性来尝试修复它:

如果 select 导致问题,请考虑将 NULL date 替换为 min date,如下所示:

SELECT IsNULL(CreationDate,'1/1/1753') CreationDate, IsNULL(LastBidDate,'1/1/1753') LastBidDate from yourtable.

作为参考,这是另一个已回答的问题: error-sqldatetime-overflow-must-be between-1-1-1753-120000-am-and-12-31-999

于 2012-07-30T09:03:55.637 回答
0

在将任何有效日期传递给变量 CreationDate 以执行存储过程之前

  //Passing Parameters 

   CreationDate = DateTime.Now or any other date you want if you dont want to pass any date then assign DBNull.Value

   **Update**
   SqlParameter spCreationDate = new SqlParameter("@CreationDate_9", CreationDate);
   spCreationDate.IsNullable = true;
   cmd.Parameters.Add(spCreationDate);

希望这能解决您的问题。

于 2012-07-30T08:26:30.020 回答
0

正如 在选择命令中的注释中提到的,当您传递Gavin类型参数时需要检查参数值是否在正确的范围内()?我传递了一个参数,我认为它的值是null,在这种情况下我从来没有在查询中使用这个参数,所以null值是正确的并且在命令中使用参数并不重要,当我再次检查时我发现参数的值不是 null 并且是. 另一种解决方案是使用支持.Net 支持的所有范围的类型。Nikola MarkovinovićDateTime1/1/1753 to 12/31/9999Nullable DateTime1/1/0001DATATIME2SQLDateTime

于 2012-07-31T11:28:29.363 回答