.Net 调用存储过程取到return的值

1. 存储过程


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
alter PROCEDURE GetOrderLine 
	@orderId varchar(50)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    select * from orderLine where OrderId = @orderId;
    
    return 123;
END
GO


注意  存储过程只能返回 int 类型,如果返回一个字符串 ,将会报类型转化错误


2 后台调用


DataTable dt = new DataTable();
            string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["BLL.Properties.Settings.ShoppingDBConnectionString"].ToString();
            using(SqlConnection conn= new SqlConnection(connStr)){
                string callName = "GetOrderLine";
                using (SqlCommand command = new SqlCommand(callName, conn))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    SqlParameter[] sps = { new SqlParameter("@orderId",SqlDbType.VarChar,50) ,
                                           new SqlParameter("@return",SqlDbType.Int)   //注册返回值类型
                                         };

                    sps[0].Value = "43c7cf15-6b2f-4d18-92b2-dbe827f30dfc";
                    sps[1].Direction = ParameterDirection.ReturnValue;   //返回参数类型

                    command.Parameters.AddRange(sps);
                    using(SqlDataAdapter sda =new SqlDataAdapter()){
                    sda.SelectCommand = command;
                    sda.Fill(dt);
                    //Console.WriteLine(sda.GetFillParameters()[1].Value);
                        Console.WriteLine(sps[1].Value);     //取到返回的值
                    }
                    
            }
            }

            if(dt.Rows.Count>0){
                for (int i = 0; i < dt.Rows.Count;i++ )
                {
                    Console.WriteLine(dt.Rows[i]["ProductId"]+":"+dt.Rows[i]["ProductPrice"]+":"+dt.Rows[i]["ProductCount"]);
                }
            }
            Console.ReadLine();





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sust2012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值