views:

47

answers:

4

I've got a WCF web call which returns the result of a SQL Command.

However this SQL Command is dynamic so i don’t know how many columns and what data types there are going to be.

How do i the results over WCF to a Silverlight application?

(Btw i then want to be able to put this data into a data grid)

Thanks

A: 

Pretty ugly, but you can return the results in a DataSet, or convert the DataSet to an XML string (possibly cleaned up a bit e.g. with XSLT).

However one of the ideals of WCF is strongly typed interfaces, including returned data. With weak typing, a client will need additional information in order to interpret the XML.

nonnb
A: 

This one crops up time and again in the business world.

The one I have used (and hate with a vengeance) was to return a DataSet or list of lists of strings with header information being in the first row. Then map column names to property types. All involve lots of code and type conversions.

A better alternative might be to return a fixed maximum number of columns (not all of them always used):

  • Create a view with column1, column2... column as your variable column names (up to your maximum)
  • Any common fixed columns (like primary key/id?) are added normally

Then at least you can map it across to the client statically. Basically a variable layout SQL result is best avoided if possible.

Good luck!

Enough already
+1  A: 
Name    Age Height  Income
Adam    50  175     88
Paul    20  166     75
Ranj    19  188     69
Omar    25  200     45

Becomes:

Key Attribute   Value
Adam    Age         50
Adam    Height      175
Adam    Income      88
Paul    Age         20
Paul    Height      175
Paul    Income      75

Etc.

Basically you flatten a data table with an arbitrary number of columns into a data table that will only ever have 3 columns (or 4 if you want to include the data type).

Noel Abrahams
how would you then get this into a datagrid ??
Jonathan D
You could try a pivot grid. I believe DevExpress does pivoting.
Noel Abrahams
A: 

For me such architecture is rapping web services. How do you want to use data with unknown structure and data types?

If you need to return generic data use XElement or XmlElement as return type from your operation and convert result to XML (manual, serialization, FOR XML select, ...).

Ladislav Mrnka