[英]SQL Procedure failing due to Conversion of Varchar To DateTime error
Im having an issue with a Stored Procedure I have written, I can call it in management studio and it works fine, yet calling it from my codebehind gives this error The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
我在编写存储过程时遇到问题,可以在Management Studio中调用它,并且工作正常,但是从我的代码背后调用它会出现此错误
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
when i call da.Fill(dt) 当我打电话给da.Fill(dt)
What is really puzzling me here is that I dont actually convert anything from varchar to datetime, the most I do with the date variables is format them into a different datetime format. 我真正困惑的是,我实际上没有将任何内容从varchar转换为datetime,我对date变量所做的最大工作就是将其格式化为另一种datetime格式。
My VB.Net code 我的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
My SQL Procedure: 我的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
I cant see where this error could be coming from, having tested running it in SQL management studio without error I am pretty sure the error is in the VB. 我测试了在SQL Management Studio中运行它而没有错误的原因,我看不出该错误的出处,我很确定该错误在VB中。
My first thought was that maybe the parameter I am passing in the @start / @end values arent being recognised as DateTime so I tried adding the parameters like: 我的第一个想法是,也许我传入@start / @end值的参数不能被识别为DateTime,所以我尝试添加以下参数:
Dim p As New SqlParameter
p.SqlDbType = SqlDbType.DateTime
p.ParameterName = "@start"
p.Value = start
cmd.Parameters.Add(p)
but this just gives me the same result. 但这给了我相同的结果。 If anyone can suggest where I am going wrong id be gratefull.
如果有人可以提出我要去哪里的错误ID,请万分感谢。
Thanks 谢谢
The language of the logins is probably different. 登录语言可能有所不同。
set dateformat mdy
declare @start datetime = '20110130'
SET @start = CONVERT(CHAR(10), CAST(@start AS DATETIME), 23)
Works. 作品。 But
set dateformat dmy
doesn't. 但是没有
set dateformat dmy
。
To extract the date
part of a datetime
you can use DATEADD(DAY,DATEDIFF(DAY,0,@start),0)
要提取
date
一的一部分, datetime
,你可以使用DATEADD(DAY,DATEDIFF(DAY,0,@start),0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.