SQL Server 2008中的datetime数据类型支持毫秒。
我正在尝试执行一个接受datetime参数的存储过程,其中带有一个毫秒的arg作为输入/输出值。
我无法将传入的字符串转换为datetime值的方法。当我没有传入毫秒值时,转换正确。
我在下面的方法中看到转换问题。
Set objpara2 = objCom.CreateParameter("datetime", adDate, adParamInputOutput, , "2011-01-01 11:01:01.123")
ADO中SQL Server 2008的datetime的等效数据类型是什么?
以下是完整的代码段:
CREATE PROCEDURE [dbo].[TestProc] @time1 time, @datetime datetime output
as
begin
SET NOCOUNT ON;
select @datetime = datetime from ALLTimeTypes where time = @time1;
end
Private Sub Command1_Click()
Dim objCon As ADODB.connection
Dim objCom As ADODB.command
Dim objPara As ADODB.Parameter
Dim objpara2 As ADODB.Parameter
Dim objRS As ADODB.Recordset
Dim k As Integer
Set objCon = New ADODB.connection
Set objCom = New ADODB.command
objConConnectionString = "Provider=SQLNCLI10;" _
& "Data Source=ES-89W87BS;" _
& "Database=MASTER;" _
& "Integrated Security=SSPI;" _
& "DataTypeCompatibility=80;" _
& "User ID=sa;" _
& "Password=<redacted>;"
objCon.ConnectionString = objConConnectionString
objCon.Open
MsgBox "Connection opened"
With objCom
.CommandText = "TestProc" 'Name of the stored procedure
.CommandType = adCmdStoredProc 'Type : stored procedure
.ActiveConnection = objCon.ConnectionString
End With
Set objPara = objCom.CreateParameter("time1", adVarChar, adParamInput, 50, "02:02:02.3456123")
Set objpara2 = objCom.CreateParameter("datetime", adDate, adParamInputOutput, , "2011-01-01 11:01:01")
objCom.Parameters.Append objPara
objCom.Parameters.Append objpara2
Set objRS = objCom.Execute
objRS.Open
Do While Not objRS.EOF
For k = 0 To objRS.Fields.Count - 1
Debug.Print objRS(k).Name & ": " & objRS(k).Value
Next
objRS.MoveNext
Loop
...
答案 0 :(得分:2)
根据this,adDBTimeStamp
答案 1 :(得分:2)
显然ADO正在消除毫秒。 http://support.microsoft.com/kb/246438
我没有尝试过这个,但我认为您应该将存储过程中参数的数据类型更改为varchar(23)并使用类似于'2011-05-17T10:18:54.293'
的字符串参数。
答案 2 :(得分:1)
在日期和时间组件之间的字符串值中包含T
时会发生什么情况?
(如2011-01-01T11:01:01.123
)
Set objpara2 = objCom.CreateParameter("datetime", adDate, adParamInputOutput,,
"2011-01-01T11:01:01.123")