ADO.NET测试(二) --测试有返回值的存储过程

本文介绍了如何使用ADO.NET测试一个SQL Server中的存储过程usp_MoreThan,该过程根据输入价格返回高于该价格的商品数量。通过创建SqlCommand对象,设置CommandType为StoredProcedure,添加输入参数及返回值参数,并执行存储过程,然后比较预期结果与实际返回值,以判断测试是否通过。

     假如,我们要测试一个叫做usp_MoreThan()的存储过程,这个存储过程返回SQL表中价格高于输入数值的int值:

create procedure usp_MoreThan

    @inputPrice money

as

    declare @ans int

    select @ans = count(*) from tblPrices where price > inputPrice

    return @ans

go

  1. int expected = 2;
  2. int actual;
  3. string input = "100.00";
  4. string connString = "Server=(local);Database=dbPrices;UID=userId;PWD=password";
  5. SqlConnection sc = new SqlConnection(connString);
  6. SqlCommend cmd = new SqlCommend("usp_MoreThan", sc);
  7. cmd.CommandType = CommandType.StoredProcedure;
  8. SqlParameter p1 = cmd.Parameters.Add("ret_val", SqlDbType.Int);
  9. p1.Direction = ParameterDirection.ReturnValue;
  10. SqlParameter p2 = cmd.Parameters.Add("@inputPrice", SqlDbType.Money);
  11. p2.Direction = ParameterDirection.Input;
  12. p2.Value = input;
  13. sc.open();
  14. if(actual == expected)
  15.     Console.WriteLine("Pass");
  16. else
  17.     Console.WriteLine("Fail");

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值