ADO.NET访问SQL Server调用存储过程带回参

1,ADO.NET访问SQL Server调用存储过程带回参

 

2,DatabaseDesign 
use northwind

go

--存储过程1

--插入一条商品 productname=芹菜 unitprice=2.3

create proc p_insert

    @productname varchar(40),

    @unitprice money

as

insert products(productname,unitprice) 

values(@productname,@unitprice)

go

--执行

exec p_insert '芹菜',2.3



--存储过程2

--查全部商品

create proc p_selectall

as

select * from products

go

--执行

exec p_selectall



--存储过程3

--根据商品编号=1,商品名称和单价

create proc p_selectbyid

    @productid int,    --入参

    @productname varchar(40) output,--出参

    @unitprice money output --出参

as

select @productname=productname,@unitprice=unitprice from products where productid=@productid



--执行

declare @name varchar(40)

declare @price money

exec p_selectbyid @productname=@name output, @unitprice=@price output, @productid=1

select @name,@price

 

3,Code

 3.1,Program.cs

View Code
using System;



using System.Data.SqlClient;

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {



           ////测试存储过程1

           // SqlConnection con = new Com.Myt.DB.DBConnection().Con;

           // SqlCommand com = con.CreateCommand();



           // com.CommandText = "usp_insert";

           // com.CommandType = System.Data.CommandType.StoredProcedure;//命令的类型是储存过程

           // com.Parameters.Add(new SqlParameter("@productname", "苹果"));

           // com.Parameters.Add(new SqlParameter("@unitprice", 200));



           // con.Open();

           // int count= com.ExecuteNonQuery();



           // con.Close();

           // Console.WriteLine("一共影响了"+count+"行");





            ////存储过程2,测试返回结果集

            //SqlConnection con = new Com.Myt.DB.DBConnection().Con;

            //SqlCommand com = con.CreateCommand();



            //com.CommandText = "usp_selectall";

            //com.CommandType = System.Data.CommandType.StoredProcedure;



            //con.Open();

            //SqlDataReader sdr = com.ExecuteReader();

            //while (sdr.Read())

            //{



            //    Console.WriteLine(sdr.GetString(0)+"\t"+sdr.GetDecimal(1));

            //}



            //com.Clone();





            //存储过程3,测试输出参数

            //已知商品编号,查名称和单价

            SqlConnection con = new Com.Myt.DB.DBConnection().Con;

            SqlCommand com = con.CreateCommand();



            com.CommandText = "usp_selectbyid";

            com.CommandType = System.Data.CommandType.StoredProcedure;



            //配参,注意出参的配置

            //入参

            com.Parameters.Add(new SqlParameter("@productid", 1));



            //出参

            SqlParameter p1 = new SqlParameter("@productname", System.Data.SqlDbType.VarChar, 20);

            SqlParameter p2 = new SqlParameter("@unitproduct", System.Data.SqlDbType.Decimal);

            //标明输出方向

            p1.Direction = System.Data.ParameterDirection.Output;

            p2.Direction = System.Data.ParameterDirection.Output;

            com.Parameters.Add(p1);

            com.Parameters.Add(p2);



            con.Open();



            com.ExecuteNonQuery();

            con.Close();



            Console.WriteLine(p1.Value+", "+p2.Value);

        }

    }

}

 

3.2,DBConnection.cs

4,

 

warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

你可能感兴趣的:(SQL Server)