Jump to content

csv


prichardson

Recommended Posts

A question on asp csv.I have a current csv that imports into an access db table and that works. I have to state the columns (what will be on the csv file and is the same in the database table) in the code to get it to import the records into the table. I would like to have a more flexible system if possible. I was wondering how to do this. I would like to have the ability to not have a template and have to fill out all the columns on the csv file. For example at the moment i have a csv file that has 10 columns and 10,000 products. If i decide that i want to change one column values for 6 records out of the thousands of records in the db table- i would liek to be able to make a new csv file which will be more simpler and quicker to make up (rather than trying to use the orginal template) and be able to import the 6 records with the one column values changed.Maybe the following times after it might be different amount of records and columns.At the moment I have a csv import that has no flexibility because i have to have all the columns and have to have these columns imprinted on the code. Instead I would like to have a more flexible csv import that allows you to make as many csv files as you like which allows you to have different number of columns each time.Any ideas or code that would be able to achieve this? I thought maybe an if statment or a select case statement to say all the possibilities of the different options of having different columns on the csv... but i think that might be too complicated for that or might end up with a large amount of code. Is there a different way? Is there a way at all?Thanks

Link to comment
Share on other sites

Getting the column names out of the CSV file is relatively simple. Just make it a business decision to include the column titles in the first line of each CSV file. For example, if your table had UserID, FirstName, LastName as the columns, then your CSV file may look like this:

UserID, FirstName, LastName1, Bill, Smith2, Jane, Doe3, George, Orwell
Then, in your code, read the first line from your CSV file, split it up on the commas, and you'll have an array of column names. Not very difficult.The difficult part comes in when you decide about the logic of when to perform UPDATEs and when to perform INSERTs based on the data and how to match the data in the CSV file with the data that is in the database. I'll leave that up to you.
Link to comment
Share on other sites

The difficult part comes in when you decide about the logic of when to perform UPDATEs and when to perform INSERTs based on the data and how to match the data in the CSV file with the data that is in the database. I'll leave that up to you.
This is what I am asking about!I want to be able to Update a record or several from the database and it may just be the 1 or 2 fields out of the 10 fields. The point i was trying to get accross is how do you make sure the csv columns will match up with the database columns?
Link to comment
Share on other sites

Thanks aspnetguy,

if you make the first record the field names of your databse table you can use those to match them up.
How would I do that? The records may have different fields within the database updated at different times, so what code would i write to get them to match up?
Link to comment
Share on other sites

He's saying to put the column names in the CSV file:

username, firstname, lastnameuser1, steve, nashuser2, amare, stoudemireuser3, boris, diawuser4, raja, belluser5, shawn, marion

You would read the column names first and that would tell you which fields to insert in the database. If you have multiple records in one file where each record might go in different fields, then that is complicating things, it would be much better to have all of the records in a single file be the same format, which is the way that CSV is supposed to be set up anyway.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...