ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c# – 使用out参数调用存储过程

2019-06-29 19:02:58  阅读:203  来源: 互联网

标签:c mysql entity-framework-5


我正在尝试调用一个存储过程,其中包含一个参数和两个输出参数.

作为一个脚本,我称之为:

set @MaxPrice = 0.00;
set @MinPrice = 0.00;
set @BSku = '1011001403';
call GetSkuMinMaxPrice(@Sku,@MaxPrice, @MinPrice);

我收回了我的价格

以下是我用ef5调用它的方法:

decimal? minPrice;
decimal? maxPrice;

var skuParameter = new MySqlParameter("?SKU", productToUpload.Sku)
{
    Direction = ParameterDirection.Input
};
var maxPriceParameter = new MySqlParameter("?MaxPrice", SqlDbType.Decimal)
{
    Direction = ParameterDirection.Output
};
var minPriceParameter = new MySqlParameter("?MinPrice", SqlDbType.Decimal)
{
    Direction = ParameterDirection.Output
};
db.Database.ExecuteSqlCommand("call GetSkuMinMaxPrice(?SKU,?MaxPrice,?MinPrice)",
                               skuParameter, 
                               maxPriceParameter, 
                               minPriceParameter);

minPrice = minPriceParameter.Value as decimal?;
maxPrice = maxPriceParameter.Value as decimal?;

对我来说,这看起来很好,但我从MySQL服务器收到此错误消息:例程tng的OUT或INOUT参数2.GetSkuBaseMinMaxPrice在BEFORE触发器中不是变量或NEW伪变量.

那么,除了not using Entity Framework之外,我需要做些什么才能完成这项工作?

到目前为止我的一些研究:

> Syntax for everything except out parameters
> Possible bug in MySQL

解决方法:

这似乎是MySQL处理out参数的结果.我的解决方法是更改​​存储过程以返回out参数的select查询,创建一个POCO,其公共属性名称与存储过程选择结果的列名称匹配.

新存储过程调用

set @BSku = '1011001403';
call GetSkuPrices(@Sku);

我的POCO:

private class PriceOutput
{
    public decimal? MaxPrice { get; set; }
    public decimal? MinPrice { get; set; }
}

我的通话代码:

decimal? minPrice = null;
decimal? maxPrice = null;

var skuParameter = new MySqlParameter("?SKU", productToUpload.Sku);
var basePrices = db.Database.SqlQuery<PriceOutput>("call GetSkuPrices(?SKU)",                                                       
                                                   skuParameter).FirstOrDefault();
if (basePrices != null)
{
    minPrice = basePrices.MinPrice;
    maxPrice = basePrices.MinPrice;
}

标签:c,mysql,entity-framework-5
来源: https://codeday.me/bug/20190629/1328616.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有