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

六月ゝ 毕业季﹏ 提交于 2020-03-09 21:56:04
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();
              


        }
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!