在SQL Server的日期时间中插入dd / mm / yyyy hh:mm:ss.000

时间:2019-05-03 09:29:58

标签: sql sql-server excel vba datetime

我在Excel中具有d/m/yyy hh:mm:ss.000格式的时间戳,并将其发送到SQL Server中的日期时间数据类型。但是,当我传输数据时,它已经正确传输了,但是没有毫秒,每个数据最后都有.000。在Excel中,我具有正确的毫秒日期。有人有解决方案吗?

2 个答案:

答案 0 :(得分:0)

Sub Sectie1Invert()

Dim Cn As ADODB.Connection
Dim ServerName As String
Dim DatabaseName As String
Dim TableName As String
Dim UserID As String
Dim Password As String
Dim rs As ADODB.Recordset
Dim rs1 As ADODB.Recordset
Dim RowCounter As Long
Dim NoOfFields As Integer
Dim StartRow As Long
Dim EndRow As Long
Dim ColCounter As Integer
Dim trigger As Boolean

Set rs = New ADODB.Recordset

ServerName = "NLDONL0113" ' Enter your server name here
DatabaseName = "Stroomwaarden" ' Enter your  database name here
TableName1I = "Sectie1invert" ' Enter your Table name here
 UserID = "" ' Enter your user ID here
Password = "" ' Enter your password here
NoOfFields = 1 ' Enter number of fields to update (eg. columns in your worksheet)
StartRow = 3 ' Enter row in sheet to start reading  records
EndRow = 349 ' Enter row of last record in sheet

Dim shtSheetToWork As Worksheet
Set shtSheetToWork = ActiveWorkbook.Worksheets("Sheet1")
 '********

Set Cn = New ADODB.Connection
Cn.Open "Driver={SQL Server};Server=" & ServerName & ";Database=" & DatabaseName & _
";Uid=" & UserID & ";Pwd=" & Password & ";"
rs.Open TableName1I, Cn, adOpenKeyset, adLockOptimistic

For RowCounter = StartRow To EndRow
   rs.AddNew
    'On Error Resume Next

     rs(0) = shtSheetToWork.Cells(RowCounter, 13)
    rs(3) = shtSheetToWork.Cells(RowCounter, 14)

    Debug.Print RowCounter
Next RowCounter

    rs.UpdateBatch
    rs.Close
    Set rs = Nothing
    Cn.Close
    Set Cn = Nothing
End Sub

答案 1 :(得分:0)

在插入db之前,您可以获得单元格的文本和格式:

Dim strValue as String
strValue=shtSheetToWork.Cells(RowCounter, 13).Text

如果是strValue类型,请在插入数据库rs(0)之前再次格式化datetime