Friday, April 15, 2011

Creating a TransactionWCF Service in 4.0


Create a table testtable with columns
as
empno
ename
esal
then follow the steps

Createing Interface: IService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService" in both code and config file together.
[ServiceContract]
public interface IService
{

[OperationContract]
[TransactionFlow(TransactionFlowOption.Allowed)]
void UpdateData(int eno,string ename,decimal esal);
[OperationContract]
DataSet DisplayData(int eno);
[OperationContract]
DataSet fill();
[OperationContract]
DataSet UpdateGrid(int eno,string ename,decimal esal);
[OperationContract]
void DeleteRow(int eno);
[OperationContract]
DataSet DispayAll();

}


Implementing the interface: service.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.
[ServiceBehavior(IncludeExceptionDetailInFaults=true)]
public class Service : IService
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constring"].ToString());
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();

[OperationBehavior(TransactionScopeRequired=true)]
public void UpdateData(int eno,string ename,decimal esal)
{
con.Close();
con.Open();
cmd = new SqlCommand("insert into testtable(empno,ename,esal) values("+eno+",'"+ename+"',"+esal+")",con);
cmd.ExecuteNonQuery();
con.Close();
}
public DataSet DisplayData(int eno)
{
con.Close();
con.Open();
da= new SqlDataAdapter("select * from testtable where empno="+eno+"",con);
da.Fill(ds, "x");
return ds;
}
public DataSet fill()
{
con.Close();
con.Open();
da = new SqlDataAdapter("select * from testtable ", con);
da.Fill(ds, "ys");
return ds;
}
public DataSet UpdateGrid(int eno,string ename,decimal esal)
{
con.Close();
con.Open();
da = new SqlDataAdapter("update testtable set ename='" + ename + "',esal=" + esal + " where empno=" + eno + "", con);
da.Fill(ds, "as");
return ds;
}
public void DeleteRow(int eno)
{
con.Close();
con.Open();
cmd = new SqlCommand("delete from testtable where empno=" + eno + "", con);
cmd.ExecuteNonQuery();
}
public DataSet DispayAll()
{
con.Close();
con.Open();
da = new SqlDataAdapter("select * from testtable",con);
da.Fill(ds);
return ds;
}
}





No comments: