我有一个字符串变量,它填充了日历中的日期,格式为 29/07/2012
我想将其转换为日期时间数据类型,然后以这种格式 2012-07-29 00:00:00:000 将其存储在 sql 数据库中
The error message i'm getting at the minute is:
"The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value."
这是一些代码:
Dim strDate As String = ""
Dim cmd2 As New SqlCommand("sp_Return6MonthTotal")
Dim strTotalHours6Month As Integer = 0
cmd2.Connection = con1
cmd2.CommandType = Data.CommandType.StoredProcedure
cmd2.Parameters.Add(New SqlParameter("@ServiceNumber", Data.SqlDbType.Char, 11))
cmd2.Parameters.Add(New SqlParameter("@OvertimeDate2", Data.SqlDbType.DateTime))
cmd2.Parameters("@ServiceNumber").Value = strServiceNumber
cmd2.Parameters("@OvertimeDate2").Value = strDate
If cmd2.ExecuteScalar() Is DBNull.Value Then
Label17.Text = "0"
Label21.Text = "0"
Else
strTotalHours6Month = cmd2.ExecuteScalar()
Label17.Text = strTotalHours6Month
Label21.Text = Math.Round(strTotalHours6Month / 6)
End If
Dim cmd3 As New SqlCommand("sp_ReturnMonthTotal")
Dim strTotalHours As Integer = 0
cmd3.Connection = con1
cmd3.CommandType = Data.CommandType.StoredProcedure
cmd3.Parameters.Add(New SqlParameter("@ServiceNumber", Data.SqlDbType.Char, 11))
cmd3.Parameters.Add(New SqlParameter("@OvertimeDate2", Data.SqlDbType.DateTime))
cmd3.Parameters("@ServiceNumber").Value = strServiceNumber
cmd3.Parameters("@OvertimeDate2").Value = strDate
Stored procedures:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_ReturnMonthTotal]
@ServiceNumber varchar(100),
@OvertimeDate2 datetime
AS
SELECT sum(hoursworked) from overtime where servicenumber = @ServiceNumber and month(CONVERT(datetime, OvertimeDate2, 103)) = month(CONVERT(datetime, @OvertimeDate2, 103)) and year(CONVERT(datetime, OvertimeDate2, 103)) = year(CONVERT(datetime, @OvertimeDate2, 103))
-------------
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_Return6MonthTotal]
@ServiceNumber varchar(100),
@OvertimeDate2 datetime
AS
SELECT sum(hoursworked) from overtime where servicenumber = @ServiceNumber and overtimedate2 >= DATEADD(month, -6, CONVERT(datetime, @OvertimeDate2, 103))
对不起,我的编程知识不是很好,我没有编写代码,所以希望这对某人有意义。