[Ext.Net]GridPanel之存储过程分页Sql版本

1.分页存储过程

 

ALTER  procedure   [dbo].[AspNetPage]
    @tblName       varchar(1000),        -- 表名
    @SelectFieldName    varchar(4000),              -- 要显示的字段名(不要加select)
    @strWhere       varchar(4000),              -- 查询条件(注意: 不要加 where)
    @OrderFieldName      varchar(255),               -- 排序索引字段名
    @PageSize       int ,                 -- 页大小
    @PageIndex      int = 1,                  -- 页码
    @iRowCount      int output,                 -- 返回记录总数
    @OrderType      bit = 0                  -- 设置排序类型, 非 0 值则降序
          
AS
declare @strSQL    varchar(4000)       -- 主语句
declare @strTmp    varchar(4000)        -- 临时变量
declare @strOrder varchar(400)        -- 排序类型
declare @strRowCount    nvarchar(4000)      -- 用于查询记录总数的语句
set @OrderFieldName=ltrim(rtrim(@OrderFieldName))
if @OrderType != 0
begin
    set @strTmp = '<(select min'
    set @strOrder = ' order by ' + @OrderFieldName +' desc'
end
else
begin
    set @strTmp = '>(select max'
    set @strOrder = ' order by ' + @OrderFieldName +' asc'
end
set @strSQL = 'select top ' + str(@PageSize) + @SelectFieldName+' from '
    + @tblName + ' where ' + @OrderFieldName + @strTmp + '('
    + right(@OrderFieldName,len(@OrderFieldName)-charindex('.',@OrderFieldName)) + ') from (select top ' + str((@PageIndex-1)*@PageSize)
    + @OrderFieldName + ' from ' + @tblName + @strOrder + ') as tblTmp)'
    + @strOrder
if @strWhere != ''
    set @strSQL = 'select top ' + str(@PageSize) + @SelectFieldName+' from '
        + @tblName + ' where ' + @OrderFieldName + @strTmp + '('
        + right(@OrderFieldName,len(@OrderFieldName)-charindex('.',@OrderFieldName)) + ') from (select top ' + str((@PageIndex-1)*@PageSize)
        + @OrderFieldName + ' from ' + @tblName + ' where ' + @strWhere + ' '
        + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
if @PageIndex = 1
begin
    set @strTmp = ''
    if @strWhere != ''
        set @strTmp = ' where ' + @strWhere
    set @strSQL = 'select top ' + str(@PageSize) + @SelectFieldName+' from '
        + @tblName + @strTmp + ' ' + @strOrder
end
exec(@strSQL)
if @strWhere!=''
begin
set @strRowCount = 'select @iRowCount=count(*) from ' + @tblName+' where '+@strWhere
end
else
begin
set @strRowCount = 'select @iRowCount=count(*) from ' + @tblName
end
exec sp_executesql @strRowCount,N'@iRowCount int out',@iRowCount out

2.Store

 

 

    <ext:Store ID="StoreFQC" runat="server" OnRefreshData="StoreFQC_RefreshData">
        <Reader>
            <ext:JsonReader>
                <Fields>
                    <ext:RecordField Name="异常编号" Type="String" />
                    <ext:RecordField Name="入库日期" Type="String" />
                    <ext:RecordField Name="检验日期" Type="String" />
                    <ext:RecordField Name="时效" Type="String" />
                    <ext:RecordField Name="供应商编码" Type="String" />
                    <ext:RecordField Name="供应商简称" Type="String" />
                    <ext:RecordField Name="类别" Type="String" />
                    <ext:RecordField Name="批号" Type="String" />
                    <ext:RecordField Name="品号" Type="String" />
                    <ext:RecordField Name="规格" Type="String" />
                    <ext:RecordField Name="数量" Type="Float" />
                    <ext:RecordField Name="抽检数" Type="Float" />
                    <ext:RecordField Name="合格数" Type="Float" />
                    <ext:RecordField Name="不合格数" Type="Float" />
                    <ext:RecordField Name="不良率" Type="Float" />
                    <ext:RecordField Name="装配不良" Type="Float" />
                    <ext:RecordField Name="颜色不良" Type="Float" />
                    <ext:RecordField Name="电性能不良" Type="Float" />
                    <ext:RecordField Name="可靠性不良" Type="Float" />
                    <ext:RecordField Name="点线缺陷" Type="Float" />
                    <ext:RecordField Name="损伤缺陷" Type="Float" />
                    <ext:RecordField Name="包装标识不良" Type="Float" />
                    <ext:RecordField Name="变形" Type="Float" />
                    <ext:RecordField Name="尺寸不良" Type="Float" />
                    <ext:RecordField Name="其他外观不良" Type="Float" />
                    <ext:RecordField Name="不良现象描述" Type="String" />
                </Fields>
            </ext:JsonReader>
        </Reader>
        <Proxy>
            <ext:PageProxy>
            </ext:PageProxy>
        </Proxy>
    </ext:Store>

3.CS

 

 

    /// <summary>
    /// 数据源刷新
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void StoreFQC_RefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        DateTime startdate;
        DateTime enddate;
        startdate = Convert.ToDateTime(this.dfstartdate.Text.Trim());
        enddate = Convert.ToDateTime(this.dfenddate.Text.Trim());
        int PageSize = this.PagingToolBar2.PageSize; //获取当前在页面中PagingToolBar 的PageSize的值
        int Count = 0;
        int CurPage = e.Start / PageSize + 1; //获取当前的页码是多少,也就是第几页
        string strWhere = "";

        strWhere += "  检验日期 >='" + string.Format("{0:yyyyMMdd}", startdate) + "'";
        strWhere += " and 检验日期 <='" + string.Format("{0:yyyyMMdd}", enddate) + "'";

        if (this.txtClient.Text.Trim() != "")
        {
            strWhere += " and 供应商编码 like '%" + this.txtClient.Text.Trim() + "%' ";
        }

        if (this.txtDocumentNo.Text.Trim() != "")
        {
            strWhere += " and 异常编号 like '%" + this.txtDocumentNo.Text.Trim() + "%' ";
        }
        if (this.txtbathno.Text.Trim() != "")
        {
            strWhere += " and 批号 like '%" + this.txtbathno.Text.Trim() + "%' ";
        }
        if (this.txtItemSpec.Text.Trim() != "")
        {
            strWhere += " and 规格 like '%" + this.txtItemSpec.Text.Trim() + "%' ";
        }
        if (this.txtItem.Text.Trim() != "")
        {
            strWhere += " and 品号 like '%" + this.txtItem.Text.Trim() + "%' ";
        }
        if (this.txtms.Text.Trim() != "")
        {
            strWhere += " and 不良现象描述 like '%" + this.txtms.Text.Trim() + "%' ";
        }

        var store = this.GPFQC.GetStore();
        string filed = "入库日期,检验日期,时效,供应商编码,供应商简称,类别,检验方式,批号,品号,规格,数量,抽检数,合格数,不合格数,装配不良,颜色不良,电性能不良,可靠性不良,点线缺陷,损伤缺陷,包装标识不良,变形,尺寸不良,其他外观不良,不良现象描述,异常编号 ";
        DataTable dt = GetPageList("GetIQCRecord", filed, strWhere, PageSize, CurPage, out Count); //连接数据库
        e.Total = Count;
        this.PagingToolBar2.DisplayMsg = "当前第" + CurPage + "页" + PageSize + "/页  共 " + e.Total + " 条";
        if (Count > 0)
        {
            StoreFQC.DataSource = dt;//绑定数据
            StoreFQC.DataBind();
        }
        else
        {
            X.Msg.Alert("查询结果", "<font style='color:red;'>没有找到任何数据!</font>").Show();
        }
    }
    /// <summary>
    /// 取得分页数据
    /// </summary>
    /// <param name="TableName">表名称</param>
    /// <param name="ReturnFields">要显示的字段名</param>
    /// <param name="strwhere">查询条件</param>
    /// <param name="PageSize">每页显示数据条数</param>
    /// <param name="CurPage">当前第几页,页码</param>
    /// <param name="RowCount">返回的总页数</param>
    /// <returns></returns>
    public DataTable GetPageList(string TableName, string ReturnFields, string strwhere, int PageSize, int CurPage, out int RowCount)
    {
        SqlCommand cmd = new SqlCommand("AspNetPage");//存储过程名称
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@tblName", TableName); //表名称
        cmd.Parameters.AddWithValue("@OrderFieldName", "入库日期");//排序索引字段名
        cmd.Parameters.AddWithValue("@PageIndex", CurPage);//当前第几页,页码
        cmd.Parameters.AddWithValue("@PageSize", PageSize);//每页显示数据条数
        cmd.Parameters.AddWithValue("@SelectFieldName", ReturnFields);//要显示的字段名(不要加select)
        cmd.Parameters.AddWithValue("@OrderType", 1);//设置排序类型, 非 0 值则降序
        cmd.Parameters.AddWithValue("@strWhere", strwhere);//查询条件(注意: 不要加 where)
        cmd.Parameters.Add(new SqlParameter("@iRowCount", SqlDbType.Int));
        cmd.Parameters["@iRowCount"].Direction = ParameterDirection.Output;
        DataTable dt = RunProcedureCmd(cmd);
        RowCount = Convert.ToInt32(cmd.Parameters["@iRowCount"].Value.ToString()); //返回的总页数
        return dt;
    }
    /// <summary>
    /// 执行存储过程,返回DataTable
    /// </summary>
    /// <param name="cmd">存储过程名称</param>
    /// <returns></returns>
    public DataTable RunProcedureCmd(SqlCommand cmd)
    {
        DataTable result = new DataTable();
        DBCon db = new DBCon("ZE");

        SqlConnection conn = new SqlConnection(db.StringConnection());//你自己的链接字符串
        try
        {
            if ((conn.State == ConnectionState.Closed))
            {
                conn.Open();
            }
            cmd.Connection = conn;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(result);
            da.Dispose();
            conn.Close();
            conn.Dispose();
            return result;
        }
        catch (Exception ex)
        {
            conn.Close();
            conn.Dispose();
            throw ex;
        }

    }

效果:

 

[Ext.Net]GridPanel之存储过程分页Sql版本_第1张图片

已解决问题:存储过程只支持一个数据表,若涉及到多个表,可以先做成视图 

待解决问题:

第19页有数据,第20 21页是空白

总共记录418条/20 =20.9 

咋回事情呢?



 

你可能感兴趣的:(gridPanel)