Returning Datatable from WCF C#
Step1) Create a WCF Service Application using .NET 4.5 C#
Step 2) Add WCF service name it as DatableDemo
[ServiceContract]
public interface IDatableDemo
{
[OperationContract]
DataTable GetEmpDetails();
}
Step 3) In the implementation file.
public DataTable GetEmpDetails()
{
DataTable emptable = new DataTable("Employees");
try
{
using (SqlConnection conn = new SqlConnection(@"server=(local)\sqlexpress2012;database=AdventureWorks2012;uid=sa;pwd=lordsiva2030&"))
{
using (SqlDataAdapter adapter = new SqlDataAdapter("select * from [AdventureWorks2012].[HumanResources].[Employee]", conn))
{
adapter.Fill(emptable);
emptable.RemotingFormat = SerializationFormat.Xml;
emptable.AcceptChanges();
conn.Close();
}
}
}
catch (Exception ex)
{
throw ex;
}
return emptable;
}
Step 4) Create a schema for DataTable using SvcUtil.exe. otherwise "The underlying connection was closed: An unexpected error occurred on a receive" error msg
will display
Step 5) Open VS2012 Command Prompt in Administrator mode.
c:\wcf>svcutil http://localhost:3054/idtdemo.svc /r:"C:\Program Files (x86)\Ref
erence Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Data.dll" /r:C:\
inetpub\wwwroot\Tutorials\WCFServices\bin\wcfservices.dll /t:metadata
c:\wcf>svcutil tempuri.org.wsdl schemas.microsoft.com.2003.10.Serialization.xsd
tempuri.org.xsd WCFServices.xsd tempuri.org.samples.xsd System.Data.xsd
Step 6) It will generate Proxy class(IDatableDemo.cs) for datatable. Add this class to any project
1) Consume WCF DataTable in Console Application
2) Consume WCF DataTable in Windows Form Application
3) Consume WCF DataTable in Windows Presentation Application(WPF).
4) Consume WCF DataTable in Silverlight Application
5) Consume WCF DataTable in ASP.NET Application
Step 7) in WPF
IdatabledemoClient client2 = new IdatabledemoClient ();
datagrid1.ItemsSource = client2.GetEmpDetails().DefaultView;
No comments:
Post a Comment