由于Varchar转换为DateTime错误导致SQL过程失败

时间:2011-10-21 09:27:13

标签: sql vb.net

我遇到了我编写的存储过程的问题,我可以在管理工作室中调用它并且它工作正常,但是当我调用da.Fill(dt)时从我的代码隐藏中调用它会出现此错误The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

我真正感到困惑的是,我实际上并没有将varchar中的任何内容转换为日期时间,我对日期变量的处理最多的是将它们格式化为不同的日期时间格式。

我的VB.Net代码

  Private Function dbGetEvents(ByVal start As DateTime, ByVal days As Integer) As DataTable
      Dim dt As New DataTable()

      Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("Blueprint").ToString())

      Dim cmd As New SqlCommand
      cmd.CommandType = CommandType.StoredProcedure
      cmd.CommandText = "spGetEventDetails"
      cmd.Parameters.AddWithValue("@start", start)
      cmd.Parameters.AddWithValue("@end", start.AddDays(days))
      cmd.Parameters.AddWithValue("@userid", ddlUser.SelectedValue)

      cmd.Connection = conn

      Using da As New SqlDataAdapter(cmd)
         conn.Open()
         da.Fill(dt)
         conn.Close()
      End Using

      Return dt
   End Function

我的SQL过程:

CREATE PROCEDURE spGetEventDetails
(
    @start AS DATETIME,
    @end AS DATETIME,
    @userid AS INT
)
AS
BEGIN

SET @start = CONVERT(CHAR(10), CAST(@start AS DATETIME), 23)
SET @end = CONVERT(CHAR(10), CAST(@end AS DATETIME), 23)

    -- GET DETAILS FOR TICKETS
    SELECT
        [ev].[id]                           AS [EventID],
        CAST([tck].[TicketID] AS NVARCHAR)  AS [TicketID],
        [tck].[Description]                 AS [Description],
        [ev].[eventstart]                   AS [Start],
        [ev].[eventend]                     AS [End],
        [ev].[status]                       AS [Status],
        [ev].[type]                         AS [EventType],
        CAST([tck].[Type] AS NVARCHAR)      AS [Type],
        CAST([tck].[Product] AS NVARCHAR)   AS [Product],
        CAST([tck].[Severity] AS NVARCHAR)  AS [Severity],
        [ev].[resource_id]                  AS [resource_id],
        CAST(pers.FirstName AS NVARCHAR)    AS [FirstName],
        CAST(pers.Surname AS NVARCHAR)      AS [Surname],
        CAST(tck.LogDate AS NVARCHAR)       AS [LogDate]
    FROM
        tblSupportEvent ev
    JOIN
        tblTicketsInEvents tie
    ON
        ev.id =tie.eventID
    JOIN
        SupportTicketsTbl tck
    ON
        tie.ticketID = tck.TicketID 
    JOIN 
        PersonnelTbl pers
    ON
        pers.ID = tck.LoggedBy 
    WHERE
        ev.type = 1
    AND 
        NOT (([eventend] <= @start) OR ([eventstart] >= @end))  
    AND  
        resource_id <> 0  
    AND   
        ev.resource_id = @userid  

UNION 
    -- GET THE DETAILS FOR NON TICKET ITEMS

    SELECT
        [ev].[id]           AS [EventID],
        ''                  AS [TicketID],
        [nti].[Description] AS [Description],
        [ev].[eventstart]   AS [Start],
        [ev].[eventend]     AS [End],
        [ev].[status]       AS [Status],
        [ev].[type]         AS [EventType],
        ''                  AS [Type],
        ''                  AS [Product],
        ''                  AS [Severity],
        [ev].[resource_id]  AS [resource_id],
        ''                  AS [FirstName],
        ''                  AS [Surname],
        ''                  AS [LogDate]
    FROM
        tblSupportEvent ev
    JOIN
        tblNonTicketsInEvents nte
    ON
        ev.id = nte.eventID 
    JOIN
        tblNonTicketItems nti
    ON
        nte.nonTicketID = nti.id    
    WHERE
        ev.type = 2
    AND
        NOT (([eventend] <= @start) OR ([eventstart] >= @end))  
    AND  
        resource_id <> 0  
    AND   
        ev.resource_id = @userid  
END

我无法看到这个错误可能来自哪里,测试在SQL管理工作室中运行它没有错误我很确定错误是在VB中。

我的第一个想法是,我在@start / @end值中传递的参数可能未被识别为DateTime,所以我尝试添加如下参数:

 Dim p As New SqlParameter

  p.SqlDbType = SqlDbType.DateTime
  p.ParameterName = "@start"
  p.Value = start
  cmd.Parameters.Add(p)

但这只会给我相同的结果。如果有人能够建议我哪里出错,那就感激不尽。

由于

1 个答案:

答案 0 :(得分:1)

登录语言可能不同。

set dateformat mdy

declare @start datetime = '20110130'

SET @start = CONVERT(CHAR(10), CAST(@start AS DATETIME), 23)

作品。但set dateformat dmy没有。

要提取date的{​​{1}}部分,您可以使用datetime