开发者

ADO.Net - Truncate Column Data When DataRow is Added to DataTable

开发者 https://www.devze.com 2023-04-05 17:48 出处:网络
This is my first post on Stackoverflow. I am reading millions of rows from a flat file (comma delimited) and iterating each read row and then each column of each row.The 开发者_如何学编程iteration of

This is my first post on Stackoverflow.

I am reading millions of rows from a flat file (comma delimited) and iterating each read row and then each column of each row. The 开发者_如何学编程iteration of each column is to allow user defined conversions, defaults, removal of special characters, etc. to be performed. The current implementation is very efficient.

The reading of the data is done in batches of 20k. When I'm processing a read row, I issue a NewRow() call on my in-memory DataTable. I then start iterating each column to scrub their values. I'm trying to minimize as much as I can when I'm processing a rows columns.

My problem is this. If the value (text in this case) that is read from the flat file is longer than the MaxLength of the targeted DataTables DataColumn, I receive an exception stating such when I issue the following:

dataTable.Rows.Add(newRow);

Is there a way to tell ADO.Net (or my in-memory DataTable) to truncate the data instead of complaining?

Again, I can easily add logic in the loop to do this check/truncation for me, but those things add up when you're dealing with millions of rows of data.


something like this should work:

var newRow = dataTable.NewRow();

...
...

if(YourText.Length < ColumnMaxLength)
{
  newRow["YourLimitedColumnName"] = YourText;
}
else
{
  newRow["YourLimitedColumnName"] = YourText.Substring(0, ColumnMaxLength);
}

...
...

dataTable.Rows.Add(newRow);
0

精彩评论

暂无评论...
验证码 换一张
取 消