Need faster data conversion WRT DataColumns

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All,
I have a DataTable, something like 500x45 (45 columns). The Datatable is
a result of a query that I run against my db. I recieve updates to the
DataTable frequently, and I'd like to speed up the performance of my app. The
first thin gI did was to stop referencing columns by name, and instead
reference them by number. This helped a lot. But one thing I'd really like
to get rid of is the calls to .ToString() amd .Parse(). For example, I have
a lot of code like this:

float f = xx; // xx is some rapidly and repeatedly inputted value
DataTable.Row[x][0] = f * float.Parse( DataTable[x][10].ToString() );

I can't cast DataTable[x][10] to a float. What is the fastest way to get a
floating point representation of a particular column value in the above
example, assuming it's string represetation is a valid floating point number?
I hope the answer is not that I have to store these values somewhere else as
floats or doubles, do the arithmetic and assign the result to the DataColumn
member.

Tyson
 
Why not create the columns as floats? That way you would have to unbox the
values, but not create/ parse strings.

Andrew Conrad
Microsoft Corp
 
Hi Andrew,
Well, the DataTable (and columns) are the result of this rough sequence:

DataTable dt = new DataTable("Table1");
SqlDataAdapter da = new SqlDataAdapter( "query string...", cn );
da.Fill( dt );

So the columns are created for me, and hence the DataColumn.DataType is
specified already. I don't think I can set the DataColumn.DataType to
"System.Double" and get a free cast (assuming prepopulated data can be casted
correctly) can I? Wishful thinking, but I'll try it.

Another idea: Can I create and add the DataColumn that will hold the result
for that column in the query in advance, and expect the results to be there
as long as the column names match? If so, what's the syntax for "unbox-ing"
the values?

Tyson
 
You should be able to manually create the datatable schema with the correct
datatypes and then call fill:

DataTable dt = new DataTable("Table1");

// add all columns with required datatypes

SqlDataAdapter da = new SqlDataAdapter( "query string...", cn );
da.Fill( dt );

As long as the column names map and the types are compatible, this approach
will work. Note - that for your scenario, this may slow down the fill
operation.
Andrew Conrad
Microsoft Corp
 
Back
Top