Hi
I want to load data from a flat file into a SQL table. The file has a header row and detail rows, all of which need to be loaded. I've tried various suggestions I came across in forums and EndOfStream is the one that loaded all the rows. The problem is that the data gets truncated. Here's a sample of the data:
H,01.00,00001,Header row text
D,2000/10/25,1 ,Detail row info 1 , 1.00,uom ,000000001,N, 1.70 00 14.000 +0000000015 +6.15 +25.50 +0000000056 +22.96 +95.20 +0000000660 +270.60 +1122.00
D,2000/10/25,2 ,Detail row info 2 , 1.00,uom ,000000002,N, 4.99 01 0.000 +0000000003 +7.50 +14.97 +0000000008 +20.00 +39.92 +0000000070 +175.00 +459.30
Note that the length of both the header and detail rows is the same, the header just has more trailing spaces. This is the code I'm using read the file and insert the date into a SQL table:
public void Main()
{
string FileName = @"C:\TextFile.DAT";
using (StreamReader sr = new StreamReader(FileName))
{
int counter = 0;
const string connStr = "Data Source=servername;Initial Catalog=sqldatabase;Integrated Security=True;";
const string query = "Insert Into Table_1 (FileName, DateImported, Imported_Text) Values (@FileName, GETDATE(), @Imported_Text)";
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.Add(new SqlParameter("@FileName", SqlDbType.NVarChar)).Value = FileName;
cmd.Parameters.Add(new SqlParameter("@Imported_Text", SqlDbType.NVarChar));
while (!sr.EndOfStream)
{
cmd.Parameters["@Imported_Text"].SqlValue = sr.ReadLine().ToString();
cmd.ExecuteNonQuery();
counter++;
}
}
conn.Close();
}
}
Dts.TaskResult = (int)ScriptResults.Success;
}
The header row loads fine but the detail rows only load up to a certain point, with the rows in the sample only loading up to 1.70 and 4.99 respectively. No error is raised so I can't figure out what causes truncation. As a test I deleted the header row, ran the Script Task and the data loaded perfectly. As soon as I add the header row back to the file there is truncation. Could it be the fact that the truncated columns are not delimited like the rest of the columns? I haven't been able to find a solution to the mixed delimeters issue, hence I'm now loading everything into one column for the time being instead of separate columns. If I can just solve the truncation problem I can then work on spltting the data to columns. Any help will be greatly appreciated.