A big topic. AKA Object-to-Relational Mapping.
If you want a general solution, there are third-party tools and runtimes out
there.
http://eurohost.webmatrixhosting.net...b-eedf27e20cfb
(beware line breaks)
If you want to do something quick and dirty, then ....
I can imagine building a utility class that employs .NET Reflection to
interrogate a type, retrieve the values of the public fields, then build a
SQL statement that updates a SQL database.
eg,
public static string UpdateSqlWithFields(object o) {
System.Type t= o.GetType();
System.Text.StringBuilder sb1= new System.Text.StringBuilder();
System.Reflection.FieldInfo[] fia= t.GetFields();
// use a db table name derived from the type of the object
string strTable= "[ObjectState " + t.GetName() + "]";
if ((fia != null) && (fia.Length != 0)) {
sb1.Append("UPDATE " + strTable + " (");
int j=0;
foreach (System.Reflection.FieldInfo fi in fia) {
if (j > 0) sb1.Append(", ");
sb1.Append(fi.Name);
j++;
}
sb1.Append(") VALUES (");
j=0;
foreach (System.Reflection.FieldInfo fi in fia) {
try{
object result = t.InvokeMember (fi.Name,
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.GetField ,
null, o, new object [] {});
if (j > 0) sb1.Append(", ");
sb1.Append(result.ToString()); // FIX THIS: quote strings if
necessary (those that include commas)
j++;
}
catch(System.Exception e){
Console.WriteLine("Exception in InvokeMember(Field) : " + e.Message
+ "\n");
continue;
}
}
sb1.Append(")");
}
string strSql= sb1.ToString();
// now you have the SQL string, just need to execute it on the db
connection...
}
BUT,
1. this example doesn't deal with public fields that might be arrays or
structures (or other objects). the algorithm assumes that all public fields
are primitive types. Getting into arrays, and objects that belong to other
objects, you need to deal with relationships and aggregation, and that is
the domain of a more serious ORM tool.
2. this example doesn't deal with public PROPERTIES, as distinct from
fields.
3. this simple example doesn't deal with the concept of object identity.
You may have 30,000,000 different instances of a particular object, you'll
want some concept of identity.
---------------------
I don't see where the XML web services part comes in; you could certainly
add it for a remoting layer, but it is not necessary to address the core
issue of "store public fields into a database".
"MIchael McDowell" <mi**************@triggerwave.co.uk> wrote in message
news:09****************************@phx.gbl...
Anyidea how this might be done using and XML Web Service
and the XMLSerialzation class?
A pointer to an online example would be deeply appreciated.
Thankyou in advance,
Michael McD