Create universal WebService data access for Silverlight projects

Source: Internet
Author: User
Tags foreach silverlight

Before using Silverlight, we created our own WebService as universal data access. The developer passes an SQL statement to get a dataset,datatable as the return value. In the Silverlight project, because of its limitations on the DataTable, we had to get the DataTable first and then create the Web Service (WCF) locally to convert the resulting DataTable. Converts an array or generic collection to meet the needs of Silverlight.

But it's hard to do, and developers are doing a lot of hard work.

Here, we have made some changes to the original webservice: whenever the return value is a dataset, the WebMethod is added a layer of "cloak", converted to XElement and returned to the caller an XML file. Developers simply use LINQ to XML for simple XML operations to get the needed collection. This eliminates the task of creating a "own" service for each project.

The following is a simple demo to illustrate this operation:

Create XElement as return value based on the resulting DataTable

static void Main (string[] args)
{
Get Data source DataTable
DataTable dt = client. ExecuteQuery (SQL). Tables[0];

Create XML Document
XDocument doc= New XDocument (
New XElement ("Tables",
New XAttribute ("xmlns", ""),
New XElement ("table"),
New XAttribute ("name", "0"),
New XElement ("columns"),
New XElement ("Rows"))
);

XElement columns = doc. Element ("Tables"). Element ("table"). Element ("columns");

foreach (DataColumn col in dt. Columns)
{
Add an element Column
Columns. ADD (New XElement ("column"),
New XAttribute ("name", Col.) ColumnName),
New XAttribute ("type", Col. Datatype.name)
));
}

XElement rows = doc. Element ("Tables"). Element ("table"). Element ("Rows");

foreach (DataRow row in dt. Rows)
{
Add an element Row
XElement NewRow = new XElement ("Row");
Rows. ADD (NewRow);

string data = null;
foreach (DataColumn col in dt. Columns)
{
Switch (Col. Datatype.name)
{
Case "DateTime":
data = (Row[col] = = DBNull.Value? "": Convert.todatetime (Row[col]). ToString ("Yyyy/mm/dd HH:mm:ss"));
Break
Default
data = (Row[col] = = DBNull.Value? "": Row[col]. ToString ());
Break
}

Add attribute for the new row
NewRow. ADD (New XAttribute (COL). ColumnName, data)
);

}
}

XElement element = doc. Element ("Tables");

Console.WriteLine (element. ToString ());
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.