From: Raj on
Pl. observe the code snippet:

class myclass
{
}

myclass me=new myclass();

Can the object me, be assigned to DataTable and saved to db?

Thank you

Regards
Raj
From: Alberto Poblacion on
"Raj" <Raj(a)discussions.microsoft.com> wrote in message
news:C4151206-FCD2-476C-A75A-F7E486A284A9(a)microsoft.com...
> Pl. observe the code snippet:
>
> class myclass
> {
> }
>
> myclass me=new myclass();
>
> Can the object me, be assigned to DataTable and saved to db?

Well, you can create a DataTable with a column of type myclass:

DataTable dt = new DataTable();
dt.Columns.Add("myColumn", typeof(myclass));
....
DataRow dr = dt.NewRow();
dr[0]=me;

This stores your instance of "myclass" inside the datatable. However, it
will not be straightforward to save it into a database. You will have to
decide what you mean by "save it to db". You can serialize the instance of
the class and then store the serialied data into a large field in the
database. If you are using SQL Server, you can define a field of type XML,
and use XML serialization to save your class in that field. You can even
create XML indexes and search on the contents of that field. If you are
going to do this, it doesn't make any sense to go through a DataTable,
unless you want to store the serialized data in the DataTable so that it can
later be dumped into the database by means of a DataAdapter.

You may wish to do a different thing. You may want to save the different
properties of "myclass" into different fields in a table the database. If
you already have a table with fields that have the same names and compatible
types as the properties of the class, you can easily write a mapping by
means of a loop using System.Reflection. If the fields in the table do not
match exactly the properties in the class, you can define some attributes to
indicate the conversions needed, and apply those attributes to your class.
The attributes can be then read by Reflection so that your routine can
perform the appropriate conversions. However, if you are going to follow
this route, you can use one of the existing mechanisms rather than writing
your own. For instance, you can use LINQ-to-SQL. Visual Studio provides a
graphical editor where you can drag the tables and modify their properties.
It will then generate a set of classes that already contain all the property
definitions and are decorated with attributes to specify their
characteristics on the database. Also, a class is generated that inherits
from DataContext, and it provides all the functionality that is necessary to
read and write the database according to those attributes. In this way, you
don't have to write the code yourself.

From: Arne Vajhøj on
On 03-04-2010 06:54, Raj wrote:
> Pl. observe the code snippet:
>
> class myclass
> {
> }
>
> myclass me=new myclass();
>
> Can the object me, be assigned to DataTable and saved to db?

As myclass does not extends DataTable then me can not be assigned
to a DataTable ref.

Given that no database types maps to your, then you can not
store me in an old DataTable or a typed DataTable either.

If you have some custom code that converts the myclass
information to something that can be stored in a database,
then you can do it.

Arne