在 WCF 中使用分页

Posted

技术标签:

【中文标题】在 WCF 中使用分页【英文标题】:Paging using in WCF 【发布时间】:2015-11-19 03:05:45 【问题描述】:

首先在分页控件中创建存储过程。再次在分页控件中的存储过程上调用 WCF。最后在使用分页控件的 WPF 应用程序中启用 WCF 服务。

【问题讨论】:

【参考方案1】:

分页控件用于 WCF 应用程序中,包含在存储过程中。此应用程序可以成功执行。我有更多时间测试。

在此处输入代码:服务名称:IEmployeeServices.cs

// 注意:您可以使用“重构”菜单上的“重命名”命令将代码和配置文件中的接口名称“IService1”一起更改。 [服务合同] 公共接口 IEmployeeService #region 属性 [运营合同] 字符串 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 方法 /// /// /// 公共字符串 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

【讨论】:

以上是关于在 WCF 中使用分页的主要内容,如果未能解决你的问题,请参考以下文章

使用 WPF 和使用存储过程进行分页

如果我要在 IIS 中托管 WCF 服务,使用 WCF 服务而不是 WCF 库有啥好处?

分享基于EF+WCF的通用三层架构及解析

C/C++ 中/使用 WCF(WCF 绑定)

如何在数据合同 WCF 中使用枚举

使用移动设备访问托管在 Windows 服务中的 WCF 服务