Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / July 2003

Tip: Looking for answers? Try searching our database.

Saving and retriving a classes public fields to SQLServer

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MIchael McDowell - 08 Jul 2003 15:44 GMT
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
Dino Chiesa [MSFT] - 08 Jul 2003 16:37 GMT
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/madgeek/SharpToolbox/Default.aspx?Page=Sear
ch&Query=ToolName%3d%26Category%3d74089b0a-1105-4389-b1db-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".

> Anyidea how this might be done using and XML Web Service
> and the XMLSerialzation class?
[quoted text clipped - 3 lines]
> Thankyou in advance,
> Michael McD

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.