469,950 Members | 1,941 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

Saving and retriving a classes public fields to SQLServer

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
Nov 11 '05 #1
1 1497
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

Nov 11 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mike Carroll | last post: by
4 posts views Thread by glenn | last post: by
12 posts views Thread by imme929 | last post: by
reply views Thread by RSH | last post: by
6 posts views Thread by =?Utf-8?B?QUEyZTcyRQ==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.