मेरे पास एक सीएसवी है जिसके माध्यम से मुझे लूप की आवश्यकता है, प्रत्येक पंक्ति की आईडी प्राप्त करें, फिर डेटाबेस के माध्यम से लूप करें, प्रत्येक डीबीआईडी को सीएसवीआईडी की तुलना करें। यदि डेटाबेस में आईडी मौजूद है, तो यह सीएसवी से प्रासंगिक जानकारी के साथ रिकॉर्ड को अपडेट करेगा।
हालांकि, मैं एक अंतहीन पाश में फंस गया हूं (जो मैं बता सकता हूं) और मुझे यकीन नहीं है कि इससे कैसे निकला जाए।
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
ऐसा करने का यह सबसे अच्छा तरीका भी नहीं हो सकता है और मैं यहां भी सुझावों के लिए तैयार हूं। लेकिन, पहले मुझे यह काम करने की आवश्यकता है: सीएसवी के माध्यम से लूप, डीबी को अपडेट करें यदि सीएसवीआईडी डीबी में मौजूद है और सीएसवीआईडी पंक्ति जानकारी डालें यदि यह मौजूद नहीं है।
//अद्यतन
रिचर्ड बेन्सन को धन्यवाद। मेरे कोड को ठीक से काम करने में सक्षम, अधिकांश भाग के लिए: मैं इस बिट कोड पर लटका हुआ हूं:
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), "'", "")
के रूप में अपने आप में रखता हूं तो यह कभी-कभी त्रुटिपूर्ण हो जाता है क्योंकि मध्य नाम फ़ील्ड कभी-कभी खाली होता है। मैं इसे ठीक से काम करने के लिए कैसे प्राप्त कर सकता हूं? इस कोड के सुचारू रूप से चलने से पहले यह अंतिम समस्या है।
3 जवाब
कोशिश करेंगे और बाद में इसे और अधिक संदर्भ में डाल देंगे, अब के लिए एक सूचक जो मैं करता हूं जब डीबी में नहीं डालने का प्रयास करते समय मैं क्या करता हूं, तो अपडेट करें।
Set rs = Server.CreateObject("ADODB.Recordset")
rs.ActiveConnection = "whateveryourconnectionstringis"
rs.Source = "SELECT * FROM Table WHERE ID = '" & intValue & ";"
rs.CursorType = 2
rs.CursorLocation = 3
rs.LockType = 3
rs.Open()
'If at this point we have no records, it doesnt exist so add it and any data all new records need'
If rs.BOF AND rs.EOF Then
rs.AddNew
rs("ID") = intValue
End If
'Update the rest of the fields
rs("Field1") = Value1
rs("Field2") = Value2
rs("Field3") = Value3
rs.Update()
rs.Close()
Set rs = Nothing
आप कैसे लूपिंग कर रहे हैं और आप कितने लूप से गुजरेंगे, इस पर निर्भर करते हुए, यह बहुत गहन हो सकता है, लेकिन यह कोड के दृष्टिकोण से सबसे सरल है
.BOF
और .EOF
दोनों की जांच करते हैं तो आपके पास कोई रिकॉर्ड नहीं लौटा है, इसलिए आप जिस आईडी की तलाश कर रहे हैं वह मौजूद नहीं है।
ADODB.Recordset
में IF Not rs.EOF Then
का उपयोग करके वर्तमान स्थिति को स्थानांतरित नहीं किया है, तो यह पर्याप्त है।
पहली बात जो मैंने नोटिस की वह यह है कि आप rs.movenext
से पहले loop
करते हैं। इसका मतलब है कि rs.eof
कभी नहीं होगा क्योंकि आप कभी भी rs.movenext
का उपयोग करके आगे नहीं बढ़ते हैं और वास्तव में एक अनंत लूप होगा।
मुझे लगता है कि यह सिर्फ एक टाइपो है, क्योंकि आप इसे बाहरी सीएसवी लूप के लिए सही तरीके से करते हैं।
INSERT
जानकारी का प्रयास करूं।
if dbID = csvID then
ब्लॉक में प्रवेश करता है?
यहां बताया गया है कि मैंने क्या उपयोग किया (यदि कोई अन्य व्यक्ति इस तरह की समस्या का सामना करता है):
आपकी सहायता के लिए सभी का धन्यवाद।
Server.ScriptTimeout = 2147483647
Response.Buffer = False
on error resume next
dim conn1, conn, rs, updatedUser, updatedDate, filePath,
dim deactivateSQL, csvConn, connCSV, csv, sql
dim csvID, csvSSN, csvLast, csvFirst, csvMiddle
dim csvGender, csvScl, csvGrd, csvCls, dbCls
updatedUser = Request.Cookies("UserN")
updatedDate = date() & " " & time()
filePath = "\path\to\file"
' --- Connect to DZ database
set conn1=Server.CreateObject("ADODB.Connection")
conn1.Provider="Microsoft.Jet.OLEDB.4.0"
conn1.Open "E:/path/to/database.mdb"
' --- Deactivate ALL students
deactivateSQL = "UPDATE tblStudent SET Active=False " &_
"AND lastUpdatedUser='" & updatedUser & "' "&_
"AND lastUpdatedDate='" & updatedDate & "';"
conn1.execute(deactivateSQL)
conn1.close
' --- Connect to Students.CSV exported by iNOW
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
' --- Begin looping through Students.csv
do until csv.eof
' --- Get Students.csv Column Values
csvID = csv.fields(0)
if isnull(csv.fields(1)) then
csvSSN = NULL
else
csvSSN = csv.fields(1)
end if
csvLast = replace(csv.fields(2), "'", "")
csvFirst = replace(csv.fields(3), "'", "")
' --- Using IsNull() fixed the 2nd problem
' --- I was having after updating the question
if isnull(csv.fields(4)) then
csvMiddle = csv.fields(4)
else
csvMiddle = replace(csv.fields(4), "'", "")
end if
csvGender = csv.fields(5)
csvScl = csv.fields(6)
csvGrd = csv.fields(7)
csvCls = csv.fields(8)
' --- Connect to database
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "E:/path/to/database.mdb"
set rs=Server.CreateObject("ADODB.Recordset")
rs.open "SELECT * FROM tblStudent " &_
"WHERE StudentID='" & csvID & "';", conn
if rs.bof and rs.eof then
' --- if rs.bof & rs.eof, the csvID is NOT in the table
' --- Add the new csvID to DB, we'll add the rest in
' --- the UPDATE statement below
conn.execute("INSERT INTO tblStudent (StudentID) " &_
"VALUES ('" & csvID & "');")
' --- Set to 0 since it's supposed to be a number
dbCls = 0
else
' --- Get tblStudents.Class from existing record
dbCls = rs.fields("Class")
end if
' --- UPDATE the table with the appropriate info
sql = "UPDATE tblStudent SET " &_
"Active=True, " &_
"SSN=" & csvSSN & ", " &_
"LastName='" & csvlast & "', " &_
"FirstName='" & csvFirst & "', " &_
"MiddleName='" & csvMiddle & "', " &_
"Gender='" & csvGender & "', " &_
"School=" & csvScl & ", " &_
"GradeLvl=" & csvGrd & ", " &_
"Class=" & csvCls & ", " &_
"PrevClass1='" & dbCls & "', " &_
"lastUpdatedUser='" & updatedUser & "', " &_
"lastUpdatedDate='" & updatedDate & "' " &_
"WHERE StudentID='" & csvID & "';"
conn.execute(sql)
csv.movenext
loop
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
संबंधित सवाल
नए सवाल
sql
संरचित क्वेरी भाषा (एसक्यूएल) डेटाबेस को क्वेरी करने के लिए एक भाषा है। प्रश्नों में कोड उदाहरण, तालिका संरचना, नमूना डेटा और DBMS कार्यान्वयन के लिए एक टैग (जैसे MySQL, PostgreSQL, Oracle, MS SQL Server, IBM DB2, आदि) का उपयोग किया जाना चाहिए। यदि आपका प्रश्न केवल एक विशिष्ट DBMS (विशिष्ट एक्सटेंशन / सुविधाओं का उपयोग करता है) से संबंधित है, तो इसके बजाय उस DBMS के टैग का उपयोग करें। एसक्यूएल के साथ टैग किए गए सवालों के जवाब में आईएसओ / आईईसी मानक एसक्यूएल का उपयोग करना चाहिए।
if csv.fields(4) <> NULL then
कोif IsNull(csv.fields(4)) then
से बदल दिया गया है। नीचे मेरा विकी-उत्तर देखें।