I have a CSV file that has about 4 million rows of clean data in it, and I need to import it into a SQL Server 2008 R2 database.
I tried to do this via TSQL only, SSIS, and few other methods, but in the end I always encountered the same problem, each row has a 2,000+ columns. SQL server had a problem with reading that many columns in a CSV file.
So, I ended up writing a little C# console program to parse the csv file and do all the work to break out the 2,000+ columns into their relational tables in my database. Everything is working great, but with the CSV file having 4 million rows, efficiency is key here.
All that said, here is the problem I am having, and can't for the life me figure out. I am using the old SqlCommand class to do bulk inserts. I can't figure out how to get a null value into the db. For instance, take this line
var PreparedCommand = "insert into person_modeling_info (person_id, modeling_info, response) values ";
PreparedCommand += "( " + info[0] + ",'" + info[1] + "'," + (item2.Value == "" ? "'NULL'" : "'"+item2.Value+"'") + "),";
I am not worried about SQL injection, as I am 100% sure that the data is clean. I know I can insert null using parameters, but parameters are slower as I have to insert one record at a time vs. doing a bulk insert like
insert into table (values), (values), (values)
When I perform the insert using concatenation, it gets inserted into the database as an empty string.
If any one has an ideas, any help would be greatly appreciated.
NULLwithout quotes should do. Of course, injection issue stands as is."'NULL'"produces the string NULL, remove the single quotes as its a DB keyword