I have a bunch of 2 line (with header row) ‘|’ delimited text files. I need to import this into a specific SQL table and I’m having a hard time with the command.
string sqltable = ("dbo.SLT_C" + "60" + "Staging");
string[] importfiles= Directory.GetFiles(@"K:\jl\load\dest", "*.txt")
SqlConnection con = new SqlConnection("Data Source=" + "Cove" + ";Initial Catalog=" + "GS_Ava_MCase"+ ";Integrated Security=" + "SSPI");
con.Open();
foreach (string importfile in importfiles)
{
}
or maybe I am going about this the whole wrong way.
You could look at a ready-made solution, like FileHelpers. This FREE library allows you to define the structure of your file by means of a class describing the fields in your file, and then you can easily load the whole file into an array of that class type.
Once that’s done, just simply iterate through the objects, and save them to your SQL Server.
Or check out the SQL Bulkcopy options:
If you want to do it in “straight” ADO.NET, use something like this approach:
That should work – you’re question was too vague to know exactly what data will be in the lines, and what kind of SQL insert statement you’d need…