By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,364 Members | 1,272 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,364 IT Pros & Developers. It's quick & easy.

DataTable.Select performance

P: n/a
Luc
I am using Visual Studio 2003 and am getting lousy performance after using
a datatable select and then trying to assign a value to a column of the row
that was found:

DataTable dt = new DataTable();
dt.Columns.Add("Number", System.Type.GetType("System.Int32"));
dt.Columns.Add("Value", System.Type.GetType("System.Double"));

for(int i=0;i<2000;i++)
{
DataRow dr = dt.NewRow();
dr["Number"] = i;
dr["Value"] = 1.0;
dt.Rows.Add(dr);
}

for(int i=0;i<2000;i++)
{
// use the select
string filter = "Number = " + i;
DataRow[] rows = dt.Select(filter);

// assign a new value to the row that was found
if(rows.Length > 0) rows[0]["Value"] = 2;
}

I want to note that this is only if both a select and assign are used
together. Performance is fine if one is used without the other. Anyone
encounter this and know of a workaround?
Jul 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Luc,
I want to note that this is only if both a select and assign are used
together. Performance is fine if one is used without the other. Anyone
encounter this and know of a workaround?
I would expect updating a value in a DataTable to take a certain amount of
time. As the previous values need to be saved & the new values set. Remember
that a DataRow actually contains upto 4 versions of the row based on the
DataRowVersion enum.

Interesting enough, if you define a primary key on the Number column (the
column you are selecting on) then the select & update goes about as fast as
just Select. As defining an "index" (Primary Key or DataView) on a column
will make Select go faster, as Select will use this "index".
dt.Columns.Add("Number", System.Type.GetType("System.Int32"));
dt.PrimaryKey = new DataColumn[] {dt.Columns[Number]}

Alternatively you can use a DataView & FindRows if the column you are
selecting on is not the primary key column.

Hope this helps
Jay

"Luc" <Lu*@discussions.microsoft.com> wrote in message
news:17**********************************@microsof t.com...I am using Visual Studio 2003 and am getting lousy performance after using
a datatable select and then trying to assign a value to a column of the
row
that was found:

DataTable dt = new DataTable();
dt.Columns.Add("Number", System.Type.GetType("System.Int32"));
dt.Columns.Add("Value", System.Type.GetType("System.Double"));

for(int i=0;i<2000;i++)
{
DataRow dr = dt.NewRow();
dr["Number"] = i;
dr["Value"] = 1.0;
dt.Rows.Add(dr);
}

for(int i=0;i<2000;i++)
{
// use the select
string filter = "Number = " + i;
DataRow[] rows = dt.Select(filter);

// assign a new value to the row that was found
if(rows.Length > 0) rows[0]["Value"] = 2;
}

I want to note that this is only if both a select and assign are used
together. Performance is fine if one is used without the other. Anyone
encounter this and know of a workaround?

Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.