我有一个我写的存储过程的问题,我可以在management studio中调用它,它工作得很好,但是当我调用da.Fill(dt)时,从我的代码后台调用它时会出现这个错误
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
。
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
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]
tblSupportEvent ev
tblTicketsInEvents tie
ev.id =tie.eventID
SupportTicketsTbl tck
tie.ticketID = tck.TicketID
PersonnelTbl pers
pers.ID = tck.LoggedBy
WHERE
ev.type = 1
NOT (([eventend] <= @start) OR ([eventstart] >= @end))
resource_id <> 0
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]
tblSupportEvent ev
tblNonTicketsInEvents nte
ev.id = nte.eventID
tblNonTicketItems nti
nte.nonTicketID = nti.id
WHERE
ev.type = 2
NOT (([eventend] <= @start) OR ([eventstart] >= @end))
resource_id <> 0
ev.resource_id = @userid
END