问题描述
后台usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.Data.SqlClient;publicpartialclassPro_page:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){AspNetPager1.PageSize=2;Binds();}}publicvoidBinds(){SQLHelpersqlhelp=newSQLHelper();//using(SqlConnectionconn=newSqlConnection(sqlhelp.GetType))using(SqlConnectionconn=newSqlConnection(sqlhelp.ToString())){SqlCommandcmd=newSqlCommand();cmd.CommandText="tz";//存储过程名cmd.Connection=conn;cmd.CommandType=CommandType.StoredProcedure;intpageindex=0;stringstrWhere="";if(AspNetPager1.CurrentPageIndex<1){pageindex=1;}else{pageindex=AspNetPager1.CurrentPageIndex;}SqlParameter[]parameters={newSqlParameter("@tblname",SqlDbType.VarChar,255),newSqlParameter("@strGetFields",SqlDbType.VarChar,1000),newSqlParameter("@fldName",SqlDbType.VarChar,255),newSqlParameter("@pagesize",SqlDbType.Int,4),newSqlParameter("@pageindex",SqlDbType.Int,4),newSqlParameter("@doCount",SqlDbType.Bit),newSqlParameter("@ordertype",SqlDbType.Bit),newSqlParameter("@strwhere",SqlDbType.VarChar,2000)};parameters[0].Value="T_Party";//数据表名称.parameters[1].Value="id,党组织编码";//要显示的字段名parameters[2].Value="id";//主键parameters[3].Value=AspNetPager1.PageSize;//每页显示记录数parameters[4].Value=pageindex;//索引页parameters[5].Value=0;//这里写死0就行了parameters[6].Value=0;//这里的1是降序,0为升序parameters[7].Value=strWhere;//查询条件组合foreach(SqlParameterpinparameters){cmd.Parameters.Add(p);}SqlDataAdaptersda=newSqlDataAdapter(cmd);DataSetds=newDataSet();sda.Fill(ds);GridView1.DataSource=ds.Tables[0].DefaultView;GridView1.DataBind();//动态设置用户自定义文本内容AspNetPager1.RecordCount=int.Parse(ds.Tables[1].Rows[0][0].ToString());AspNetPager1.CustomInfoHTML="记录总数:"+AspNetPager1.RecordCount.ToString()+"条,";AspNetPager1.CustomInfoHTML+="共有"+AspNetPager1.PageCount.ToString()+"页,";AspNetPager1.CustomInfoHTML+="当前页:"+AspNetPager1.CurrentPageIndex.ToString();CustomInfoHTML.Text=AspNetPager1.CustomInfoHTML;}}protectedvoidAspNetPager1_PageChanged(objectsender,EventArgse){Binds();}}连接数据库控制GridView分页SQLHelperusingSystem;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Collections;usingSystem.Configuration;///<summary>///该类中封装了大部分数据库操作,该部分能防止SQL注入攻击///</summary>publicclassSQLHelper{#region属性privatestringConnectionStr=ConfigurationManager.ConnectionStrings["PartyMis"].ConnectionString;privateSqlConnectioncnn=null;privateSqlDataAdapterdadPartyMis=null;//数据库适配器对象privateSqlCommandcmdPartyMis=null;//命令对象#endregion#region构造、析构publicSQLHelper(){cnn=newSqlConnection(ConnectionStr);}~SQLHelper(){cnn.Dispose();}#endregion#region防sql注入后//将HashTable中变量名、值转为参数privatevoidGetParams(SqlCommandcmd,Hashtableht){if((ht.Count==0)||(cmd==null))return;cmd.Parameters.Clear();foreach(DictionaryEntryvalinht){cmd.Parameters.Add((string)val.Key,val.Value);}}///<summary>///执行sql语句strSql,成功则返回true;///</summary>///<paramname="strSql">sql语句</param>///<returns>true:成功;false:失败</returns>publicintExecSql(stringstrSql,Hashtableht){using(cmdPartyMis=newSqlCommand(strSql,cnn)){GetParams(cmdPartyMis,ht);cnn.Open();intval=cmdPartyMis.ExecuteNonQuery();cnn.Close();returnval;}}///<summary>///执行SQL语句,返回第一行的第一列///</summary>///<paramname="sql">sql</param>///<returns>第一行的第一列</returns>publicobjectGetFirst(stringsql,Hashtableht){using(cmdPartyMis=newSqlCommand(sql)){cmdPartyMis.Connection=cnn;GetParams(cmdPartyMis,ht);cnn.Open();objectval=cmdPartyMis.ExecuteScalar();cnn.Close();returnval;}}///<summary>///执行sql语句strSql,返回查询结果。strSql格式为:select*fromXwhere字段1=@a或execP_Sel@a///</summary>///<paramname="strSql">查询语句</param>///<returns>查询结果</returns>publicDataTableGetTable(stringstrSql,Hashtableht){using(dadPartyMis=newSqlDataAdapter()){cmdPartyMis=newSqlCommand(strSql,cnn);GetParams(cmdPartyMis,ht);DataTabledt=newDataTable();dadPartyMis.SelectCommand=cmdPartyMis;dadPartyMis.Fill(dt);returndt;}}#endregion///<summary>///通过该方法可以执行指定的存储过程///</summary>///<paramname="strProcName">存储过程名称</param>///<paramname="strInputs">输入参数列表</param>///<paramname="alOutputs">输出参数列表</param>///<returns></returns>publicboolExecProc1(stringstrProcName,HashtablealInputs,HashtablealOutputs){using(cmdPartyMis=newSqlCommand()){cmdPartyMis.Connection=cnn;cmdPartyMis.CommandType=CommandType.StoredProcedure;cmdPartyMis.CommandText=strProcName;SqlParameterparmReturnValue;try{foreach(DictionaryEntryinputinalInputs){cmdPartyMis.Parameters.Add(input.Key.ToString(),input.Value.ToString());}foreach(DictionaryEntryentryinalOutputs){if(entry.Valueisint)cmdPartyMis.Parameters.Add(entry.Key.ToString(),SqlDbType.Int);elseif(entry.Valueisstring)cmdPartyMis.Parameters.Add(entry.Key.ToString(),SqlDbType.VarChar,200);cmdPartyMis.Parameters[entry.Key.ToString()].Direction=System.Data.ParameterDirection.Output;}parmReturnValue=cmdPartyMis.Parameters.Add("ReturnValue",SqlDbType.Int);parmReturnValue.Direction=ParameterDirection.ReturnValue;cmdPartyMis.Connection.Open();cmdPartyMis.ExecuteNonQuery();Hashtableht=newHashtable();foreach(DictionaryEntryentryinalOutputs){ht.Add(entry.Key.ToString(),cmdPartyMis.Parameters[entry.Key.ToString()].Value);}cmdPartyMis.Connection.Close();alOutputs.Clear();foreach(DictionaryEntryentryinht){alOutputs.Add(entry.Key.ToString(),entry.Value.ToString());}returntrue;}catch(Exceptione1){cmdPartyMis.Connection.Close();returnfalse;}}}///<summary>///执行指定存储过程(不带输出参数)///</summary>///<paramname="strProcName">存储过程名称</param>///<paramname="strInputPars">参数列表</param>///<paramname="alInputValues">参数值</param>///<returns>返回值</returns>publicboolExecProc2(stringstrProcName,HashtablealInputValues){using(cmdPartyMis=newSqlCommand()){cmdPartyMis.Connection=cnn;cmdPartyMis.CommandType=CommandType.StoredProcedure;cmdPartyMis.CommandText=strProcName;foreach(DictionaryEntryvalinalInputValues){cmdPartyMis.Parameters.Add(val.ToString(),val.Value.ToString());}try{cmdPartyMis.Connection.Open();cmdPartyMis.ExecuteNonQuery();cmdPartyMis.Connection.Close();returntrue;}catch(Exceptione1){cmdPartyMis.Connection.Close();returnfalse;}}}}
解决方案
解决方案二:
我只是在困惑你在困惑什么呢?
解决方案三:
引用1楼的回复:
我只是在困惑你在困惑什么呢?
+1
解决方案四:
同L上,把LZ的困惑用中文来写出来
解决方案五:
那个全是错的困惑连接数据库根本无法运行。。。大家帮帮忙看下实在不行给我个能运行的连接数据库控制GridView分页的就行
解决方案六:
....下载AspNetPager演示一看就懂啊
解决方案七:
http://www.webdiyer.com/download/show/30
解决方案八:
我下载的时候怎么就一个下载地址没演示啊
解决方案九:
演示里面有各种成功的数据库连接分页实例http://www.webdiyer.com/download/show/30
解决方案十:
就是下载地址啊,你把他代码下载下来就可以看了啊,都是演示代码
解决方案十一:
该回复于2012-05-10 14:37:36被版主删除
解决方案十二:
恩恩我看看谢了啊
解决方案十三:
引用2楼的回复:
引用1楼的回复:我只是在困惑你在困惑什么呢?+1
++