Convert a dataset to an array

  • Thread starter Thread starter Alvin Bruney [MVP]
  • Start date Start date
A

Alvin Bruney [MVP]

Surely, there must exist an elegant way to flatten a dataset into a 1D
array. I cannot believe that the only way accomplish this is to loop thru
the rows in the dataset. Yuck!!!

This is what i tried
DataSet ds = loadData();
ds.Tables[0].Rows.CopyTo(str,0); //bombout here

next...

DataColumnCollection dc = ds.Tables[0].Columns;
string[] columns = new string[dc.Count];
dc.CopyTo(columns, 0); //bombout here
strHead = String.Join(",", columns);

I believe the bombout is because of the system.dbnull which a dataset allows
however, i'm not sure. I'd like to use one of these approaches. I absolutely
hate this
for(int i = 0; i < ds.Tables[0].Rows.Count; i++)
StringBuilderVal.Append(",").Append( blah blah blah Yuck!!!

private DataSet loadData()
{
DataSet dsTemp = new DataSet();
DataTable Tables = new DataTable();
dsTemp.Tables.Add(Tables);
dsTemp.Tables[0].Columns.Add( "test", System.Type.GetType(
"System.String" ) );

for(int col = 0; col < 10; col++)
{
DataRow myRow = dsTemp.Tables[0].NewRow();
myRow[0] = col.ToString();
dsTemp.Tables[0].Rows.Add(myRow);
}
return dsTemp;
}

Anybody?
 
I'm not trying to serialize. I'm trying to load data into a component. The
component accepts 1D arrays. The data source is a dataset. I'd like to put
the two together elegantly.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
Miha Markic said:
Hi Alvin,

I am not sure what you are after.
DataSet can be serialized (always as XML).
A better result you will achieve by using this stuff:
True Binary Serialization And Compression of DataSets
http://www.eggheadcafe.com/articles/20031219.asp

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Alvin Bruney said:
Surely, there must exist an elegant way to flatten a dataset into a 1D
array. I cannot believe that the only way accomplish this is to loop thru
the rows in the dataset. Yuck!!!

This is what i tried
DataSet ds = loadData();
ds.Tables[0].Rows.CopyTo(str,0); //bombout here

next...

DataColumnCollection dc = ds.Tables[0].Columns;
string[] columns = new string[dc.Count];
dc.CopyTo(columns, 0); //bombout here
strHead = String.Join(",", columns);

I believe the bombout is because of the system.dbnull which a dataset allows
however, i'm not sure. I'd like to use one of these approaches. I absolutely
hate this
for(int i = 0; i < ds.Tables[0].Rows.Count; i++)
StringBuilderVal.Append(",").Append( blah blah blah Yuck!!!

private DataSet loadData()
{
DataSet dsTemp = new DataSet();
DataTable Tables = new DataTable();
dsTemp.Tables.Add(Tables);
dsTemp.Tables[0].Columns.Add( "test", System.Type.GetType(
"System.String" ) );

for(int col = 0; col < 10; col++)
{
DataRow myRow = dsTemp.Tables[0].NewRow();
myRow[0] = col.ToString();
dsTemp.Tables[0].Rows.Add(myRow);
}
return dsTemp;
}

Anybody?

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
 
Hi Alvin,

What kind of array? Object[]?
Does DataRow.ItemArray help?
You have still loop through all the rows, though.

--
Miha Markic [MVP C#] - DXSquad/RightHand .NET consulting & software
development
miha at rthand com www.rthand.com

Developer Express newsgroups are for peer-to-peer support.
For direct support from Developer Express, write to (e-mail address removed)
Bug reports should be directed to: (e-mail address removed)
Due to newsgroup guidelines, DX-Squad will not answer anonymous postings.

Alvin Bruney said:
I'm not trying to serialize. I'm trying to load data into a component. The
component accepts 1D arrays. The data source is a dataset. I'd like to put
the two together elegantly.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
Miha Markic said:
Hi Alvin,

I am not sure what you are after.
DataSet can be serialized (always as XML).
A better result you will achieve by using this stuff:
True Binary Serialization And Compression of DataSets
http://www.eggheadcafe.com/articles/20031219.asp

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Alvin Bruney said:
Surely, there must exist an elegant way to flatten a dataset into a 1D
array. I cannot believe that the only way accomplish this is to loop thru
the rows in the dataset. Yuck!!!

This is what i tried
DataSet ds = loadData();
ds.Tables[0].Rows.CopyTo(str,0); //bombout here

next...

DataColumnCollection dc = ds.Tables[0].Columns;
string[] columns = new string[dc.Count];
dc.CopyTo(columns, 0); //bombout here
strHead = String.Join(",", columns);

I believe the bombout is because of the system.dbnull which a dataset allows
however, i'm not sure. I'd like to use one of these approaches. I absolutely
hate this
for(int i = 0; i < ds.Tables[0].Rows.Count; i++)
StringBuilderVal.Append(",").Append( blah blah blah Yuck!!!

private DataSet loadData()
{
DataSet dsTemp = new DataSet();
DataTable Tables = new DataTable();
dsTemp.Tables.Add(Tables);
dsTemp.Tables[0].Columns.Add( "test", System.Type.GetType(
"System.String" ) );

for(int col = 0; col < 10; col++)
{
DataRow myRow = dsTemp.Tables[0].NewRow();
myRow[0] = col.ToString();
dsTemp.Tables[0].Rows.Add(myRow);
}
return dsTemp;
}

Anybody?

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
 
I'm tired of playing with this without success.

It seems i will have to iterate the dataset. I was hoping to avoid this.

I can't find a good reason why the copyto method is not working since the
dataset maps to .net strings there shouldn't be a problem.

thanks for the help.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
Miha Markic said:
Hi Alvin,

What kind of array? Object[]?
Does DataRow.ItemArray help?
You have still loop through all the rows, though.

--
Miha Markic [MVP C#] - DXSquad/RightHand .NET consulting & software
development
miha at rthand com www.rthand.com

Developer Express newsgroups are for peer-to-peer support.
For direct support from Developer Express, write to (e-mail address removed)
Bug reports should be directed to: (e-mail address removed)
Due to newsgroup guidelines, DX-Squad will not answer anonymous postings.

Alvin Bruney said:
I'm not trying to serialize. I'm trying to load data into a component. The
component accepts 1D arrays. The data source is a dataset. I'd like to put
the two together elegantly.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
Miha Markic said:
Hi Alvin,

I am not sure what you are after.
DataSet can be serialized (always as XML).
A better result you will achieve by using this stuff:
True Binary Serialization And Compression of DataSets
http://www.eggheadcafe.com/articles/20031219.asp

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

"Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
Surely, there must exist an elegant way to flatten a dataset into a 1D
array. I cannot believe that the only way accomplish this is to loop thru
the rows in the dataset. Yuck!!!

This is what i tried
DataSet ds = loadData();
ds.Tables[0].Rows.CopyTo(str,0); //bombout here

next...

DataColumnCollection dc = ds.Tables[0].Columns;
string[] columns = new string[dc.Count];
dc.CopyTo(columns, 0); //bombout here
strHead = String.Join(",", columns);

I believe the bombout is because of the system.dbnull which a dataset
allows
however, i'm not sure. I'd like to use one of these approaches. I
absolutely
hate this
for(int i = 0; i < ds.Tables[0].Rows.Count; i++)
StringBuilderVal.Append(",").Append( blah blah blah Yuck!!!

private DataSet loadData()
{
DataSet dsTemp = new DataSet();
DataTable Tables = new DataTable();
dsTemp.Tables.Add(Tables);
dsTemp.Tables[0].Columns.Add( "test", System.Type.GetType(
"System.String" ) );

for(int col = 0; col < 10; col++)
{
DataRow myRow = dsTemp.Tables[0].NewRow();
myRow[0] = col.ToString();
dsTemp.Tables[0].Rows.Add(myRow);
}
return dsTemp;
}

Anybody?

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
 
HI Alvin,

Alvin Bruney said:
I'm tired of playing with this without success.

It seems i will have to iterate the dataset. I was hoping to avoid this.

What's the big deal here? Only few lines of code...
I can't find a good reason why the copyto method is not working since the
dataset maps to .net strings there shouldn't be a problem.

Eh, huh, what? What do you mean by "dataset maps to .net strings"?


--
Miha Markic [MVP C#] - DXSquad/RightHand .NET consulting & software
development
miha at rthand com www.rthand.com

Developer Express newsgroups are for peer-to-peer support.
For direct support from Developer Express, write to (e-mail address removed)
Bug reports should be directed to: (e-mail address removed)
Due to newsgroup guidelines, DX-Squad will not answer anonymous postings.
 
What's the big deal here? Only few lines of code...

The big deal here is that these few lines of code are enough to be a
distraction from the concept I am trying to teach/expand upon. I'm not
writing an application - which is probably why you think it is not a big
deal, I'm trying to explain a difficult concept in an article. And adding
these extra lines of code is proving to be a distraction to the concept. I
cannot just leave out the conversion either as it will surely cause more
grief and confusion.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
Miha Markic said:
HI Alvin,

Alvin Bruney said:
I'm tired of playing with this without success.

It seems i will have to iterate the dataset. I was hoping to avoid this.

What's the big deal here? Only few lines of code...
I can't find a good reason why the copyto method is not working since the
dataset maps to .net strings there shouldn't be a problem.

Eh, huh, what? What do you mean by "dataset maps to .net strings"?


--
Miha Markic [MVP C#] - DXSquad/RightHand .NET consulting & software
development
miha at rthand com www.rthand.com

Developer Express newsgroups are for peer-to-peer support.
For direct support from Developer Express, write to (e-mail address removed)
Bug reports should be directed to: (e-mail address removed)
Due to newsgroup guidelines, DX-Squad will not answer anonymous postings.
 
Hi Alvin,

Alvin Bruney said:
The big deal here is that these few lines of code are enough to be a
distraction from the concept I am trying to teach/expand upon. I'm not
writing an application - which is probably why you think it is not a big
deal, I'm trying to explain a difficult concept in an article. And adding
these extra lines of code is proving to be a distraction to the concept. I
cannot just leave out the conversion either as it will surely cause more
grief and confusion.

Ah, I see. You might tell them that .net is incredibly flexible but it
doesn't contain all of the solutions out of the box? ;-)

--
Miha Markic [MVP C#] - DXSquad/RightHand .NET consulting & software
development
miha at rthand com www.rthand.com

Developer Express newsgroups are for peer-to-peer support.
For direct support from Developer Express, write to (e-mail address removed)
Bug reports should be directed to: (e-mail address removed)
Due to newsgroup guidelines, DX-Squad will not answer anonymous postings.
 
Back
Top