存储过程

--存储过程

create proc stuinfo

@stuid int

as

select a.* from Student a where stuid=@stuid

 

--执行存储过程

exec stuinfo 1002

 

--删除存储过程

drop proc stuinfo

 

//VS中执行存储过程stuinfo

SqlConnection con = new SqlConnection();

        con.ConnectionString = "server=.;database=CRM;uid=sa;pwd=123";

        con.Open();

 

        SqlCommand cmd = new SqlCommand();

        cmd.Connection = con;

        //指定命令对象要执行存储过程

        cmd.CommandType = CommandType.StoredProcedure;

        //存储过程名称

        cmd.CommandText = "stuinfo";

        //存储过程参数

        SqlParameter spid = new SqlParameter("@stuid", SqlDbType.Int);

        spid.Value = 1002;

        cmd.Parameters.Add(spid);

 

        SqlDataAdapter ads = new SqlDataAdapter();

        ads.SelectCommand = cmd;

 

        DataSet ds = new DataSet();

        ads.Fill(ds);

 

        GridView1.DataSource = ds;

        GridView1.DataBind();

        con.Close();

 

--存储过程2

create proc stuinsert

@stuname varchar(20),

@stusex char(2),

@stubirthday datetime

as

insert into Student

values(@stuname,@stusex,@stubirthday)

 

SqlConnection con = new SqlConnection();

        con.ConnectionString = "server=.;database=CRM;uid=sa;pwd=123";

        con.Open();

 

        SqlCommand cmd = new SqlCommand();

        cmd.Connection = con;

        //指定命令对象要执行存储过程

        cmd.CommandType = CommandType.StoredProcedure;

        //存储过程名称

        cmd.CommandText = "stuinsert";

        //存储过程参数1

        SqlParameter spname = new SqlParameter("@stuname", SqlDbType.VarChar);

        spname.Value = TextBox1.Text.Trim();

        cmd.Parameters.Add(spname);

        //存储过程参数2

        SqlParameter spsex = new SqlParameter("@stusex", SqlDbType.Char);

        ton1spsex.Value = RadioButton1.Checked ? "" : "";

// RadioButton1RadioButton2GroupName相同

        cmd.Parameters.Add(spsex);

        //存储过程参数3

        SqlParameter spbir = new SqlParameter("@stubirthday", SqlDbType.VarChar);

        spbir.Value = TextBox2.Text.Trim();

        cmd.Parameters.Add(spbir);

 

        int count = cmd.ExecuteNonQuery();

        if (count > 0)

        {

            Response.Write("sg");

        }

        con.Close();

 

--存储过程,新增一行数据并返回该行数据的stuid

create proc stuinsert2

@stuname varchar(20),

@stusex char(2),

@stubirthday datetime,

@stuid int output

as

insert into Student

values(@stuname,@stusex,@stubirthday)

set @stuid=@@identity;

 

SqlConnection con = new SqlConnection();

        con.ConnectionString = "server=.;database=CRM;uid=sa;pwd=123";

        con.Open();

 

        SqlCommand cmd = new SqlCommand();

        cmd.Connection = con;

        //指定命令对象要执行存储过程

        cmd.CommandType = CommandType.StoredProcedure;

        //存储过程名称

        cmd.CommandText = "stuinsert2";

        //存储过程参数1

        SqlParameter spname = new SqlParameter("@stuname", SqlDbType.VarChar);

        spname.Value = TextBox1.Text.Trim();

        cmd.Parameters.Add(spname);

        //存储过程参数2

        SqlParameter spsex = new SqlParameter("@stusex", SqlDbType.Char);

        spsex.Value = RadioButton1.Checked ? "" : "";

// RadioButton1RadioButton2GroupName相同

        cmd.Parameters.Add(spsex);

        //存储过程参数3

        SqlParameter spbir = new SqlParameter("@stubirthday", SqlDbType.VarChar);

        spbir.Value = TextBox2.Text.Trim();

        cmd.Parameters.Add(spbir);

 

        SqlParameter spid = new SqlParameter("@stuid", SqlDbType.Int);

        spid.Direction = ParameterDirection.Output;

        cmd.Parameters.Add(spid);

 

        int count = cmd.ExecuteNonQuery();

        if (count > 0)

        {

            Response.Write(cmd.Parameters["@stuid"].Value);

        }

        con.Close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值