我坚持使用一个程序,也许我正在测试它太久而且遗漏了一些东西
无论如何,我有一个循环将数据上传到数据库。我想使用参数,因为数据是excel表中的用户输入
正在上传三种类型的数据 - 字符串,单个,日期
问题在于单个数据类型值。
Dim ProjectList As Variant
Dim Value1 as Long, Value2 as Long
ProjectList = Range(Cells(12, 2), Cells(LastRow, "BZ")).Value
With cmdProject
.ActiveConnection = cnn
.CommandText = "INSERT INTO tblProject ([Field1], [Field2], [Field3], [Field4], [Field5], [Field6], [Field7], " & _
"[Field8], [Field9], [Field10], [Field11], [Field12], [Field13], [Field14], [Field15], " & _
"[Field16], [Field17], [Field18], [Field19], [Field20], [Field21], [Field22], [Field23], [Field24], " & _
"[Field25], [Field26], [Field27], [Field28], [Field29], [Field30], [Field31]) " & _
"Values ('" & Value1 & "', '" & Value2 & "', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "
.Parameters.Append .CreateParameter(, adBigInt, adParamInput)
.Parameters.Append .CreateParameter(, adBigInt, adParamInput)
.Parameters.Append .CreateParameter(, adBigInt, adParamInput)
.Parameters.Append .CreateParameter(, adBigInt, adParamInput)
.Parameters.Append .CreateParameter(, adBigInt, adParamInput)
.Parameters.Append .CreateParameter(, adBigInt, adParamInput)
.Parameters.Append .CreateParameter(, adBigInt, adParamInput)
.Parameters.Append .CreateParameter(, adBigInt, adParamInput)
.Parameters.Append .CreateParameter(, adBigInt, adParamInput)
.Parameters.Append .CreateParameter(, adChar, adParamInput, 255)
.Parameters.Append .CreateParameter(, adChar, adParamInput, 255)
.Parameters.Append .CreateParameter(, adChar, adParamInput, 255)
.Parameters.Append .CreateParameter(, adChar, adParamInput, 255)
.Parameters.Append .CreateParameter(, adChar, adParamInput, 255)
.Parameters.Append .CreateParameter(, adChar, adParamInput, 255)
.Parameters.Append .CreateParameter(, adChar, adParamInput, 255)
.Parameters.Append .CreateParameter(, adChar, adParamInput, 255)
.Parameters.Append .CreateParameter(, adChar, adParamInput, 255)
.Parameters.Append .CreateParameter(, adSingle, adParamInput)
.Parameters.Append .CreateParameter(, adSingle, adParamInput)
.Parameters.Append .CreateParameter(, adSingle, adParamInput)
.Parameters.Append .CreateParameter(, adSingle, adParamInput)
.Parameters.Append .CreateParameter(, adSingle, adParamInput)
.Parameters.Append .CreateParameter(, adDate, adParamInput)
.Parameters.Append .CreateParameter(, adDate, adParamInput)
.Parameters.Append .CreateParameter(, adDate, adParamInput)
.Parameters.Append .CreateParameter(, adDate, adParamInput)
.Parameters.Append .CreateParameter(, adDate, adParamInput)
.Parameters.Append .CreateParameter(, adDate, adParamInput)
End With
For i = 1 To UBound(ProjectList)
With cmdProject
.Parameters.Item(0).Value = CLng(ProjectList(i, 4))
.Parameters.Item(1).Value = CLng(ProjectList(i, 5))
.Parameters.Item(2).Value = CLng(ProjectList(i, 6))
.Parameters.Item(3).Value = CLng(ProjectList(i, 9))
.Parameters.Item(4).Value = CLng(ProjectList(i, 14))
.Parameters.Item(5).Value = CLng(ProjectList(i, 17))
.Parameters.Item(6).Value = CLng(ProjectList(i, 19))
.Parameters.Item(7).Value = CLng(ProjectList(i, 20))
.Parameters.Item(8).Value = CLng(ProjectList(i, 21))
.Parameters.Item(9).Value = CStr(ProjectList(i, 7))
.Parameters.Item(10).Value = CStr(ProjectList(i, 8))
.Parameters.Item(11).Value = CStr(ProjectList(i, 10))
.Parameters.Item(12).Value = CStr(ProjectList(i, 11))
.Parameters.Item(13).Value = CStr(ProjectList(i, 12))
.Parameters.Item(14).Value = CStr(ProjectList(i, 13))
.Parameters.Item(15).Value = CStr(ProjectList(i, 15))
.Parameters.Item(16).Value = CStr(ProjectList(i, 16))
.Parameters.Item(17).Value = CStr(ProjectList(i, 18))
.Parameters.Item(18).Value = CSng(ProjectList(i, 22))
.Parameters.Item(19).Value = CSng(ProjectList(i, 23))
.Parameters.Item(20).Value = CSng(ProjectList(i, 24))
.Parameters.Item(21).Value = CSng(ProjectList(i, 25))
.Parameters.Item(22).Value = CSng(ProjectList(i, 29))
.Parameters.Item(23).Value = CDate(Format(ProjectList(i, 30), "YYYY-MM-DD"))
.Parameters.Item(24).Value = CDate(Format(ProjectList(i, 31), "YYYY-MM-DD"))
.Parameters.Item(25).Value = CDate(Format(ProjectList(i, 32), "YYYY-MM-DD"))
.Parameters.Item(26).Value = CDate(Format(ProjectList(i, 33), "YYYY-MM-DD"))
.Parameters.Item(27).Value = CDate(Format(ProjectList(i, 34), "YYYY-MM-DD"))
.Parameters.Item(28).Value = CDate(Format(ProjectList(i, 35), "YYYY-MM-DD"))
.Execute
End With
我在.execute
上收到错误
运行时错误-2147217913(80040e07)
标准表达式中的数据类型不匹配
数据库表有31个字段(Access accdb)
字段1到11 - 数据类型编号 - 字段大小长整数
字段12-20 - 数据类型文本 - 字段大小255
字段21-25 - 数据类型编号 - 字段大小单精度
字段26-31 - 数据类型日期/时间
单个数据类型值都设置为1,5
当我省略具有单个数据类型值的参数并在sql字符串中添加一些值时,它正在工作。
什么是有趣的第一次循环上传数据确定所以1行总是上传,它在第二次崩溃。
当我进行调试时,在.execute
之前的第二次运行中,我转到即时窗口并放置cmdProject.Parameters.Item(10).Type = adsingle
它也会上传但是下一行只有一行有错误。
我可以在不使用参数的情况下轻松运行此过程,所有数据都以我想要的方式上传。
此外,我可以上传这样的数据,但我不想在每个循环中创建新命令
For i = 1 To UBound(ProjectList)
Set cmdProject = New ADODB.Command
With cmdProject
.ActiveConnection = cnn
.CommandText = "INSERT INTO tblProject ([Field1], [Field2], [Field3], [Field4], [Field5], [Field6], [Field7], " & _
"[Field8], [Field9], [Field10], [Field11], [Field12], [Field13], [Field14], [Field15], " & _
"[Field16], [Field17], [Field18], [Field19], [Field20], [Field21], [Field22], [Field23], [Field24], " & _
"[Field25], [Field26], [Field27], [Field28], [Field29], [Field30], [Field31]) " & _
"Values ('" & Value1 & "', '" & Value2 & "', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "
.Parameters.Append .CreateParameter(, adBigInt, adParamInput, , CLng(ProjectList(i, 4)))
.Parameters.Append .CreateParameter(, adBigInt, adParamInput, , CLng(ProjectList(i, 5)))
.Parameters.Append .CreateParameter(, adBigInt, adParamInput, , CLng(ProjectList(i, 6)))
.Parameters.Append .CreateParameter(, adBigInt, adParamInput, , CLng(ProjectList(i, 9)))
.Parameters.Append .CreateParameter(, adBigInt, adParamInput, , CLng(ProjectList(i, 14)))
.Parameters.Append .CreateParameter(, adBigInt, adParamInput, , CLng(ProjectList(i, 17)))
.Parameters.Append .CreateParameter(, adBigInt, adParamInput, , CLng(ProjectList(i, 19)))
.Parameters.Append .CreateParameter(, adBigInt, adParamInput, , CLng(ProjectList(i, 20)))
.Parameters.Append .CreateParameter(, adBigInt, adParamInput, , CLng(ProjectList(i, 21)))
.Parameters.Append .CreateParameter(, adChar, adParamInput, 255, CStr(ProjectList(i, 7)))
.Parameters.Append .CreateParameter(, adChar, adParamInput, 255, CStr(ProjectList(i, 8)))
.Parameters.Append .CreateParameter(, adChar, adParamInput, 255, CStr(ProjectList(i, 10)))
.Parameters.Append .CreateParameter(, adChar, adParamInput, 255, CStr(ProjectList(i, 11)))
.Parameters.Append .CreateParameter(, adChar, adParamInput, 255, CStr(ProjectList(i, 12)))
.Parameters.Append .CreateParameter(, adChar, adParamInput, 255, CStr(ProjectList(i, 13)))
.Parameters.Append .CreateParameter(, adChar, adParamInput, 255, CStr(ProjectList(i, 15)))
.Parameters.Append .CreateParameter(, adChar, adParamInput, 255, CStr(ProjectList(i, 16)))
.Parameters.Append .CreateParameter(, adChar, adParamInput, 255, CStr(ProjectList(i, 18)))
.Parameters.Append .CreateParameter(, adSingle, adParamInput, , CSng(ProjectList(i, 22)))
.Parameters.Append .CreateParameter(, adSingle, adParamInput, , CSng(ProjectList(i, 23)))
.Parameters.Append .CreateParameter(, adSingle, adParamInput, , CSng(ProjectList(i, 24)))
.Parameters.Append .CreateParameter(, adSingle, adParamInput, , CSng(ProjectList(i, 25)))
.Parameters.Append .CreateParameter(, adSingle, adParamInput, , CSng(ProjectList(i, 29)))
.Parameters.Append .CreateParameter(, adDate, adParamInput, , CDate(Format(ProjectList(i, 30), "YYYY-MM-DD")))
.Parameters.Append .CreateParameter(, adDate, adParamInput, , CDate(Format(ProjectList(i, 31), "YYYY-MM-DD")))
.Parameters.Append .CreateParameter(, adDate, adParamInput, , CDate(Format(ProjectList(i, 32), "YYYY-MM-DD")))
.Parameters.Append .CreateParameter(, adDate, adParamInput, , CDate(Format(ProjectList(i, 33), "YYYY-MM-DD")))
.Parameters.Append .CreateParameter(, adDate, adParamInput, , CDate(Format(ProjectList(i, 34), "YYYY-MM-DD")))
.Parameters.Append .CreateParameter(, adDate, adParamInput, , CDate(Format(ProjectList(i, 35), "YYYY-MM-DD")))
.execute
End With
这种方式没有问题。
我认为单个数据类型参数在向它们分配值时必定是错误的,但我看不出有什么问题。