473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 1616
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
2
by: Mike Carroll | last post by:
I have an XML schema for JobPositionPosting (from www.hr-xml.org). If I give this to XSD.EXE as input and tell it to generate classes, I get a 1500-line ..cs file containing approximately 70 class...
4
by: glenn | last post by:
Is there a way to loop through a classes fields without first knowing what fields are there? Trying to write a generic class that could build a query based on a classes fields and types. This...
12
by: imme929 | last post by:
How do I do it? Nothing in the books is helpful. I need to save a structure with different data types.
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
0
by: RSH | last post by:
I am struggling with a concept I'm not really finding a lot of in depth articles on but Im sure others have had similar issues. What is the best practice when it comes to persisting custom...
0
by: bharathreddy | last post by:
Before going to that i want to say few thing on serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
6
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
I have a class library which contains 3 classes; one exposes all its behaviours and the other two are used internally by the public class. My problem is that although I can create an instance of...
5
by: jc | last post by:
RE: Two Classes with the same Data Structure.. saving code? Inheriting a structure? I have two classes. One in Inherits System.Collections.CollectionBase, the other does not, but they both have...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.