我已经了解了如何通过another question将CSV导入Access数据库。但是在answer中没有关于如何在导入时定位特定列的信息。如何确保CSV中的正确列放入数据库中的正确列?
我需要能够在使用经典ASP 导入时编辑/添加列值。例子:
我正在处理一些旧代码和DB是我没有创建的。相信我,如果可以的话,我会使用别的东西,所以请不要使用MS Access。
我想这整件事情归结为:
我已将所有内容设置为相信应该如何,但我收到错误:
ADODB.Recordset error '800a0bb9'
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
/datazone_bkup/Rollover/importTeachers.asp, line 12
第12行:CSV.open "SELECT * FROM teachers.csv", conn
我的完整代码如下:
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &_
Server.MapPath("\path\to\file") & ";Extended Properties='text;HDR=no;FMT=Delimited';"
Set connCSV = Server.CreateObject("ADODB.Connection")
connCSV.Open strConn
Set CSV = Server.CreateObject("ADODB.recordset")
CSV.open "SELECT * FROM teachers.csv", conn
do until CSV.eof
UserName = CSV.Fields(4)
LastName = CSV.Fields(1)
FirstName = CSV.Fields(2)
MiddleName = CSV.Fields(3)
School = CSV.Fields(5)
if CSV.Fields(6) = "Teacher" then
SecLevel = "Classroom"
end if
Active = "Yes"
TeacherNumber = rsCSV.Fields(0)
rsCSV.movenext
sql = "INSERT INTO tblTeacher (UserName, LastName, FirstName, MiddleName, School, SecLevel, Active, TeacherNumber) " &_
"VALUES (" &_
"'" & UserName & "','" & LastName & "','" & FirstName & "','" & MiddleName & "'," &_
"'" & School & "','" & SecLevel & "','" & Active & "','" & TeacherNumber & "'" &_
")"
on error resume next
conn.execute(sql)
loop
@HansUp ,当我在!
中尝试使用rsDB!UserName
时,我收到了Syntax Error
。所以我回去为CSV列值设置变量。
再次感谢您的帮助。
答案 0 :(得分:1)
与您之前链接的问题一样,我们假设您已使用CSV内容打开了ADO记录集。您可以为Access目标表打开第二个ADO记录集。
如果CSV文件包含列名,则可以遍历其recorset行并将其值插入目标记录集。如果CSV记录集是rsCSV并且目标表的recorset是rsDB ...
Do While Not rsCSV.EOF
rsDB.AddNew
rsDB!userid = rsCSV!userid & "@domain.com"
If rsCSV!classification = "Teacher" Then
rsDB!classification = "Classroom"
Else
rsDB!classification = rsCSV!classification
End If
rsDB.Update
rsCSV.MoveNext
Loop
如果CSV不包含列名,您仍然可以执行此操作,但需要在字段集合中使用字段的序号位置。这些位置编号从零开始,因此第4列为3,依此类推。
Do While Not rsCSV.EOF
rsDB.AddNew
rsDB!userid = rsCSV.Fields(3) & "@domain.com"
If rsCSV.Fields(5) = "Teacher" Then
rsDB!classification = "Classroom"
Else
rsDB!classification = rsCSV.Fields(5)
End If
rsDB.Update
rsCSV.MoveNext
Loop