在WCF中使用分页

时间:2015-08-25 12:02:58

标签: c# wpf wcf

首先在分页控制中创建存储过程.ACF在分页控件中调用存储过程。最终在WPF应用程序中使用分页控件启用WCF服务。

1 个答案:

答案 0 :(得分:0)

在WCF应用程序中使用了分页控件,其中包含了Stored过程。这个应用程序可以成功执行。我可以在更长的时间内进行测试。

在此处输入代码:Sevicesname:IEmployeeServices.cs

{     //注意:您可以使用“重构”菜单上的“重命名”命令将代码和配置文件中的接口名称“IService1”一起更改。     [服务合约]     公共接口IEmployeeService     {         #region属性         [OperationContract的]         string AddEmployeeDetails(Employee Employee);

    [OperationContract]
    List<Employee> GetEmployeeDetails(int PageNumber,int PageSize,string SortColumn,string SortOrder);

    [OperationContract]
    string UpdateEmployeeDetails(Employee Employee);
    #endregion
}

}

enter code here:EmployeeServices.svc

命名空间EmployeeServiceApps {     //注意:您可以使用“重构”菜单上的“重命名”命令将代码,svc和配置文件中的类名“Service1”一起更改。     公共类EmployeeService:IEmployeeService     {         #region方法         ///         ///添加新的EmployeeRecord方法         ///         ///         ///         public string AddEmployeeDetails(Employee Employee)

    {
        string result = string.Empty;
        SqlConnection con;
        SqlCommand cmd;
        try
        {
            string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
            con = new SqlConnection(ConString);
            con.Open();
            cmd = new SqlCommand("usp_Employee", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@EmployeeID", Employee.EmployeeID);
            cmd.Parameters.AddWithValue("@EmployeeName", Employee.EmployeeName);
            cmd.Parameters.AddWithValue("@EmployeeAddress", Employee.EmployeeAddress);
            cmd.Parameters.AddWithValue("@EmployeePhoneNo", Employee.EmployeePhoneNo);
            cmd.Parameters.AddWithValue("@Action", Employee.Action);
            cmd.ExecuteNonQuery();
            con.Close();
            result = "Record Inserted Successfully";
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return result;
    }

    /// <summary>
    /// Update EmployeeRecords Methods
    /// </summary>
    /// <param name="Employee"></param>
    /// <returns></returns>
    public string UpdateEmployeeDetails(Employee Employee)
    {
        SqlConnection con;
        SqlCommand cmd;
        DataSet ds;
        ds = new DataSet();
        string result = string.Empty;
        string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
        try
        {
            con = new SqlConnection(ConString);
            cmd = new SqlCommand("usp_Employee", con);
            con.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("EmployeeID", Employee.EmployeeID);
            cmd.Parameters.AddWithValue("EmployeeName", Employee.EmployeeName);
            cmd.Parameters.AddWithValue("EmployeeAddress", Employee.EmployeeAddress);
            cmd.Parameters.AddWithValue("EmployeePhoneNo", Employee.EmployeePhoneNo);
            cmd.Parameters.AddWithValue("@Action", Employee.Action);
            cmd.ExecuteNonQuery();
            result = "Record Updated Sucessfully";
            con.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return result;
    }

    /// <summary>
    /// GetEmployeeDetails Methods
    /// </summary>
    /// <returns></returns>
    public List<Employee> GetEmployeeDetails(int PageNumber, int PageSize, string SortColumn, string SortOrder)
    {
        List<Employee> Employees = null;
        SqlConnection con;
        SqlCommand cmd;
        DataSet ds;
        SqlDataAdapter sda;
        ds = new DataSet();
        string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
        try
        {
            con = new SqlConnection(ConString);
            con.Open();
            cmd = new SqlCommand("usp_Employee", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Action", "GET");
            cmd.Parameters.AddWithValue("@PageNumber", PageNumber);
            cmd.Parameters.AddWithValue("@PageSize", PageSize);
            cmd.Parameters.AddWithValue("@SortColumn", SortColumn);
            cmd.Parameters.AddWithValue("@SortOrder", SortOrder);
            sda = new SqlDataAdapter(cmd);
            sda.Fill(ds);
            sda.Dispose();
            Employees = parseEmployeeDetails(ds);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return Employees;
    }

    /// <summary>
    ///Parsing the EmployeeRecords
    /// </summary>
    /// <param name="dsEmployeeDetails"></param>
    /// <returns></returns>
    public List<Employee> parseEmployeeDetails(DataSet dsEmployeeDetails)
    {
        List<Employee> employees = null;
        try
        {
            employees = new List<Employee>();

            if (dsEmployeeDetails != null && dsEmployeeDetails.Tables.Count > 0)
            {
                DataTable dt = new DataTable();

                dt = dsEmployeeDetails.Tables[0];

                foreach (DataRow dr in dt.Rows)
                {
                    Employee employee = new Employee();

                    employee.EmployeeID = Convert.ToString(dr["EmployeeID"]);
                    employee.EmployeeName = Convert.ToString(dr["EmployeeName"]);
                    employee.EmployeeAddress = Convert.ToString(dr["EmployeeAddress"]);
                    employee.EmployeePhoneNo = Convert.ToString(dr["EmployeePhoneNo"]);
                    employee.TotalCount = Convert.ToInt32(dr["TotalCount"]);
                    employees.Add(employee);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return employees;
    }
    #endregion
}

}