假如,我们要测试一个叫做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
- int expected = 2;
- int actual;
- string input = "100.00";
- string connString = "Server=(local);Database=dbPrices;UID=userId;PWD=password";
- SqlConnection sc = new SqlConnection(connString);
- SqlCommend cmd = new SqlCommend("usp_MoreThan", sc);
- cmd.CommandType = CommandType.StoredProcedure;
- SqlParameter p1 = cmd.Parameters.Add("ret_val", SqlDbType.Int);
- p1.Direction = ParameterDirection.ReturnValue;
- SqlParameter p2 = cmd.Parameters.Add("@inputPrice", SqlDbType.Money);
- p2.Direction = ParameterDirection.Input;
- p2.Value = input;
- sc.open();
- if(actual == expected)
- Console.WriteLine("Pass");
- else
- Console.WriteLine("Fail");
本文介绍了如何使用ADO.NET测试一个SQL Server中的存储过程usp_MoreThan,该过程根据输入价格返回高于该价格的商品数量。通过创建SqlCommand对象,设置CommandType为StoredProcedure,添加输入参数及返回值参数,并执行存储过程,然后比较预期结果与实际返回值,以判断测试是否通过。
1961

被折叠的 条评论
为什么被折叠?



