使用VBA将整个excel表导出到sql表

时间:2017-07-06 04:56:11

标签: sql-server excel vba excel-vba

有问题将整张表发送到sql表。我尝试了两种不同的方法,如下所示。两者都以不同的方式工作。最终,我正在努力将多行数据导出到sql DB

第一种方法我只需单击一下按钮即可成功将多行导入sql表,唯一的问题是即使我在excel中输入正常时间,StartTime和FinishTime也会以00:00:00的形式进入sql表/ p>

Sub Button1_Click()
  Dim conn As New ADODB.Connection
  Dim iRowNo As Integer
  Dim sEventDate, sID, sDeptCode, sOpCode, sStartTime, sFinishTime, sUnits  As String

  With Sheets("Sheet1")
    'Open a connection to SQL Server
    conn.Open "Provider=SQLOLEDB;Data Source=db\db1;Initial Catalog=PTW;Integrated Security=SSPI;"
    'Skip the header row
    iRowNo = 2
    'Loop until empty cell in FirstName
    Do Until .Cells(iRowNo, 1) = ""
    sEventDate = .Cells(iRowNo, 1)
    sID = .Cells(iRowNo, 2)
    sDeptCode = .Cells(iRowNo, 3)
    sOpCode = .Cells(iRowNo, 4)
    sStartTime = .Cells(iRowNo, 5).Text
    sFinishTime = .Cells(iRowNo, 6).Text
    sUnits = .Cells(iRowNo, 7)
    'Generate and execute sql statement to import the excel rows to SQL Server table
    conn.Execute "insert into dbo.TimeLog (EventDate, ID, DeptCode, Opcode, StartTime, FinishTime, Units) values ('" & sEventDate & "', '" & sID & "', '" & sDeptCode & "', '" & sOpCode & "', cast('" & dStartTime & "' as datetime), cast('" & dFinishTime & "' as datetime), '" & sUnits & "')"

    iRowNo = iRowNo + 1

    Loop

    MsgBox "Data Successfully Exported."
    conn.Close
    Set conn = Nothing
  End With
End Sub

第二种方式工作并发送StartTime和FinishTime,就像它在excel表中一样,但它只允许我一次向sql表发送一行

Sub Button1_Click()
  Dim conn As ADODB.Connection
  Dim cmd As ADODB.Command
  Dim strSQL As String

  strSQL = "INSERT INTO dbo.TimeLog" & _
     "(EventDate, ID, DeptCode, Opcode, StartTime, FinishTime, Units) " & _
     "VALUES (?,?,?,?,?,?,?)"

Set conn = New ADODB.Connection
  conn.Open "Provider=SQLOLEDB;Data Source=db\db1;Initial Catalog=PTW;Integrated Security=SSPI;"

iRowNo = 1

Set cmd = New ADODB.Command

  cmd.ActiveConnection = conn
  cmd.CommandType = adCmdText
  cmd.CommandText = strSQL



With Sheets("Sheet1")
  'Loop until empty cell in FirstName
   Do Until .Cells(iRowNo, 1) = ""

  cmd.Parameters.Append _
    cmd.CreateParameter("pEventDate", adVarChar, adParamInput, 8, .Cells(iRowNo, 1))
  cmd.Parameters.Append _
    cmd.CreateParameter("pID", adInteger, adParamInput, , .Cells(iRowNo, 2))
  cmd.Parameters.Append _
    cmd.CreateParameter("pDeptCode", adVarChar, adParamInput, 2, .Cells(iRowNo, 3))
  cmd.Parameters.Append _
    cmd.CreateParameter("pOpCode", adVarChar, adParamInput, 2, .Cells(iRowNo, 4))
  cmd.Parameters.Append _
    cmd.CreateParameter("pStartTime", adDBTime, adParamInput, 0, .Cells(iRowNo, 5))
  cmd.Parameters.Append _
    cmd.CreateParameter("pFinishTime", adDBTime, adParamInput, 0, .Cells(iRowNo, 6))
  cmd.Parameters.Append _
    cmd.CreateParameter("pUnits", adInteger, adParamInput, , .Cells(iRowNo, 7))

  cmd.Execute


  iRowNo = iRowNo + 1
  Loop

  MsgBox "Data Successfully Exported"

End With

  conn.Close
  Set conn = Nothing
End Sub

2 个答案:

答案 0 :(得分:0)

你必须施放价值。在mssql中,yourt时间值被识别为字符串。

施放('8:00:00'作为日期时间)

conn.Execute "insert into dbo.TimeLog (EventDate, ID, DeptCode, Opcode, StartTime, FinishTime, Units) values ('" & sEventDate & "', '" & sID & "', '" & sDeptCode & "', '" & sOpCode & "', cast('" & dStartTime & "' as datetime), cast('" & dFinishTime & "' as datetime), '" & sUnits & "')"

答案 1 :(得分:0)

在第一个脚本的循环中添加这个是我正在寻找的结果......

    Set cmd = New ADODB.Command
    cmd.ActiveConnection = conn
    cmd.CommandType = adCmdText
    cmd.CommandText = strSQL