Slow loading DataRow ItemArray


M

Mark

I'm splitting a csv file into records and then into string fields. Then I
load the string fields into a new DataRow using ItemArray property. I set
the DefaultValue of the fields to the approprate type. This part is very
fast but then I have to load the columns based on the datatype and convert
the string to the correct column datatype and check for string.empty. This
is very slow. Is there a faster way to do this process?

Below is the OnChanging event handler that is slow processing the column
change events. The largest group of fields are strings then numerics a few
dates in yyyyMMdd hh:mm:ss format. I evaluate the database column data type
to know how to convert the string containing the data into the correct data
type.

protected void OnColumnChanging(object sender, DataColumnChangeEventArgs
args)
{
if (args.ProposedValue.ToString() == string.Empty)
{
args.ProposedValue = args.Column.DefaultValue;
return;
}
switch (Type.GetTypeCode(args.Column.DataType))
{
case TypeCode.DateTime:
args.ProposedValue = DateTime.ParseExact(args.ProposedValue.ToString(),
((args.ProposedValue.ToString().Length > 8) ? LONGDATETIMEMASK :
SHORTDATETIMEMASK), CultureInfo.CurrentCulture.DateTimeFormat);
break;
case TypeCode.Int16:
args.ProposedValue = Convert.ToInt16(args.ProposedValue);
break;
case TypeCode.Int32:
args.ProposedValue = Convert.ToInt32(args.ProposedValue);
break;
case TypeCode.Int64:
args.ProposedValue = Convert.ToInt64(args.ProposedValue);
break;
case TypeCode.Decimal:
args.ProposedValue = Convert.ToDecimal(args.ProposedValue);
break;
case TypeCode.Char:
args.ProposedValue = args.ProposedValue.ToString().Substring(0, 1);
break;
case TypeCode.Boolean:
args.ProposedValue =
Convert.ToBoolean(args.ProposedValue.ToString().Substring(0, 1));
break;
}
}
}
 
Ad

Advertisements

A

Andrew Conrad

For one thing, catching the need to format before setting the column value
will be faster then listening to the OnColunmChanging events.
Also, do you really need convert the values when the type is not DateTime.

Another interesting approach might be to adding an expression column which
converts the values as you require.
Andrew Conrad
Microsoft Corp
 
Ad

Advertisements

M

Mark

Could you explain adding an expression column I'm not sure what you mean?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

[.NET1.1] datarow and itemarray 1
ReadXML retrieving ItemArray 1
accessing the ItemArray[] for a row in a dataset 1
DataRow 2
DataRow? 3
DataRow 1
Editing a Datarow 0
Updating a datarow 2

Top