CSV ReadRecord event

11 views
Skip to first unread message

Nikky

unread,
Sep 11, 2007, 8:49:38 AM9/11/07
to CSVChat
Hi,

I have developed an ASP .Net application which imports a csv file into
SQL using SQL bulkcopy. The problem is I need to change my csv file
into certain format (like spliting one column into two columns...etc)
before uploading into database. I thought I will read the input csv
file and write it into a new csv file with my changes with the help of
ReadRecord event but couldnt accomplish it.

My input csv file has columns like

County, SubDivision, AddressRange(Ex;114-118)......

Output csv file should have Address range splited into two two columns
AddressTo and AddressFrom

County, SubDivision, AddressTo(Ex; 114), AddressFrom(Ex: 118)

Can you please help me regarding this.

shriop

unread,
Sep 11, 2007, 3:29:51 PM9/11/07
to CSVChat
You won't need to create a new file. You can do what you need to do on
the fly during the middle of the process. Look at this thread,
http://groups.google.com/group/CSVChat/browse_thread/thread/972d75d46da34c24/d1480de47bde8a29?lnk=gst&q=readrecord&rnum=1#d1480de47bde8a29
. Basically, you will want to add two columns that don't exist to the
Columns property of the CsvDataReader instance and name them AddressTo
and AddressFrom. In the SqlBulkCopy ColumnMappings, add mappings for
those columns to their destination columns and do not add a mapping
for the AddressRange column. In the ReadRecord event handler, do
something like this...

e.Values["AddressFrom"] = e.Values["AddressRange"].Split('-')[0];
e.Values["AddressTo"] = e.Values["AddressRange"].Split('-')[1];

That should be all you need to do.

Bruce Dunwiddie

Reply all
Reply to author
Forward
0 new messages