利用.Net 怎么对存储过程进行调用?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
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();
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。