There are no integrity issues, as each table is dealt with in order of integrity rules. tblProductTypes, then tblCategories, and finally tblProducts.
Okay, I'll post the code to help out a bit.
Also, the database I'm using is an Access 2000 db, using an ADO connection.
CODE:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"[URL unfurl="true"]http://www.w3.org/TR/REC-html40/loose.dtd">[/URL]
<!-- METADATA TYPE="typelib" FILE="C:\Program Files\Common Files\System\ado\msado15.dll" -->
<%
'Set text file name for text streaming object
Const sFile = "WebUpdates.txt"
'Set for total number of tables in website db
Const iNoOfTables = 3
'Change MaxFields to reflect changes in fields imported (+1)
'IE: current number of fields (Nov 2001): tblProdTypes (5), tblCategories(9), tblProducts (15)
Const MaxFields = 15
%>
<!--#include file="getdatafile.asp" -->
<HTML>
<HEAD>
<TITLE>Importing Website Data...</TITLE>
</HEAD>
<BODY>
<!--#include file="openconn.asp" -->
<H1 class="center"><BR />Importing Website Data...</H1>
<%
'Data table import order - tblProdTypes, tblCategories, tblProducts
Call ImportData()
'Close and reset database connection object
oConn.Close
Set oConn = Nothing
%>
<H2 class="center">IMPORT COMPLETE!<BR /><BR /></H2>
<%
Public Sub ImportData()
Const sDelimeter = ","
Const StartReplace = 1
Const ReplaceCount = 1
Dim oRS, sRecord, sTable
Dim iTable, iArrayLen, iNoOfFields, iField
Dim sFindString, sTableFieldHeads
Dim bolModify
Dim sFieldName(), sFieldValue()
ReDim sFieldName(MaxFields), sFieldValue(MaxFields)
Set oRS=Server.CreateObject("ADODB.Recordset")
'Loop around for the total number of tables in website db
For iTable = 1 to iNoOfTables
Response.Write "</TABLE><BR /><BR />"
If oTextStream.AtEndOfStream Then Exit For
'If second time round main loop extract next table name and close old HTML table
If sRecord & "" = "" Then
sTable = oTextStream.ReadLine()
Else
sTable = sRecord
End If
'Open recordset and populate with data
oRS.Open sTable, oConn, adOpenStatic, adLockBatchOptimistic
oRS.MoveLast
oRS.MoveFirst
'Read next line for table field name
sRecord = oTextStream.ReadLine()
'Last line in file is always empty
If sRecord & "" = "" Then Exit For
iArrayLen = MaxFields - 1
iNoOfFields = MaxFields
sTableFieldHeads = ""
'Extract Field Names and output to HTML table-heads
For iField = 0 To iArrayLen
'Test for end of fields
If Not sRecord & "" = "" Then
sFieldName(iField) = ExtractFieldValue(sRecord, sDelimeter)
sRecord = Replace(sRecord, sFieldName(iField) & sDelimeter, "", StartReplace, ReplaceCount)
sTableFieldHeads = sTableFieldHeads & "<TH class=left>" & sFieldName(iField) & "</TH>"
Else
'Total number of fields less than array length
If iNoOfFields = MaxFields Then iNoOfFields = iField
End If
Next
Response.Write "<TABLE cols=" & iNoOfFields+1 & " cellspacing=0 cellpadding=3>"
Response.Write "<TR><TH class=center colspan=" & iNoOfFields+1 & ">Modifying Table: " & sTable & "</TH></TR><TR>"
Response.Write sTableFieldHeads
Response.Write "<TH class=left>Action Result</TH>"
Response.Write "</TR>"
'Loop through text file until end of file or new table name discovered
Do Until oTextStream.AtEndOfStream
'Get first line of table data
sRecord = oTextStream.ReadLine()
If Left(sRecord, 3) = "tbl" Then Exit Do
Response.Write "<TR>"
'Extract Field Values
For iField = 0 To iNoOfFields - 1 'This stops the LEFT command falling over at end of record (in ExtractFieldValue Function)
sFieldValue(iField) = ExtractFieldValue(sRecord, sDelimeter)
sRecord = Replace(sRecord, sFieldValue(iField) & sDelimeter, "", StartReplace, ReplaceCount)
sFieldValue(iField) = Trim(sFieldValue(iField))
Response.Write "<TD class=left>" & sFieldValue(iField) & "</TD>"
Next
'Build recordset find string
If IsNumeric(sFieldValue(0)) Then
sFindString = sFieldName(0) & " = " & sFieldValue(0)
Else
sFindString = sFieldName(0) & " LIKE '" & sFieldValue(0) & "'"
End If
Response.Write "Find String: " & sFindString & "<BR />"
'Find record
oRS.Find sFindString
'Modify Table entries
If oRS.Eof Then
If sFieldValue(iNoOfFields - 1) = "MOD" Then
'Add record
bolModify = True
oRS.AddNew
oRS(sFieldName(0)) = sFieldValue(0)
Response.Write "<TD class=left>Added</TD>"
Else
'Record doesn't exist - cannot delete
bolModify = False
Response.Write "<TD class=left>No Record</TD>"
End If
Else
If sFieldValue(iNoOfFields - 1) = "MOD" Then
'Modify record
bolModify = True
Response.Write "<TD class=left>Edited</TD>"
Else
'Delete record
bolModify = False
oRS.Delete
Response.Write "<TD class=left>Deleted</TD>"
End If
End If
'Only run this if add or edit
If bolModify Then
'Set all other field values (add & edit)
For iField = 1 To (iNoOfFields - 2)
Response.Write sFieldName(iField) & ": " & sFieldValue(iField) & "(" & iField & ")<BR />"
If sFieldValue(iField) = "Null" Then
If IsNumeric(oRS(sFieldName(iField))) Then
oRS(sFieldName(iField)) = Empty
Else
oRS(sFieldName(iField)) = ""
End If
Else
Response.Write "Modifying Field: " & sFieldName(iField) & " = " & sFieldValue(iField) & "<BR />"
oRS(sFieldName(iField)) = sFieldValue(iField)
End If
Next
oRS.Update
End If
'Reset recordset pointer
Response.Write "After Update<BR />"
' oRS.MoveLast
' oRS.MoveFirst
Response.Write "</TR>"
Loop
oRS.Close
Next
'Clear down recordset object
Set oRS = Nothing
End Sub
Public Function ExtractFieldValue(sRecordData, sDelimeter)
Dim iDelimeterLoc
iDelimeterLoc = CInt(Instr(sRecordData, sDelimeter))
' Response.Write sRecordData & "<BR />Delimeter: " & sDelimeter & " - Delimeter Location: " & iDelimeterLoc
ExtractFieldValue = Left(sRecordData, iDelimeterLoc - 1)
' Response.Write ExtractFieldValue & "<BR />"
End Function
%>
</BODY>
</HTML>
Told you it was massive! Exapmle of CSV data file below:
[tt]tblCategories
DesignID,Design,ProdTypeID,Details,DishwasherSafe,MicrowaveSafe,OvenToTable,Discontinued,WebUploadAction,
11 ,Infinity, 1 ,Fine china/porcelain.<BR /><BR /><B>20 piece sets contain:</B><BR />4 Dinner Plates, 4 Tea Plates, 4 Soup/Cereal Bowls, 4 Tea Cups, and 4 Tea Saucers.,False,False,False,True,MOD,
13 ,Linton Gold, 1 ,Fine china/porcelain.<BR /><BR /><B>20 piece sets contain:</B><BR />4 Dinner Plates, 4 Tea Plates, 4 Soup/Cereal Bowls, 4 Tea Cups, and 4 Tea Saucers.,False,False,False,False,MOD,
17 ,Sapphire, 1 ,Fine china/porcelain.<BR /><BR /><B>20 piece sets contain:</B><BR />4 Dinner Plates, 4 Tea Plates, 4 Soup/Cereal Bowls, 4 Tea Cups, and 4 Tea Saucers.,False,False,False,True,MOD,
18 ,Sherwood, 1 ,Fine china/porcelain.<BR /><BR /><B>20 piece sets contain:</B><BR />4 Dinner Plates, 4 Tea Plates, 4 Soup/Cereal Bowls, 4 Tea Cups, and 4 Tea Saucers.,False,False,False,True,MOD,
27 ,Kensington Blue, 1 ,Fine china/porcelain.<BR /><BR /><B>20 piece sets contain:</B><BR />4 Dinner Plates, 4 Tea Plates, 4 Soup/Cereal Bowls, 4 Tea Cups, and 4 Tea Saucers.,False,False,False,True,MOD,
28 ,Kensington Green, 1 ,Fine china/porcelain.<BR /><BR /><B>20 piece sets contain:</B><BR />4 Dinner Plates, 4 Tea Plates, 4 Soup/Cereal Bowls, 4 Tea Cups, and 4 Tea Saucers.,False,False,False,True,MOD,
29 ,Kensington Red, 1 ,Fine china/porcelain.<BR /><BR /><B>20 piece sets contain:</B><BR />4 Dinner Plates, 4 Tea Plates, 4 Soup/Cereal Bowls, 4 Tea Cups, and 4 Tea Saucers.,False,False,False,True,MOD,
39 ,Silver Garland, 1 ,Fine china/porcelain.<BR /><BR /><B>20 piece sets contain:</B><BR />4 Dinner Plates, 4 Tea Plates, 4 Soup/Cereal Bowls, 4 Tea Cups, and 4 Tea Saucers.,False,False,False,False,MOD,
40 ,Café, 2 ,Pure white, vitrified porcelain. Ideal for restaurants and café's.<BR /><BR />See <A href='prodlist.asp?id=65'>Arctic</A> for more items.,True,True,True,False,MOD,
tblProducts
ItemCode,DesignID,OurDescription,RetailPrice,RelItem1,RelItem2,RelItem3,RelItem4,RelItem5,RelItem6,RelItem7,RelItem8,RelItem9,RelItem10,WebUploadAction,
AC10ES, 100 ,Vase Mini - Blue & White - Trees (Singharaja), 20 ,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,MOD,
AC10L, 100 ,Vase Large - Blue & White - Trees (Singharaja), 40 ,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,MOD,
AC10M, 100 ,Vase Medium - Blue & White - Trees (Singharaja), 33 ,AC10ES,AC10S,AC10L,Null,Null,Null,Null,Null,Null,Null,MOD,
AC10S, 100 ,Vase Small - Blue & White - Trees (Singharaja), 27 ,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,MOD,
AC13ES, 100 ,Vase Mini - Light Green - Wheat (Hillside), 20 ,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,MOD,
AC13L, 100 ,Vase Large - Light Green - Wheat (Hillside), 40 ,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,MOD,
AC13M, 100 ,Vase Medium - Light Green - Wheat (Hillside), 33 ,AC13ES,AC13S,AC13L,Null,Null,Null,Null,Null,Null,Null,MOD,
AC13S, 100 ,Vase Small - Light Green - Wheat (Hillside), 27 ,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,MOD,
AC16A, 100 ,Kettle - Metallic Black - Model A (Dhanu), 30 ,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,MOD,
AC16B, 100 ,Kettle - Metallic Black - Model B (Dhanu), 30 ,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,MOD,[/tt]
(tblProductTypes not shown - import.asp falls over at third product item from tblProducts) Ahdkaw
"Get that ST away from me!!!"
Where the sensible gather... or so they say.