参数存储过程

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();
        }
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值