Writing Data from DataTable

M

Mick Walker

I am attempting to write some information contained in a datatable to a
textbox on my form.

I do it like this:

// get the number of columns
int intCols = myDataSet.Tables[0].Columns.Count;
// get the number of rows
int intRows = myDataSet.Tables[0].Rows.Count;
foreach (DataRow row in myDataSet.Tables[0].Rows) {
for(int i = 0; i < intCols ; i++) {
textBox2.Text += (string) row + " , ";
}
textBox2.Text += Environment.NewLine;
}

It is taking a VERY long time, over 10 mins for less than 2000 rows.
Does anyone have any siggestions on making this faster?
 
A

Alberto Poblacion

Mick Walker said:
// get the number of columns
int intCols = myDataSet.Tables[0].Columns.Count;
// get the number of rows
int intRows = myDataSet.Tables[0].Rows.Count;
foreach (DataRow row in myDataSet.Tables[0].Rows) {
for(int i = 0; i < intCols ; i++) {
textBox2.Text += (string) row + " , ";
}
textBox2.Text += Environment.NewLine;
}

It is taking a VERY long time, over 10 mins for less than 2000 rows.
Does anyone have any siggestions on making this faster?


I suspect that the problem may be in the string concatenations. Strings
in .Net are immutable, so every time that you change the string it has to be
discarded and a new string allocated instead. These operations are slow. It
should be faster if you use a StringBuilder:

using System.Text;
....
int intCols = myDataSet.Tables[0].Columns.Count;
int intRows = myDataSet.Tables[0].Rows.Count;
StringBuilder sb = new StringBuilder();
foreach (DataRow row in myDataSet.Tables[0].Rows) {
for(int i = 0; i < intCols ; i++) {
sb.Append((string) row + " , ");
}
sb.Append(Environment.NewLine);
}
textBox2.Text = sb.ToString();
 
M

Mick Walker

Alberto said:
Mick Walker said:
// get the number of columns
int intCols = myDataSet.Tables[0].Columns.Count;
// get the number of rows
int intRows = myDataSet.Tables[0].Rows.Count;
foreach (DataRow row in myDataSet.Tables[0].Rows) {
for(int i = 0; i < intCols ; i++) {
textBox2.Text += (string) row + " , ";
}
textBox2.Text += Environment.NewLine;
}

It is taking a VERY long time, over 10 mins for less than 2000 rows.
Does anyone have any siggestions on making this faster?


I suspect that the problem may be in the string concatenations.
Strings in .Net are immutable, so every time that you change the string
it has to be discarded and a new string allocated instead. These
operations are slow. It should be faster if you use a StringBuilder:

using System.Text;
...
int intCols = myDataSet.Tables[0].Columns.Count;
int intRows = myDataSet.Tables[0].Rows.Count;
StringBuilder sb = new StringBuilder();
foreach (DataRow row in myDataSet.Tables[0].Rows) {
for(int i = 0; i < intCols ; i++) {
sb.Append((string) row + " , ");
}
sb.Append(Environment.NewLine);
}
textBox2.Text = sb.ToString();

WOW!

Thank you. I knew of the StringBuilder object, but I had never had a
casue to use it. In this case it cut execution time down to around 2.1
seconds, and for 40,000 rows, it was around 5 seconds. That is some
performance gain.

Thanks very much Alberto
 

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

Top