Dataset to typed dataset ?

G

Guest

Hey
I am using SqlHelper and I need to convert returned dataset to a typed
dataset. Any ideas how can I do this ?
Jarod
 
W

W.G. Ryan eMVP

Jarod - why not just fill the Typed DataSet in the first place. There's not
a direct convert function and I'm pretty sure you'd have to write your own
dataset to accomplish this implementing IConvertible for instance. I've
seen a lot of people do stuff like pass in a regular dataset to SqlHelper,
then loop through it copying the rows and then putting them in the typed
dataset (there's so much wrong with approaches like this it's hard to know
where to begin). It's a LOT easier to just fill the typed dataset from the
get go. DataSets are often pretty big too and consume a good bit of memory,
so creating one, just so you can convert it to another type and then destroy
it is definitely not an approach that will scale well,

As an aside, check this out:

private void button1_Click(object sender, System.EventArgs e) {

Dataset1 dss = new Dataset1();

FillDataSet(dss);

}

private void FillDataSet(DataSet ds){

MessageBox.Show(ds.Tables.Count.ToString());

}



Notice that I'm passing in a Typed DataSet but the signature is for a
DataSet. It works b/c the typed dataset is still of type dataset so it will
still work. With SqlHelper, you can specify your tablemappings and
columnmappings, but essentially the same approach can work for you there.
 
G

Guest

Jarod - why not just fill the Typed DataSet in the first place. There's not
a direct convert function and I'm pretty sure you'd have to write your own
dataset to accomplish this implementing IConvertible for instance. I've
seen a lot of people do stuff like pass in a regular dataset to SqlHelper,
then loop through it copying the rows and then putting them in the typed
dataset (there's so much wrong with approaches like this it's hard to know
where to begin). It's a LOT easier to just fill the typed dataset from the
get go. DataSets are often pretty big too and consume a good bit of memory,
so creating one, just so you can convert it to another type and then destroy
it is definitely not an approach that will scale well,

As an aside, check this out:

private void button1_Click(object sender, System.EventArgs e) {

Dataset1 dss = new Dataset1();

FillDataSet(dss);

}

private void FillDataSet(DataSet ds){

MessageBox.Show(ds.Tables.Count.ToString());

}


SqlHelper.ExecuteDataSer(...) it returns DataSet ( untyped :( so I need to
find a way for it to convert. My datasets will be really small only a few
rows. So the scalability don't matters. Maybe you can show me how to invoke
stored procedure by using sqlhelper and then use typed dataset.
Jarod
 
W

W.G. Ryan eMVP

Jarod - the FillDataSet was the method I was speaking of. In practice, I
actually augmented it to have a FillTypedDataSet method and the only
difference is essentially that I have another parameter for the
tablemappings/columnmappings.

Also, although this isn't exactly what you asked origianlly, just as an FYI,
you can use a TypedDataset in the place of where you're expecting an
Untyped one - just not the other way around:

private void button1_Click(object sender, System.EventArgs e) {

DataSet dss = FillDataSet();

MessageBox.Show(dss.Tables.Count.ToString());

}

private Dataset1 FillDataSet(){

Dataset1 ds = new Dataset1();

DataTable dt = new DataTable();

DataColumn dc = new DataColumn("TestColumn", typeof(System.String));

dt.Columns.Add(dc);

ds.Tables.Add(dt);

DataRow dro = dt.NewRow();

dro[0] = "Bill Ryan";

dt.Rows.Add(dro);

return ds;

}



But ultimatey my point is that in this instance, you can easily add an
augmented method to your SqlHelper class as we did, FillTypedDataSet and
since it's a reference type, when you pass it in, any changes made to it
will be made to the origianl (I know, many purists say that you should use
return values instead but I honestly don't see anything wrong with this
approach).

On a scale of 1-10, it's about a 3 to add this in, and you can reuse it
everywhere (Typed Dataset have many many things to like about them, and not
having to worry about getting the colum names right is one of the bigger
ones) so it's a great component to build that you can use all over the
place. I'll be glad to walk you through it.



Cheers,



Bill
 

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