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;
namespace 使用存储过程
{
using System.Data.SqlClient;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string connStr = "Data Source=.;Initial Catalog=MySchoolMoreData;Integrated Security=True";
#region 调用没有参数的存储过程 +void btnNoPARAMAS_Click(object sender, EventArgs e)
/// <summary>
/// 调用没有参数的存储过程
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnNoPARAMAS_Click(object sender, EventArgs e)
{
//SqlDataAdapter da = new SqlDataAdapter("select * from Student", connStr);
SqlDataAdapter da = new SqlDataAdapter("usp_getAllStuInfo", connStr);
DataTable dt = new DataTable();
da.Fill(dt);
this.dgvList.DataSource = dt;
}
#endregion
#region 调用有输入参数的存储过程 +void btnHasParamas_Click(object sender, EventArgs e)
/// <summary>
/// 调用有输入参数的存储过程
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnHasParamas_Click(object sender, EventArgs e)
{
SqlDataAdapter da = new SqlDataAdapter("usp_getStuInfoBySexAndCname", connStr);
//1.如果传入了存储过程,必须告诉服务器按存储过程进行处理,否则就会按sql语句进行处理
da.SelectCommand.CommandType = CommandType.StoredProcedure;
//2.创建存储过程中所需要的参数,注意:名称必须与存储过程的参数名称对应
SqlParameter[] ps = {
new SqlParameter("@cname",this.cboClass.Text),//Text获取显示在下拉列表控件中的文本值
new SqlParameter("@Sex",rdoMale.Checked?"男":"女")
};
//3.将参数传递给服务器使用
da.SelectCommand.Parameters.AddRange(ps);
DataTable dt = new DataTable();
da.Fill(dt);
this.dgvList.DataSource = dt;
}
#endregion
#region 加载班级下拉列表数据和Dgv控件的分页数据 +void Form1_Load(object sender, EventArgs e)
/// <summary>
/// 加载班级下拉列表数据和Dgv控件的分页数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
#region 加载下拉列表数据
SqlDataAdapter da = new SqlDataAdapter("select classid ,classname from classes where classid<@num", connStr);
SqlParameter p = new SqlParameter("@num", 15);
SqlParameter p2 = new SqlParameter("@num2", 150);
da.SelectCommand.Parameters.Add(p2);
da.SelectCommand.Parameters.Add(p);
DataTable dt = new DataTable();
da.Fill(dt);
this.cboClass.DisplayMember = "classname";
this.cboClass.ValueMember = "classid";
this.cboClass.DataSource = dt;
#endregion
LoadDgvData();
}
#endregion
int pageIndex = 1; //当前页索引
//int pageCount = 5;
#region 调用带输出参数和返回值的存储过程 +void btnOutput_Click(object sender, EventArgs e)
/// <summary>
/// 调用带输出参数和返回值的存储过程
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOutput_Click(object sender, EventArgs e)
{
SqlDataAdapter da = new SqlDataAdapter("usp_GetCountByCnameAndSex", connStr);
//1.如果传入了存储过程,必须告诉服务器按存储过程进行处理,否则就会按sql语句进行处理
da.SelectCommand.CommandType = CommandType.StoredProcedure;
//2.创建存储过程中所需要的参数,注意:名称必须与存储过程的参数名称对应
SqlParameter[] ps = {
new SqlParameter("@cname",this.cboClass.Text),//Text获取显示在下拉列表控件中的文本值
new SqlParameter("@Sex",rdoMale.Checked?"男":"女"),
//创建输出参数的时候,没有必要赋值
//创建一个输出参数,服务器最终将输出参数的值返回到这个参数对象的Value属性中
new SqlParameter("@totalCount",100),
new SqlParameter("@cnt",SqlDbType.Int),
new SqlParameter("@result",SqlDbType.Int)
};
//3.一定要修改输出参数的方向,否则服务器会将所有参数当成输入参数进行处理,需要客户端传入值,如果没有就报错
ps.Direction = ParameterDirection.Input;//默认就是input,不设置也没有关系
//ps.Direction = ParameterDirection.Output;//设置参数的方向为输出参数
//指定方向,是向服务器发送返回对应类型输出参数或者返回值的请求
ps.Direction = ParameterDirection.Output;
ps.Direction = ParameterDirection.ReturnValue;
//3.将参数传递给服务器使用
da.SelectCommand.Parameters.AddRange(ps);
DataTable dt = new DataTable();
da.Fill(dt);
this.dgvList.DataSource = dt;
this.lblMsg.Text = "总人数是:" + ps.Value + ",指定性别的人数是:" + ps.Value + ",返回值是:" + ps.Value;
}
#endregion
#region 下一页 + void btnNext_Click(object sender, EventArgs e)
/// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnNext_Click(object sender, EventArgs e)
{
if (pageIndex.ToString() == System.Configuration.ConfigurationManager.AppSettings["totalPageCount"])
{
MessageBox.Show("没有下一页了");
return;
}
pageIndex++;
LoadDgvData();
}
#endregion
#region 获取分页数据 +void LoadDgvData()
/// <summary>
/// 获取分页数据
/// </summary>
private void LoadDgvData()
{
string count = System.Configuration.ConfigurationManager.AppSettings["pageCount"];
SqlParameter[] ps ={
new SqlParameter("@pageIndex",pageIndex),
new SqlParameter("@pageCount",count),
new SqlParameter("@totalPageCount",SqlDbType.Int)
};
ps.Direction = ParameterDirection.Output;//修改参数的方法为输出参数--发送请求
this.dgvList.DataSource = SqlHelper.ExecuteTable("usp_getPageData", CommandType.StoredProcedure, ps);
System.Configuration.ConfigurationManager.AppSettings["totalPageCount"] = ps.Value.ToString();
}
#endregion
#region 上一页 +void btnPre_Click(object sender, EventArgs e)
/// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPre_Click(object sender, EventArgs e)
{
if (pageIndex == 1)
{
MessageBox.Show("没有上一页了");
return;
}
pageIndex--;
LoadDgvData();
}
#endregion
}
}
页:
[1]