【问题标题】:Blank value is getting passed to Database when importing data from Excel to SQL Server using VBScript使用 VBScript 将数据从 Excel 导入 SQL Server 时,将空白值传递到数据库
【发布时间】:2014-06-25 20:09:59
【问题描述】:

我正在尝试开发一个 vbs 来将数据从 excel 导入到 sql server。 字节值被导入,但在 DB 的 varchar 字段中,Excel 中的字符串值没有被导入(该字段为空)。下面是代码

Option Explicit

Const adOpenStatic = 3
Const adLockOptimistic = 3
dim strSqlInsertString,objConnection2,objRecordSet2
dim objExcel,objWorkBook,strRow

Set objConnection2 = CreateObject("ADODB.Connection")
Set objRecordSet2 = CreateObject("ADODB.Recordset")

objConnection2.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=appapollo;Password=dna;Initial Catalog=6057;Data Source=lxi282"

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open _
    ("D:\Cardiopacs\Automation\Forward\test.xls")

strRow = 2

dim AEName,AEDescription,AEIPAddress,AEPort,ModifiedDate,QRSSApplicationEntityID,MobileAE,NotificationXML,PreFetch,PreFetchSuffix


strSqlInsertString = "INSERT INTO SSDICOMApplicationEntities (AEName,AEDescription,AEIPAddress,AEPort,ModifiedDate,QRSSApplicationEntityID,MobileAE," & _
"NotificationXML,PreFetch,PreFetchSuffix) " & _
"VALUES('" &AEName& "','" &AEDescription& "','" &AEIPAddress& "','" &AEPort& "','" &ModifiedDate& "','" &QRSSApplicationEntityID& "','" &MobileAE& "'," & _
"'" &NotificationXML& "','" &PreFetch& "','" &PreFetchSuffix& "')"

Do Until objExcel.Cells(strRow,1).Value = ""

    'SSDIOMApplicationEntityID = objExcel.Cells(intRow, 1).Value
    AEName = objExcel.Cells(strRow, 1).Value
    AEDescription = objExcel.Cells(strRow, 2).Value
    AEIPAddress = objExcel.Cells(strRow, 3).Value
    AEPort = objExcel.Cells(strRow, 4).Value
    ModifiedDate = objExcel.Cells(strRow, 5).Value
    QRSSApplicationEntityID = objExcel.Cells(strRow, 6).Value
    MobileAE = objExcel.Cells(strRow, 7).Value
    NotificationXML = objExcel.Cells(strRow, 8).Value
    PreFetch = objExcel.Cells(strRow, 9).Value
    PreFetchSuffix = objExcel.Cells(strRow, 10).Value
    strRow = strRow + 1



set objRecordSet2=objConnection2.execute(strSQLInsertString)

loop

objconnection2.close

set objConnection2 = Nothing


objExcel.Quit

【问题讨论】:

  • OLE DB Provide for Excel 不是只检查前 8 条记录来确定列的数据类型吗?

标签: sql excel vbscript


【解决方案1】:

问题是你的代码是做什么的

generate sql command with empty values
do until end
    retrieve values from excel
    execute sql command with empty values
loop

而它应该做的是

do until end
    retrieve values from excel
    generate the new sql command from the retrieved values
    execute the new generated sql command
loop

【讨论】:

  • 是的。将strSqlInsertString 移动到循环中,在Execute() 命令之前,您将获得更好的结果。附带说明...strRow?为什么不intRow? :)
猜你喜欢
  • 2022-12-26
  • 2018-04-17
  • 1970-01-01
  • 2015-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多