关于这个网上有很多,但都不是非常完整,许多看的一头雾水。我也是根据前辈的很多资料进行了总结,做一个完整的介绍,希望大家共同学习。
首先是存储过程:
ALTERPROCEDURE[dbo].[GetRecordFromPage]@tblNamevarchar(255),--表名@fldNamevarchar(255),--字段名@PageSizeint=10,--页尺寸@PageIndexint=1,--页码@IsReCountbit=0,--返回记录总数,非0值则返回@OrderTypebit=0,--设置排序类型,非0值则降序@strWherevarchar(2000)=''--查询条件(注意:不要加where)ASdeclare@strSQLvarchar(6000)--主语句declare@strTmpvarchar(1000)--临时变量declare@strOrdervarchar(500)--排序类型declare@strvarchar(500)--字符串临时变量if@OrderType!=0beginset@strTmp='<(selectmin'set@strOrder='orderby['+@fldName+']desc'endelsebeginset@strTmp='>(selectmax'set@strOrder='orderby['+@fldName+']asc'endset@strSQL='selecttop'+str(@PageSize)+'*from['+@tblName+']where['+@fldName+']'+@strTmp+'(['+@fldName+'])from(selecttop'+str((@PageIndex-1)*@PageSize)+'['+@fldName+']from['+@tblName+']'+@strOrder+')astblTmp)'+@strOrderif@strWhere!=''set@strSQL='selecttop'+str(@PageSize)+'*from['+@tblName+']where['+@fldName+']'+@strTmp+'(['+@fldName+'])from(selecttop'+str((@PageIndex-1)*@PageSize)+'['+@fldName+']from['+@tblName+']where('+@strWhere+')'+@strOrder+')astblTmp)and('+@strWhere+')'+@strOrderif@PageIndex=1beginset@strTmp=''if@strWhere!=''set@strTmp='where('+@strWhere+')'set@strSQL='selecttop'+str(@PageSize)+'*from['+@tblName+']'+@strTmp+''+@strOrderendif@IsReCount!=0set@strSQL='selectcount(*)asTotalfrom['+@tblName+']'+'where('+@strWhere+')'exec(@strSQL)
如果是需要建立的那么就将ALTER修改为CREATE即可;
下面就介绍如何使用,我采用三层架构的方式:
首先是WEB:
///<summary>///查询按钮///</summary>///<paramname="sender"></param>///<paramname="e"></param>protectedvoidbtnSearch_Click(objectsender,EventArgse){SZBG.BLL.HS.OrdinaryHSInquires_BLbll=newSZBG.BLL.HS.OrdinaryHSInquires_BL();this.AspNetPager.RecordCount=bll.GetCount(this.txtStr.Text);this.BindData();}
这个是按钮的事件
///<summary>///绑定列表///</summary>privatevoidBindData(){SZBG.BLL.HS.OrdinaryHSInquires_BLbll=newSZBG.BLL.HS.OrdinaryHSInquires_BL();DataSetds=bll.GetList(this.AspNetPager.PageSize,this.AspNetPager.CurrentPageIndex,this.txtStr.Text,"1");this.HSGridView.DataSource=ds;this.HSGridView.DataBind();}
这个是绑定到GridView的函数;
BLL层:
///<summary>///分页获得数据列表///</summary>///<paramname="PageSize">每页数量</param>///<paramname="PageIndex">当前页索引</param>///<paramname="strWhere">查询字符串</param>///<paramname="OrderType">设置排序类型,非0值则降序</param>///<returns></returns>publicDataSetGetList(intPageSize,intPageIndex,stringstrWhere,stringOrderType){returndal.GetList(PageSize,PageIndex,strWhere,OrderType);}
DAL层:
///<summary>///分页获取数据列表///</summary>///<paramname="PageSize">每页数量</param>///<paramname="PageIndex">当前页索引</param>///<paramname="strWhere">查询字符串</param>///<paramname="OrderType">设置排序类型,非0值则降序</param>///<returns></returns>publicDataSetGetList(intPageSize,intPageIndex,stringstr,stringOrderType){stringkeyStr="";if(str!=""){keyStr="Namelike'%"+str+"%'";}SqlParameter[]parameters={newSqlParameter("@tblName",SqlDbType.VarChar,255),newSqlParameter("@fldName",SqlDbType.VarChar,255),newSqlParameter("@PageSize",SqlDbType.Int),newSqlParameter("@PageIndex",SqlDbType.Int),newSqlParameter("@IsReCount",SqlDbType.Bit),newSqlParameter("@OrderType",SqlDbType.Bit),newSqlParameter("@strWhere",SqlDbType.VarChar,1000),};parameters[0].Value="HS_Code";parameters[1].Value="Name";parameters[2].Value=PageSize;parameters[3].Value=PageIndex;parameters[4].Value=0;parameters[5].Value=int.Parse(OrderType);parameters[6].Value=keyStr;returnDBHelper.RunProcedure("GetRecordFromPage",parameters,"ds");}
其中keyStr="Namelike'%"+str+"%'"; 就是传入模糊查询的参数了
最后就是DBHelper了:
///<summary>///执行存储过程///</summary>///<paramname="storedProcName">存储过程名</param>///<paramname="parameters">存储过程参数</param>///<paramname="tableName">DataSet结果中的表名</param>///<returns>DataSet</returns>publicstaticDataSetRunProcedure(stringstoredProcName,IDataParameter[]parameters,stringtableName){using(SqlConnectionconnection=newSqlConnection(connectionString)){DataSetdataSet=newDataSet();connection.Open();SqlDataAdaptersqlDA=newSqlDataAdapter();sqlDA.SelectCommand=BuildQueryCommand(connection,storedProcName,parameters);sqlDA.Fill(dataSet,tableName);connection.Close();returndataSet;}}///<summary>///构建SqlCommand对象(用来返回一个结果集,而不是一个整数值)///</summary>///<paramname="connection">数据库连接</param>///<paramname="storedProcName">存储过程名</param>///<paramname="parameters">存储过程参数</param>///<returns>SqlCommand</returns>privatestaticSqlCommandBuildQueryCommand(SqlConnectionconnection,stringstoredProcName,IDataParameter[]parameters){SqlCommandcommand=newSqlCommand(storedProcName,connection);mandType=CommandType.StoredProcedure;foreach(SqlParameterparameterinparameters){if(parameter!=null){if((parameter.Direction==ParameterDirection.InputOutput||parameter.Direction==ParameterDirection.Input)&&(parameter.Value==null)){parameter.Value=DBNull.Value;}command.Parameters.Add(parameter);}}returncommand;}
这样大体能实现功能了。
希望对大家有帮助。