Integrating Data


Listing 9.2 illustrates how Web services can be used to return complex types marshaled as XML. Apart from the custom serialization approach used earlier, Web services can be used to return DataSets that represent data retrieved from enterprise SQL databases (actually, DataSets can be used to represent nonrelational data as well). DataSets are then automatically serialized into their appropriate XML representation (Figure 9.6). This dataset can then be used by another ASP.NET Web application (by data binding a DataGrid Web server control) or a Windows Form application (by data binding a DataGrid Windows forms control). The data binding capability within the .NET Framework makes Web services a mechanism for distributing pieces of the application on different systems (see Listing 9.3).

Listing 9.3 Integrating Data with Web Services
 <%@ Webservice class="hks.EmployeeService" language="C#"%> using System; using System.Data; using System.Data.SqlClient; using System.Xml; using System.Xml.Serialization; using System.Configuration; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Collections; namespace hks {    [WebService(Namespace="http://www.hiteshseth.com/WebServices")]    public class EmployeeService    {       [WebMethod(Description="Get All Employees")]       public DataSet GetAllEmployees()       {          return Employee.GetAll();       }       [WebMethod(Description="Lookup Employee")]       public DataSet LookUpEmployee(String employeeId)       {          return Employee.LookUp(employeeId);       }    }    public class Employee    {       public static DataSet GetAll()       {          return RunSql("select * from Employees");       }       public static DataSet LookUp(String employeeId)       {          return RunSql("select * from Employees where Id="+employeeId);       }       private static DataSet RunSql(String sql)       {               String strConn = ConfigurationSettings.AppSettings["DSN"];               SqlConnection cn = new SqlConnection(strConn);          cn.Open();          DataSet ds = new DataSet();          SqlDataAdapter cmdSelect = new SqlDataAdapter(sql,cn);          cmdSelect.Fill(ds,"Employees");          cn.Close();          return ds;       }    } } 
Figure 9.6. Making enterprise data available through Web services.



Microsoft.Net Kick Start
Microsoft .NET Kick Start
ISBN: 0672325748
EAN: 2147483647
Year: 2003
Pages: 195
Authors: Hitesh Seth

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net