随笔-85  评论-51  文章-0  trackbacks-5
        /// <summary>
        
/// 返回数据集
        
/// </summary>
        
/// <param name="sql"></param>
        
/// <returns></returns>

        public DataSet GetData(string sql,string strconn)
        
{
            OleDbConnection ole
=new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source="+strconn);
            ole.Open();
            OleDbCommand cm
=new OleDbCommand(sql,ole);
            DataSet ds
=new DataSet();
            OleDbDataAdapter da
=new OleDbDataAdapter(cm);
            da.Fill(ds);
            ole.Close();

            
return ds;
        }


        /// <summary>
        
/// 分页
        
/// </summary>
        
/// <param name="pagesize">每页大小</param>
        
/// <param name="pageindex">当前页数</param>
        
/// <param name="field">获取字段名</param>
        
/// <param name="tablename">表名</param>
        
/// <param name="orderfield">排序字段</param>
        
/// <param name="taxis">排序方式,true为升序,false为降序</param>
        
/// <param name="condition">满足的条件</param>
        
/// <returns></returns>

        public DataSet GetPage(int pagesize,int pageindex,string field,string tablename,string orderfield,bool taxis,string condition)
        
{
            
string temp;
            
if(taxis)
            
{
                temp
="asc";
            }

            
else
            
{
                temp
="desc";
            }


            
string sql;
            
if(pageindex==1)              
            
{
                
if(condition=="")
                
{
                    sql
="select top "+pagesize+" "+field+" from "+tablename+" order by "+orderfield+" "+temp;
                    
return GetData(sql);
                }

                
else
                
{
                    sql
="select top "+pagesize+" "+field+" from "+tablename+" where "+condition+" order by "+orderfield+" "+temp;
                    
return GetData(sql);
                }

            }

            
else
            
{
                pageindex
=(pageindex-1)*pagesize;
                
if(condition=="")
                
{
                    
if(taxis)
                    
{
                        sql
="select top "+pagesize+" "+field+" from "+tablename+" where "+orderfield+">all(select top "+pageindex+" "+orderfield+" from "+tablename+" order by "+orderfield+" "+temp+") order by "+orderfield+" "+temp;
                    }

                    
else
                    
{
                        sql
="select top "+pagesize+" "+field+" from "+tablename+" where "+orderfield+"<all(select top "+pageindex+" "+orderfield+" from "+tablename+" order by "+orderfield+" "+temp+") order by "+orderfield+" "+temp;
                    }

                    
return GetData(sql);
                }

                
else
                
{
                    
if(taxis)
                    
{
                        sql
="select top "+pagesize+" "+field+" from "+tablename+" where "+condition+" and "+orderfield+">all(select top "+pageindex+" "+orderfield+" from "+tablename+" where "+condition+" order by "+orderfield+" "+temp+") order by "+orderfield+" "+temp;
                    }

                    
else
                    
{
                        sql
="select top "+pagesize+" "+field+" from "+tablename+" where "+condition+" and "+orderfield+"<all(select top "+pageindex+" "+orderfield+" from "+tablename+" where "+condition+" order by "+orderfield+" "+temp+") order by "+orderfield+" "+temp;
                    }

                    
return GetData(sql);
                }

            }


        }



希望给于评价
posted on 2006-12-08 15:36 感動常在 阅读(923) 评论(4)  编辑 收藏 网摘 所属分类: 数据库心得

评论:
#1楼 2006-12-08 17:44 | 一滴水      
分页可以看看这篇:
http://tonyqus.cnblogs.com/archive/2006/03/12/344357.html">http://tonyqus.cnblogs.com/archive/2006/03/12/344357.html
尽量不要使用字符串拼凑sql,建议使用参数

  回复  引用  查看    
#2楼 2006-12-11 15:02 | 旋律游魂      
太多字符串连接,性能低,用StringBulider来代替。
  回复  引用  查看    
#3楼 2007-01-01 22:35 | 过路人[未注册用户]
字符串拼凑太多,建议使用高级SQL中的分页查询功能!
  回复  引用    
#4楼 2009-03-15 14:22 | 学习分页的游客[未注册用户]
确实是。其实你可以把参数都做好处理再把字符串接好。。。

接好以后。最后再用一句SQL来执行。

以前ASP这样做的。。现在学.net刚好看到这篇文章

  回复  引用    



发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 586505




相关文章:

相关链接: