problem writing to text file - how to exclude odd chars that cause data row breaks? (using VS2008)

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I need to write large text files (hundreds of thousands of rows long) for a client to import to their sql server, but I am encountering breaks in the data rows which causes errors in the data imports to the sql server. The errors appear to be caused by odd characters, and I need to exclude these chars during the data export from my App. I have included an image of an odd char that my app sees which it is writing to the text file. I am also including the code I am using to write the data to the text file(s). I am using StringBuilder which is incredibly fast, but cannot control the odd chars. I used to loop through the columns of each row (very slow for large data), but that gave me a lot more control. Is there a way I could modify my code to exclude the odd chars?
80f219659833330e53ef3b195cae35f8.png

and here is the code sample I am usingprivate void GetTextFromDataTable(string strPath, DataTable dataTable)
{
int i = 0, j = 0;
var stringBuilder = new StringBuilder();

//--get the header row
stringBuilder.AppendLine(string.Join("|", dataTable.Columns.Cast<DataColumn>().Select(arg => arg.ColumnName).ToArray()));
Application.DoEvents();
foreach (DataRow dataRow in dataTable.Rows)
{
//--only collect 1000 rows at a time to prevent out of memory error
if (i < 1000)
{
stringBuilder.AppendLine(string.Join("|", dataRow.ItemArray.Select(arg => arg.ToString()).ToArray()));
}
else
{
File.AppendAllText(strPath, stringBuilder.ToString());
stringBuilder = new StringBuilder(); //--clear stringBuilder
i = 0;
Application.DoEvents();
tssL1.Text = j.ToString(); //--display row count with j
}
i++;
j++;
}
//--get the last set of rows from the dataTable //--this part not working too good either
File.AppendAllText(strPath, stringBuilder.ToString());
Application.DoEvents();
tssL1.Text = j.ToString();
}Thanks in advance for any suggestions

Rich P

View the full article
 
Back
Top