You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Excel中的date类型数据导入SQL Server表?代码无报错但未插入

问题排查与解决方法

1. 移除On Error Resume Next,暴露真实错误

这条语句会跳过所有错误,导致你无法看到代码执行中的隐性问题(比如日期参数不匹配、SQL语法错误等)。直接删除或注释掉该语句,执行时会弹出错误提示,快速定位问题根源。

2. 处理空表时MAX(id)返回Null的情况

如果SQL表testyt是空的,MAX(id)会返回Null,此时tableRange.Cells(i,1).Value > maxId的判断会始终为False,导致没有数据被插入。修改获取maxId的代码:

Dim maxIdResult As Variant
maxIdResult = con.Execute(maxIdSql).Fields(0).Value
maxId = IIf(IsNull(maxIdResult), 0, maxIdResult)

当表为空时,maxId会被设为0,Excel中id大于0的行都会被纳入插入范围。

3. 修正日期参数传递方式

Excel单元格的日期显示格式不影响实际值(编辑栏的显示是系统区域设置导致的),无需用Format转换为字符串。adDBDate类型应直接传递Excel的日期序列值,让ADO自动完成类型转换。同时,date是SQL Server保留字,需要用方括号括起来避免语法冲突:

  • 修改SQL语句:
    Sql = "INSERT INTO testyt (id, name, number, [date]) VALUES (?, ?, ?, ?)"
    
  • 修改日期参数绑定:
    cmdInsert.Parameters.Append cmdInsert.CreateParameter("Param4", 133, 1, , tableRange.Cells(i, 4).Value) ' adDBDate, adParamInput
    

4. 验证条件判断逻辑

在循环中添加调试输出,确认哪些行满足id > maxId的条件:

Debug.Print "Current id: " & tableRange.Cells(i, 1).Value & ", maxId: " & maxId

打开VBA立即窗口(Ctrl+G)查看输出,确认判断逻辑是否符合预期。

修改后的完整代码

Sub InsertNewRowsToSqlServer()
  ' Set up the SQL Server connection
  Dim con As Object
  Set con = CreateObject("ADODB.Connection")
  con.Open "driver={sql server};server=DESKTOP-F3OGEH0\VE_SERVER;database=test;uid=sa;pwd=Cotherm123;"

  ' Check if the connection is successful
  If con.State <> 1 Then
    MsgBox "Error connecting to SQL Server.", vbExclamation
    Exit Sub
  End If

  ' Specify the Excel data range (starting from A1)
  Dim ws As Worksheet
  Set ws = ActiveSheet
  Dim lastRow As Long
  lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

  ' Retrieve the maximum value of 'id' in the existing SQL Server table
  Dim maxId As Long
  Dim maxIdSql As String
  maxIdSql = "SELECT MAX(id) FROM testyt"
  Dim maxIdResult As Variant
  maxIdResult = con.Execute(maxIdSql).Fields(0).Value
  maxId = IIf(IsNull(maxIdResult), 0, maxIdResult)

  Dim tableRange As Range
  Set tableRange = ws.Range("A1:D" & lastRow)
   
  Dim i As Long
  For i = 2 To tableRange.Rows.Count
    Debug.Print "Current id: " & tableRange.Cells(i, 1).Value & ", maxId: " & maxId
    If tableRange.Cells(i, 1).Value > maxId Then
      Dim Sql As String
      Sql = "INSERT INTO testyt (id, name, number, [date]) VALUES (?, ?, ?, ?)"
       
      Dim cmdInsert As Object
      Set cmdInsert = CreateObject("ADODB.Command")
      cmdInsert.ActiveConnection = con
      cmdInsert.CommandText = Sql
      cmdInsert.CommandType = 1 ' adCmdText
      
      cmdInsert.Parameters.Append cmdInsert.CreateParameter("Param1", 3, 1, , tableRange.Cells(i, 1).Value)
      cmdInsert.Parameters.Append cmdInsert.CreateParameter("Param2", 200, 1, 50, tableRange.Cells(i, 2).Value)
      cmdInsert.Parameters.Append cmdInsert.CreateParameter("Param3", 3, 1, , tableRange.Cells(i, 3).Value)
      cmdInsert.Parameters.Append cmdInsert.CreateParameter("Param4", 133, 1, , tableRange.Cells(i, 4).Value)

      cmdInsert.Execute
    End If
  Next i

  con.Close
  MsgBox "Data added to SQL Server!"
End Sub

内容的提问来源于stack exchange,提问作者malak77

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 06:04:53