Click here to Skip to main content
15,891,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys,
I have a .CSV file with this content:

1732,Juan Perez,435.00,11-05-2002
554,Pedro Gomez,12342.30,06-02-2004
112,Ramiro Politti,0.00,01-02-2000
924,Pablo Ramirez,3321.30,24-11-2002

I have managed to read and insert all the records into a DataTable in C#, now I need to generate a TSQL INSERT script for this table along with the DATA in it.

any suggestions?
Posted

Hi,

Why you want to create INSERT script for each record? you can use BULK INSERT for single call of DataBase.

See the following Example
SQL
BULK
INSERT CSVTest
FROM 'c:\csvtest.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO


Thanks,
Imdadhusen
 
Share this answer
 
Comments
Mastersev 7-Mar-11 2:15am    
I know it is possible with SQL server but I need to export the INSERT script along with the Table data in C#, I need the C# code.
Sunasara Imdadhusen 7-Mar-11 2:18am    
What??
Mastersev 7-Mar-11 2:28am    
Imagine a DataTable that is filled with data, I need the TSQL script that creates that TABLE and INSERTS the data in SQL server.
 
Share this answer
 
I had same required a month before. I think for your requirement solution 1 fits best. Because you dont have and reference in these records.

But if you have reference in records then you can use this logic.

First divide whole file to record seperator. ie \n here
then for each array index use for loop and again seperate into record delimeter. ie , here

and create datatable and call your insert method.

It will all I guess.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900