我有一个需要循环的 CSV,获取每行的 ID,然后循环通过数据库,将 csvID 与每个 dbID 进行比较。如果 ID存在于数据库中,它将使用 CSV 中的相关信息更新记录。
但是,我陷入了无限循环(据我所知),不知道如何摆脱它。
Option Explicit
Server.ScriptTimeout = 2147483647
dim conn, rs, updatedUser, updatedDate, filePath
dim deactivateSQL, csvConn, connCSV, csv, sql
dim dbID, dbSSN, dbLast, dbFirst, dbMiddle, dbGender, dbScl, dbCls
dim csvID, csvSSN, csvLast, csvFirst, csvMiddle, csvGender
dim csvScl, csvCls, csvGrd, csvHrm
updatedUser = Request.Cookies("UserN")
updatedDate = date() & " " & time()
filePath = "\path\to\file"
' Connect to Students.CSV
csvConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &_
Server.MapPath(filePath) &_
";Extended Properties='text;HDR=no;FMT=Delimited';"
Set connCSV = Server.CreateObject("ADODB.Connection")
connCSV.Open csvConn
Set csv = Server.CreateObject("ADODB.recordset")
csv.open "SELECT * FROM Students.csv", connCSV
temp = csv.RecordCount
redim toAdd(temp)
' Begin looping through Students.csv
do until csv.eof
' Get Students.csv Column Values
' please disregard the "replace" stuff for now
csvID = replace(replace(csv.fields(0), " ", ""), "'", "")
csvSSN = replace(replace(csv.fields(1), " ", ""), "'", "")
csvLast = replace(replace(csv.fields(2), " ", ""), "'", "")
csvFirst = replace(replace(csv.fields(3), " ", ""), "'", "")
csvMiddle = replace(replace(csv.fields(4), " ", ""), "'", "")
csvGender = replace(replace(csv.fields(5), " ", ""), "'", "")
csvScl = replace(replace(csv.fields(6), " ", ""), "'", "")
csvGrd = replace(replace(csv.fields(7), " ", ""), "'", "")
csvHrm = replace(replace(csv.fields(8), " ", ""), "'", "")
' Connect to database
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "E:/path/to/file/database.mdb"
set rs=Server.CreateObject("ADODB.Recordset")
rs.open "SELECT * FROM tblStudent", conn
' Begin looping through tblStudents
do until rs.eof
' Get tblStudents.StudentID
dbID = rs.fields("StudentID")
dbSSN = rs.fields("SSN")
dbLast = rs.fields("LastName")
dbFirst = rs.fields("FirstName")
dbMiddle = rs.fields("MiddleName")
dbGender = rs.fields("Gender")
dbScl = rs.fields("School")
dbCls = rs.fields("Class")
if dbID = csvID then
' if dbID matches csvID,
' update tblStudents with the new CSV data
sql = "UPDATE tblStudent SET " &_
"Active='Yes' AND " &_
"SSN='" & csvSSN & "' AND " &_
"LastName='" & csvlast & "' AND " &_
"FirstName='" & csvFirst & "' AND " &_
"MiddleName='" & csvMiddle & "' AND " &_
"Gender='" & csvGender & "' AND " &_
"School='" & csvScl & "' AND " &_
"GradeLvl='" & csvGrd & "' AND " &_
"HomeRoomID='" & csvHrm & "' AND " &_
"PrevClass1='" & dbCls & "' AND" &_
"lastUpdatedUser='" & updatedUser & "' AND" &_
"lastUpdatedDate='" & updatedDate & "'" &_
"WHERE StudentID=" & dbID & ";"
on error resume next
conn.execute(sql)
else
' I am not sure what to do here...
' I thought about creating a dynamic array:
' adding to the array for each ID not found
' however, I am not THAT skilled.
' If someone could help me with that,
' I would be grateful
end if
rs.movenext
loop
csv.movenext
loop
' This is the INSERT SQL I need to execute,
' but do not exactly know where it needs to be placed either
sql = "INSERT INTO tblStudent (" &_
"Active, StudentID, SSN, LastName, FirstName, MiddleName, Gender, "&_
"School, GradeLvl, HomeRoomID, lastUpdatedUser, LastUpdatedDate" &_
") VALUES (" &_
"'Yes', '" & csvID & "', '" & csvSSN & "', '" & csvLast & "', '" &_
csvFirst & "', '" & csvMiddle & "', '" & csvGender & "', '" &_
csvScl & "', '" & csvGrd & "', '" & csvHrm & "', '" &_
updatedUser & "', '" & updatedDate & _
"');"
on error resume next
conn.execute(sql)
if error<>0 then
response.cookies("updated") = "no"
response.cookies("updated").Expires = dateadd("s", 2, now())
response.redirect("step-5.asp")
else
response.cookies("updated") = "yes"
response.cookies("updated").Expires = dateadd("s", 2, now())
response.redirect("step-6.asp")
end if
这甚至可能不是最好的方法,我也愿意接受这里的建议。但是,首先我需要完成这项工作:循环遍历 CSV,如果数据库中存在 csvID,则更新数据库,如果不存在则插入 csvID 行信息。
//更新
感谢Richard Benson,我已经能够让我的代码在大多数情况下正常工作:我已经挂断了这段代码:
csvLast = replace(csv.fields(2), "'", "")
csvFirst = replace(csv.fields(3), "'", "")
if csv.fields(4) <> NULL then
csvMiddle = replace(csv.fields(4), "'", "")
else
csvMiddle = csv.fields(4)
end if
该replace()
功能适用于名字和姓氏,但是当我到达中间名时,它就不起作用了。如果我保留它csvMiddle = replace(csv.fields(4), "'", "")
本身,它有时会出错,因为中间名字段有时是空的。我怎样才能让它正常工作?这很可能是这段代码顺利运行之前的最后一个问题。