using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace MySchool.Dal
{
public class Studentdal
{
//输出参数存储过程
public DataTable gull(string gender,int genderid,out int couu) {
string srt = SqlHear.count;
string sql = "Usp_320";
SqlConnection con = new SqlConnection(srt);
SqlCommand comd = new SqlCommand(sql,con);
SqlParameter[] drsr =
{
new SqlParameter("@gender",gender),
new SqlParameter("@genderid",genderid),
new SqlParameter("@ccc",SqlDbType.Int),
new SqlParameter ("@frr",SqlDbType.Int)
};
//告诉第三个参数是输出参数
drsr[2].Direction = ParameterDirection.Output;
drsr[3].Direction = ParameterDirection.ReturnValue;
comd.Parameters.AddRange(drsr);
//编译器 是调度后台的存储过程
comd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter ad = new SqlDataAdapter();
ad.SelectCommand = comd;
DataSet ds = new DataSet();
ad.Fill(ds,"oop");
//输出参数值,拿到输出参数的值
couu = Convert.ToInt32(drsr[2].Value);
int frr = Convert.ToInt32(drsr[3].Value);
Console.WriteLine(couu);
Console.WriteLine(frr);
return ds.Tables["oop"];
}
}
}
SQL 数据
alter proc usp_320
@gender nvarchar(32),
@genderid int,
@ccc int output
as
select * from Student
where Gender=@gender and GradeId=@genderid
select @ccc=count(studentNo) from Student
where Gender=@gender and GradeId=@genderid
return 22
declare @oom int
set @oom=0
declare @frr int
set @frr=0
exec @frr=usp_320 1,1,@oom output
print @oom
print @frr
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 MySchool.Dal;
namespace MM.UI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Studentdal dal = new Studentdal();
string gender = textBox1.Text;
int com;
DataTable der = dal.gull(gender,1,out com);
dataGridView1.DataSource = der;
this.textBox2.Text = com.ToString();
}
}
}