C# 使用存储过程来查询数据库

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 _02_通过存储过程查询数据
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private int _pageIndex = 1;//--@pageIndex  --当前查询的是第几页
        private int _rowsInPage = 10;//--@rowsInPage --一页中有多少行
        private int _rowsInTable;//--@rowsInTable --一共有多少行
        private int _pageInTable;//--@pagesInTable --一共有多少页
        private void Form1_Load(object sender, EventArgs e)
        {
            
            //C#使用数据库存储过程的注意事项
            //1.要在sqlcommand里面声明 cmd.SelectCommand.CommandType = CommandType.StoredProcedure;
            //2.output 参数要加{ Direction =ParameterDirection.Output }
            //3.output参数的返回值connect.close(),conn.dispose()之后才能拿到返回值
            //4.adapter.SelectCommand.CommandType = CommandType.StoredProcedure其实就相当于在存储过程名前面加了个“exec ”
            //5.要根据存储过程的参数,来写pms集合,不能缺少

            
            formatDataGridView1();

        }

        private void formatDataGridView1()
            //思路
        {   //1.sql 语句  直接使用 存储过程名
            string constr = "Data Source=PS-03\\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True;";
            string sql = "GetPageRows";
            //2.初始化pms
            SqlParameter[] pms = new SqlParameter[]
            {
            new SqlParameter("@pageIndex",SqlDbType.Int){ Value=this._pageIndex},
            new SqlParameter("@rowsInPage",SqlDbType.Int){ Value=this._rowsInPage},
            new SqlParameter("@rowsInTable",SqlDbType.Int){ Direction =ParameterDirection.Output },//output参数需要设置Direction =ParameterDirection.Output
            new SqlParameter("@pagesInTable",SqlDbType.Int){ Direction =ParameterDirection.Output }
            };

            //3.调用 SqlDataAdapter 读取数据
            DataTable dt = new DataTable();//声明一个datatable接收数据
            using (var adapter = new SqlDataAdapter(sql, constr))
            {
                adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                adapter.SelectCommand.Parameters.AddRange(pms);
                adapter.Fill(dt);
            }
            //4.更新table
            
            label1.Text = "第"+this._pageIndex.ToString()+"/"+ pms[3].Value.ToString() + "页";
            label2.Text = "一共有" + pms[3].Value.ToString() + "页";
            label3.Text = "一共有" + pms[2].Value.ToString() + "行";
            //5.捆绑datagridview
            dataGridView1.DataSource = dt;

            //6.将总页数返回给字段
            this._pageInTable = (int)pms[3].Value;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this._pageIndex < this._pageInTable)
            {
                this._pageIndex++;
            }
            else
            { 
                this._pageIndex=1;
            }
            formatDataGridView1();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                this._pageIndex = Convert.ToInt32(textBox1.Text.Trim());
                formatDataGridView1();
            }
            catch { }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this._pageIndex > 1)
            {
                this._pageIndex--;

            }
            else
            {
                this._pageIndex = this._pageInTable;
            }
            formatDataGridView1();
              


        }
    }
}
发布了55 篇原创文章 · 获赞 4 · 访问量 1416

猜你喜欢

转载自blog.csdn.net/BowenXu11/article/details/104759546