I have a tab separated text file which is about 1.2 GB , i need to convert it into CSV file(Comma Separated) using c#. I have to insert a bulk data into sqlserver, the data is i
Import the tab separated file into Excel (or another spreadsheet program) then export it as CSV.
If its SQl Server 2005 and above, right click the database you want to import to and select Tasks and then Import Data. Point to your tab delimited file and just go through the remaining screens. On the final screen, just before you actually import, you have an option to save it as a SSIS package(check box). This is by far the easiest, fast and efficient way (without the conversion from tab delimited to csv) to import it without writing any code and the code is also available at the end of the wizard, which can be used to automate the task, if required.
The BULK INSERT command in SQL Server 2005 will use a tab as a field delimiter by default so there is no need to convert a tab delimited text file to csv format.
In fact, converting it to a real csv will make things more difficult. BULK INSERT will not respect quotes as a string delimiter so a true csv file that has strings encapsulated by quotes with commas somewhere in the string will break the import.
See https://msdn.microsoft.com/en-us/library/ms188365(v=sql.90).aspx for more information.
I came up with this which splits on the tab character:
using(System.IO.StreamReader rdr = new System.IO.StreamReader(@"C:\text.txt"))
{
int counter = 0;
string lne;
while((lne = rdr.ReadLine()) != null)
{
string[] temp = lne.Split('\t');
Console.WriteLine(temp[0]);
Console.WriteLine(temp[1]);
counter++;
}
}
Console.ReadLine();
After that you can just use a StringBuilder to concatenate the the items in the array with a comma.
If you are using sqlserver, you should have a look at SSIS (SqlServer integration Services). This tool will help you to import your csv file in a efficent manner. You can configure your seperator in this tool, so there is no need to replace the tabs with comma's.