ADO中SQL Server 2008的datetime的等效数据类型是什么?

时间:2022-09-16 13:09:15

The datetime datatype in SQL Server 2008 supports milliseconds.

SQL Server 2008中的datetime数据类型支持毫秒。

I am trying to execute a stored procedure that accepts a datetime parameter, with an arg with milliseconds, as input/output value.

我正在尝试执行一个接受datetime参数的存储过程,其中arg为毫秒,作为输入/输出值。

I am unable to convert the string that I pass in to the method as datetime value. When I do not pass in the millisecond values, the conversion happens correctly.

我无法将传入的字符串转换为datetime值的方法。当我没有传入毫秒值时,转换正确。

I see the conversion problem in the below method.

我在下面的方法中看到了转换问题。

Set objpara2 = objCom.CreateParameter("datetime", adDate, adParamInputOutput, , "2011-01-01 11:01:01.123")

What is the equivalent datatype for datetime of SQL Server 2008 in ADO?

ADO中SQL Server 2008的datetime的等效数据类型是什么?

Here is the complete code snippet:

以下是完整的代码段:

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        
    ...

3 个解决方案

#1


2  

According to this, adDBTimeStamp

根据这个,adDBTimeStamp

#2


2  

Apparently ADO is removing the milliseconds. http://support.microsoft.com/kb/246438

显然ADO正在消除毫秒。 http://support.microsoft.com/kb/246438

I have not tried this but I think you should change the data type for your parameter in the stored procedure to varchar(23) and use a string parameter that looks like this '2011-05-17T10:18:54.293'.

我没有尝试过,但我认为您应该将存储过程中参数的数据类型更改为varchar(23)并使用类似于“2011-05-17T10:18:54.293”的字符串参数。

#3


1  

What happens when you include a T in the string value between the date and time components?
(like 2011-01-01T11:01:01.123)

在日期和时间组件之间的字符串值中包含T时会发生什么? (如2011-01-01T11:01:01.123)

Set objpara2 = objCom.CreateParameter("datetime", adDate, adParamInputOutput,,
 "2011-01-01T11:01:01.123")

#1


2  

According to this, adDBTimeStamp

根据这个,adDBTimeStamp

#2


2  

Apparently ADO is removing the milliseconds. http://support.microsoft.com/kb/246438

显然ADO正在消除毫秒。 http://support.microsoft.com/kb/246438

I have not tried this but I think you should change the data type for your parameter in the stored procedure to varchar(23) and use a string parameter that looks like this '2011-05-17T10:18:54.293'.

我没有尝试过,但我认为您应该将存储过程中参数的数据类型更改为varchar(23)并使用类似于“2011-05-17T10:18:54.293”的字符串参数。

#3


1  

What happens when you include a T in the string value between the date and time components?
(like 2011-01-01T11:01:01.123)

在日期和时间组件之间的字符串值中包含T时会发生什么? (如2011-01-01T11:01:01.123)

Set objpara2 = objCom.CreateParameter("datetime", adDate, adParamInputOutput,,
 "2011-01-01T11:01:01.123")