Monday, March 12, 2012

how to Copy/Clone a derived DataTable

I would like to add a few properties and methods to an ADO.NET DataTable. So
the normal thing would be to derive a class:
Public Class MyTable
Inherits DataTable
Private This As That
Private TheOtherThing As Whatever
Now, a DataTable has Clone and Copy methods. Is there a straight forward way
for my Derived class to implement Clone and Copy, and rely on the base class
to copy the schema and data? Or do I have to do the heavy lifting myself?
I mean, I can create Clone and Copy functions in the derived MyTable class,
mark them as Shadows to have them replace or override these functions in the
base class, but is there any easy way to copy the schema and data?
ThanksIf it's 2.0, then Serialize and Deserialize your derived DataTable.
- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
----
"Mark" <mark.bax@.blaisesoft-xxx.com> wrote in message
news:%235wOqEfSGHA.1204@.TK2MSFTNGP12.phx.gbl...
>I would like to add a few properties and methods to an ADO.NET DataTable.
>So the normal thing would be to derive a class:
> Public Class MyTable
> Inherits DataTable
> Private This As That
> Private TheOtherThing As Whatever
> Now, a DataTable has Clone and Copy methods. Is there a straight forward
> way for my Derived class to implement Clone and Copy, and rely on the base
> class to copy the schema and data? Or do I have to do the heavy lifting
> myself?
> I mean, I can create Clone and Copy functions in the derived MyTable
> class, mark them as Shadows to have them replace or override these
> functions in the base class, but is there any easy way to copy the schema
> and data?
> Thanks
>

Labels: , , , , , , , , , , ,

How to convert Class object into DataSet?

Lets say, I have a following XSD utility generated class as follows:

[XmlRoot(Namespace="http://example.com/2007/ex1")]
public class Example {
public Example () {}
public string Name;
public int Power;
}

I have to pull data from several tables(MS Access2003) and store it in a DataSet.

Now, Is it possible to use it like:

Example example = new Example();
example.Power = 42;
example.Name = "Kibo";
DataSet myDataSet = new DataSet();
myDataSet = example;

or

myDataSet.Add(example);

Somehow, to add the example object tree in myDataSet, and pull values from tables directly into myDataSet.

This will help me in doing following things:

1. I'd not need to change my existing XSD generated class structure into DataSet.

2. Pull data directly into the DataSet, using explicit queries

3. Generate the Xml file directly.

Any help/idea/pointer/etc would help Alot.

Thanks.

The sample code you have given shouldn't work cause a DataSet can't load data from an custom entity object.

Check this site for more details:

http://msdn2.microsoft.com/en-us/library/ekw4dh3f(VS.80).aspx

http://msdn2.microsoft.com/en-us/library/ekw4dh3f(VS.80).aspx

Labels: , , , , , , , , , , , , , , ,