标题:使用存储过程来查询;小弟在此请教下大家啊
只看楼主
growingbird
Rank: 1
等 级:新手上路
帖 子:68
专家分:5
注 册:2008-12-24
结帖率:0
已结贴  问题点数:20 回复次数:8 
使用存储过程来查询;小弟在此请教下大家啊
            DateTime dtt=DateTime.Now;  //当前时间

            DateTime starMonth=dtt.AddDays(1-dtt.Day); //月初的那天

                //查询当月的总计数据
        string sqlsel = "select count (*) from sickinfo where addtime between '" + starMonth.ToString("yyyy-MM-dd")+ "' and '" + dtt.ToString("yyyy-MM-dd") + "'";

                //查询当月的已到数据
                string sqlsel1 = "select count (*) from sickinfo where addtime between '" + starMonth.ToString("yyyy-MM-dd") + "' and '" + dtt.ToString("yyyy-MM-dd") + "' and state='已接待'";

这个是我在主窗体中的查询语句,我现在数据库用的是远程数据库,感觉程序开始运行的时候很慢,估计是被这个查询拖慢了速度

所以我想把这SQL语句做成存储过程,以此来提高查询效率

但是小弟初次接触,不知道怎么实现,还望各位大哥指导下啊
搜索更多相关主题的帖子: 查询 
2010-12-15 15:07
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2545
专家分:9359
注 册:2007-11-3
得分:20 
是不会写存储过程 还是想优化sql语句啊?

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2010-12-15 15:10
growingbird
Rank: 1
等 级:新手上路
帖 子:68
专家分:5
注 册:2008-12-24
得分:0 
CREATE PROCEDURE searchfor

@dtt DateTime=DateTime,

@starMonth DateTime =""

AS

declare @strSQL varchar(5000)  --主语句

set  @strSQL= ' select count (*) from sickinfo where addtime

between '+starMonth.ToString("yyyy-MM-dd")+' and '+dtt.ToString("yyyy-MM-dd")+' '

exec (@strSQL)
GO

存储过程我是这么写的;感觉应该不对

还有,最后怎么在C#中得到存储过程查询回来的值呢,这两点不是很清楚
2010-12-15 15:15
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2545
专家分:9359
注 册:2007-11-3
得分:0 
没测试下语法 不用写那么复杂 select 一般不需要返回参数 你用datareader查询就返回datareder dataset就返回dataset 和执行sql语句一样 例如
程序代码:
 CREATE PROCEDURE [getInfo]

 @bgDate varchar(20),

 @edDate varchar(20)

 as


 select count (*) from sickinfo where addtime between @bgDate and @edDate 


 GO


出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2010-12-15 15:22
growingbird
Rank: 1
等 级:新手上路
帖 子:68
专家分:5
注 册:2008-12-24
得分:0 
我引用了网上一位大侠的利用存储过程来分页的代码
///////////////////////////////存储过程////////////////////////////
CREATE PROCEDURE Pagination
@PageSize int =10, --页尺寸

@PageIndex int =1 --页码

 AS

declare @strSQL varchar(5000)  --主语句

set @strSQL='select top '+str(@PageSize)+' * from sickinfo
where id > (select isnull (max(id),0) from (select top' +str((@PageIndex-1)
*@PageSize)+' id from  sickinfo order by id ) as T) order by id
 '

exec (@strSQL)
GO
///////////////////////////////存储过程到此////////////////////////////

// 调用存储过程
            this.dataGridView1.DataSource = db.ExecuteDataSetProc("Pagination", PageSize, NowPage).Tables["sickinfo"];


最后结果是实现了,那在这里面,我怎么让结果是按照ID 的降序排列的  单单的加 desc 我都试过了,实现不了;那具体该怎么做才能实现我的最终结果按照降序排列的呢;麻烦了啊
2010-12-15 15:50
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2545
专家分:9359
注 册:2007-11-3
得分:0 
拿自己的数据库测试没有问题啊
set @strSQL='select top '+str(@PageSize)+' * from sickinfo
where id > (select isnull (max(id),0) from (select top' +str((@PageIndex-1)
*@PageSize)+' id from  sickinfo order by id ) as T) order by id desc


[ 本帖最后由 wangnannan 于 2010-12-15 16:10 编辑 ]

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2010-12-15 16:08
growingbird
Rank: 1
等 级:新手上路
帖 子:68
专家分:5
注 册:2008-12-24
得分:0 
已经实现了;在此对那些帮助弱小者成长的人说声深深的感谢!
///////////////实现降序排列的存储过程//////////////////////////////
CREATE PROCEDURE Pagination  
 
@PageSize   int = 10,          -- 页尺寸  
 
@PageIndex  int = 1          -- 页码  
 
AS  

declare @strSQL   varchar(5000)       -- 主语句  

set @strSQL ='select top ' +str(@PageSize) +' * from sickinfo  where id>(select isnull(max(id),0) from(select top '+ str((@PageIndex-1)*@PageSize) +' id from sickinfo  order by id  )as T)order by id '

if(@PageIndex =1)
begin
set @strSQL='select top '+str(@PageSize)+' * from sickinfo   order by id desc '
end
else
begin
 set @strSQL ='select top ' +str(@PageSize) +' * from sickinfo  where id<(select isnull (min(id),0) from(select top '+str((@PageIndex-1)*@PageSize) +' id from sickinfo  order by id desc )as T)order by id desc '
end
 
exec ( @strSQL)
GO
////////////////////////到此/////////////////////////////////////////////////
2010-12-16 11:25
growingbird
Rank: 1
等 级:新手上路
帖 子:68
专家分:5
注 册:2008-12-24
得分:0 
通过存储过程来实现分页实例
高手飘过,发给那些在摸索中的新手们,在此表示那些热心肠的高手们,谢谢你们了~
///////////////////////////////////////////////

建立类文件SqlHelper.cs
////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;


namespace ERP.userclass
{
   class SqlHelper
    {
      private static readonly string strCon = "server='数据库IP';uid='用户名';pwd='密码';database=数据库名";

       SqlConnection con = new SqlConnection(strCon);

        private DataSet ds;
        private SqlCommand cmd;
        private SqlDataAdapter sda;
        private SqlDataReader myReader;
        private SqlTransaction transaction;

       //检查连接的方法;若连接是关闭的,则打开连接
       public void CheckConnection()
       {
           if (this.con.State == ConnectionState.Closed)
           {
               this.con.Open();
           }
       }

       public DataSet ExecuteDataSetProc(string strSQL,int pagesize ,int pageindex)
       {
           CheckConnection();

           DataSet ds1 = new DataSet();

           try
           {
               cmd = new SqlCommand(strSQL, con);
                = CommandType.StoredProcedure;
               SqlParameter[] para ={ new SqlParameter("@PageSize", SqlDbType.Int), new SqlParameter("@PageIndex", SqlDbType.Int) };

               para[0].Value = pagesize;
               para[1].Value = pageindex;
               cmd.Parameters.AddRange(para);
               SqlDataAdapter dap = new SqlDataAdapter();

               dap.SelectCommand = cmd;
               dap.Fill(ds1, "sickinfo");

           }
           catch (Exception e)
           {
               e.Message.ToString();
           }

           finally
           {
               con.Close();
           }
           return ds1;

       }

       public int ExecuteNonQuery(string strSQL)
       {

           int count = 0;

           CheckConnection();
           try
           {
               cmd = new SqlCommand(strSQL, con);
               count = cmd.ExecuteNonQuery();
           }

           catch (Exception ex)
           {

               throw new Exception(ex.Message);
           }
           finally
           {
               con.Close();
           }
           return count;

       }

       public int ReturnRowCount(string strSQL)
       {
           CheckConnection();
           try
           {
               cmd = new SqlCommand(strSQL, con);
               return Convert.ToInt32(cmd.ExecuteScalar().ToString());
           }
           catch
           {
               return 0;

           }

           finally
           {
               con.Close();
           }
       }

    }
}
////////////////////////////////////////////




using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ADMEWinForm
{
    public partial class Form1 : Form
    {
        SqlHelper db = new SqlHelper();
        public int rowcount;
        public int NowPage = 1, PageCount = 1, PageSize = 20;
        public Form1()
        {
            InitializeComponent();
        }
        public void BindData()
        {
            setBtnStatus();

         //这里是调用存储过程并传入页码和当前页面
            this.dataGridView1.DataSource = db.ExecuteDataSetProc("Pagination", PageSize, NowPage).Tables["sickinfo"];         
        }   
        private void Form1_Load(object sender, EventArgs e)
        {
            BindData();
        }
        //计算余页
        public int OverPage()
        {
            int pages = 0;
            if (rowcount % PageSize != 0)
                pages = 1;
            else
                pages = 0;
            return pages;
        }
        public void setBtnStatus()
        {
           rowcount= db.ReturnRowCount("select count(1) from TGongwen ");
           PageCount = rowcount / PageSize + OverPage();
            this.label2.Text="共"+PageCount+"页 共"+rowcount+"条记录";
            this.label1.Text = "当前是第" + NowPage + "页";
            if (NowPage > 1)
            {
                this.button5.Enabled = true;
                this.button6.Enabled = true;
            }
            else
            {
                this.button5.Enabled = false;
                this.button6.Enabled = false;
            }
            if (NowPage == PageCount)
            {
                this.button7.Enabled = false;
                this.button8.Enabled = false;
            }
            else
            {
                this.button7.Enabled = true;
                this.button8.Enabled = true;
            }        
        }
        private void button5_Click(object sender, EventArgs e)
        {
            NowPage = 1;
            Form1_Load(sender,e);
        }
        private void button6_Click(object sender, EventArgs e)
        {
            NowPage -= 1;
            Form1_Load(sender,e);
        }
        private void button7_Click(object sender, EventArgs e)
        {
            NowPage += 1;
            Form1_Load(sender,e);
        }
        private void button8_Click(object sender, EventArgs e)
        {
            NowPage = PageCount;
            Form1_Load(sender,e);
        }
                    }
}

///////////////////////存储过程/////////////////////////////////////////////////

CREATE PROCEDURE Pagination  
 
@PageSize   int = 10,          -- 页尺寸  
 
@PageIndex  int = 1          -- 页码  
 
AS  

declare @strSQL   varchar(5000)       -- 主语句  

set @strSQL ='select top ' +str(@PageSize) +' * from sickinfo  where id>(select isnull(max(id),0) from(select top '+ str((@PageIndex-1)*@PageSize) +' id from sickinfo  order by id  )as T)order by id '

if(@PageIndex =1)
begin
set @strSQL='select top '+str(@PageSize)+' * from sickinfo   order by id desc '
end
else
begin
 set @strSQL ='select top ' +str(@PageSize) +' * from sickinfo  where id<(select isnull (min(id),0) from(select top '+str((@PageIndex-1)*@PageSize) +' id from sickinfo  order by id desc )as T)order by id desc '
end
 
exec ( @strSQL)
GO
///////////////////////////////////////////////

2010-12-16 13:00
zhaoxiao183
Rank: 3Rank: 3
来 自:湖南浏阳
等 级:论坛游侠
帖 子:48
专家分:144
注 册:2011-4-2
得分:0 
VS2005里面不是有个分页的控件么  ?挺好用的 ,绑定下数据就可以自动分页了

程序员是孤独的
2011-04-06 17:01



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-329031-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.052296 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved