asp_databases thread: CSV read failing.
All,
I am in the process of creating a csv to SQL import ASP page, it'll
take any csv file and import it into a database with the name of the table
being the csv filename. The csv's can have any number of fields. Some of
them have rows that do not have the name number of fields:
Data, data, data
Data, data, data, data, data
Data, data
I am having a problem getting the data out of the csv. I can get the maximum
number of columns (in the case above:5), so I can create the table first,
then loop through the csv and insert the data to the new table.I can get the
number of columns using .count , but it always seems to lop off the last
column of the longest one. The first records in the csv are shorter than the
middle records, but it gets the fields for the ones in between (also greater
than the first), but misses the last column of the longest one. Here's the
code:
sql = "SELECT * FROM " & filename
set rs = conn.execute(sql)
set columns=rs.Fields
colcount = columns.count
tblcreate = "CREATE TABLE " & left(UCASE(filename), len(filename)-4) & " ("
for i = 0 to colcount
tblcreate = tblcreate & "field" & i+1 & " nvarchar(50),"
next
tblcreate = left(tblcreate,len(tblcreate)-1)
tblcreate = tblcreate & ")"
Response.Write tblcreate
'Print out the contents of our recordset
Do While Not rs.EOF
i = 0
for i = 0 to colcount - 1
fields = fields & "field" & i + 1 & ","
values = values & "'" & columns(i) & "',"
next
fields=left(fields,len(fields)-1)
values=left(values,len(values)-1)
Response.Write "<br>"
Response.Write "INSERT INTO " & filename & " (" & fields & ") VALUES
(" & values & ")"
Response.Write "<br>"
fields = ""
values = ""
rs.MoveNext 'Move to the next record
Loop
I am building an insert statement at the moment and am piping it to the
screen for debugging purposes.
Let me know if it needs further explanation
Brandon Ludwig
Brandon.Ludwig@M...