c#运用sql语句实现超简单分页

c#运用sql语句实现超简单分页

大家都知道c#中对dataGridView来分页相对麻烦,经常一下就是几十行代码,今天来给大家带来一个非常简单的sql分页
小编这里临时搭了一个窗口

c#运用sql语句实现超简单分页_第1张图片
下面我们来看代码
这里先给大家发一下核心代码

 /// 
        /// 填充数据到datagridview
        /// 

        public void da1()
        {
            string sql = @"SELECT TOP 10 * FROM [students] WHERE id NOT IN(SELECT TOP (10 * ("+textBox1.Text + " - 1)) id FROM[students] )";

            DataBase dbase = new DataBase();
            DataSet myds = dbase.dataset(sql);
            dataGridView1.DataSource = myds.Tables[0];          
        }
此语句可直接进行分页,通过定义变量来改变TOP中的数值即可!

下面附上小编本窗口的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 练习_登录窗体
{
    public partial class 分页 : Form
    {
        int maths;//页码
        int zts = 2;//总条数
        DataSet myds;

        public 分页()
        {
            InitializeComponent();
        }

        private void 分页_Load(object sender, EventArgs e)
        {
            maths = 1;           
            textBox1.Text = maths.ToString();
            da1();
            selectData();
            label1.Text = "共" + (zts/10+1) + "页";
        }

        /// 
        /// 填充数据到datagridview
        /// 

        public void da1()
        {
            string sql = @"SELECT TOP 10 * FROM [students] WHERE id NOT IN(SELECT TOP (10 * ("+textBox1.Text + " - 1)) id FROM[students] )";


            DataBase dbase = new DataBase();
            DataSet myds = dbase.dataset(sql);
            dataGridView1.DataSource = myds.Tables[0];
           

            // dataGridView2.Columns[0].FillWeight = 40;


        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            MessageBox.Show(textBox1.Text);
        }


        /// 
        /// 页码判断
        /// 
        /// 
        /// 
        private void button2_Click(object sender, EventArgs e)
        {
            if (maths == (zts / 10 + 1))
            {
                MessageBox.Show("已经是最后1页");
                textBox1.Text = (zts / 10 + 1).ToString();
                return;
            }
            maths += 1;
            textBox1.Text = maths.ToString();
            da1();
        }

        
        private void button1_Click(object sender, EventArgs e)
        {
            if (maths == 1)
            {
                MessageBox.Show("已经是第1页");
                textBox1.Text = "1";
                return;
            }
           
            else
            {
                maths -= 1;
                textBox1.Text = maths.ToString();
                da1();
            }
        }

        /// 
        /// 查询数据条数
        /// 
        public void selectData()
        {
            string constr = "server=.;database=Test;uid=sa;pwd=123456";         
            SqlConnection con = new SqlConnection(constr);
            con.Open();
            string sql = "SELECT COUNT(ID) as math FROM students";
            SqlCommand com = new SqlCommand(sql, con);
            SqlDataReader sread = com.ExecuteReader();
            sread.Read();
            zts = Convert.ToInt32(sread["math"]);
        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                maths = Convert.ToInt32(textBox1.Text);
                da1();
            }
            catch
            {

            }
           
        }
    }
}

代码可能有点不完善,但这里想说的是,sql分页的好处,十行代码搞定分页,然后加上相对应的提示即可,有不懂的评论区留言哦,感谢支持!

你可能感兴趣的:(c#,winform,c#,sql)