Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSV (or sheet in XLS) to SQL create (and insert) statements with .Net?

Tags:

.net

sql

csv

xls

Does anyone have a technique for generating SQL table create (and data insert) commands pragmatically from a CSV (or sheet in a .xls) file?

I've got a third party database system which I'd like to populate with data from a csv file (or sheet in a xls file) but the importer supplied can't create the table structure automatically as it does the import. My csv file has lots of tables with lots of columns so I'd like to automate the table creation process as well as the data importing if possible but I'm unsure about how to go about generating the create statement...

like image 490
Danielb Avatar asked Dec 14 '25 21:12

Danielb


2 Answers

In SQL server it is as easy as

SELECT * INTO NewTablenNmeHere
FROM OPENROWSET( 'Microsoft.Jet.OLEDB.4.0', 
'Excel 8.0;Database=C:\testing.xls','SELECT * FROM [Sheet1$]') 
like image 93
SQLMenace Avatar answered Dec 17 '25 11:12

SQLMenace


I recommend having a look at csvkit. Its csvsql function can generate table insert statements, or even execute them for you, from most tabular data sources.

like image 38
wombat Avatar answered Dec 17 '25 11:12

wombat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!