none
How to implement customized class for sqldataadapter in WCF? RRS feed

  • Question

  • Hello

    I am very new to WCF. I have been looking for useful sample that shows me how to work with sqldataadapter in wcf using vs 2008. Actually we are trying convert our data access layer to WCF. I have problem when trying to convert sqldataadapter. Not sure which approach I should use. Appreciate if anyone could provide me useful links and some explainations.

    Much Thanks!

    Monday, April 14, 2014 8:25 AM

Answers

  • Hi,

    The following is a simple example using the sqldataadapter in WCF, please try to check it:

    Step 1: In IService1.cs, add ServiceContract and OperationContract:

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        Employee GetEmployee();
    }
    
    
    
    [DataContract]
    public class Employee
    {
        [DataMember]
        public DataTable EmployeeTable
        {
            get;
            set;
        }
    }
    

    Step 2: Add the following code in the Service1.svc.cs file inside Service1 class:

    public class Service1 : IService1
    {
        string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
        SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter sda;
        DataTable dt;
        Employee emp = new Employee(); 
        public Employee GetEmployee()
        {             
            using (con = new SqlConnection(ConString))
            {
                cmd = new SqlCommand("SELECT EmployeeID, FirstName, LastName FROM Employees", con);
                sda = new SqlDataAdapter(cmd);
                dt = new DataTable("Paging");
                sda.Fill(dt);
                emp.EmployeeTable = dt;
                return emp;
            }        
        }
    }


    Now we have created our WCF service that will return Employees data as a DataTable. Now its time to consume this service.

    Best Regards,
    Amy Peng





    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, April 15, 2014 6:56 AM
    Moderator