我在SQL server 2012和web API物體框架.NET core 2.2上作業。 所以我面臨的問題是,我不能實作web API執行存盤程序,如下所示
創建程式 ItemCalculateStock
@ItemId int = NULL,
@InventoryLocation int=NULL
as
開始
SELECT i.itemName,l. InventoryName, SUM(case when QTY > 0 then QTY else 0 end) as PurchasedItem, SUM(case when QTY < 0 then -QTY else 0 end) as ConsumItems, SUM(case when QTY > 0 then QTY else 0 end) SUM(case when QTY < 0 then QTY else 0 end) as remaining
FROM [dbo].[Invenroty] n with(nolock)
inner join [dbo].[InventoryLocations] l with(nolock) on l.id=n.InventoryLocID
inner join [dbo].[Items] i with(nolock) on n.itemid=i.id
inner join [dbo].[TransactionTypes] t with(nolock) on n.transactionTypeId=t. ID and InventoryLocID=case when @InventoryLocation is null Then n. InventoryLocID else @InventoryLocation end
and i.id=case when @ItemId is null then n. itemid else @ItemId end
GROUP BY i.itemName,l.InventoryName
結束
因此,如何使用Entity Framework .NET core 2.2在Web API上獲得存盤程序的結果
。[HttpGet("CalculateInventoryData")/span>]
public IActionResult CalculateInventoryData([FromQuery]int optionId。[FromQuery] int ItemId, [FromQuery] int InventoryLocation)
{
//here how to get stored procedure result here
//so I ask question to know how to get result of stored procedure above.
}
為了呼叫API,我使用了下面的鏈接:
https:/localhost:44374/api/Inventory/getInventoryData? optionId=1& ItemId=2& InventoryLocation=1
更新后的帖子
我試了一下
context.Database.ExecuteSqlCommand("ItemCalculateStock @OptionId ,@ItemId,@InventoryLocation"/span>, parameters: new[] {optionId,ItemId,InventoryLocation})。)
i get錯誤,不能隱含地將型別int into轉換為Microsoft .aspnetcore.mvc.action result
那么如何解決這個問題呢?
uj5u.com熱心網友回復:
執行不帶引數的存盤程序的代碼如下:
SqlConnection conn=new SqlConnection("connectionString")。
SqlDataAdapter da = new SqlDataAdapter()。
da.SelectCommand = new SqlCommand()。
da.SelectCommand.Connection = conn;
da.SelectCommand.Command = "NameOfProcedure"/span>;
da.SelectCommand.CommType = CommandType.StoredProcedure。
執行帶引數的存盤程序的代碼如下(我們可以將呼叫存盤程序的函式宣告為ExeProcedure(string inputdate)):
param = new SqlParameter("@ParameterName"/span>, SqlDbType.DateTime)。
param.Direction = ParameterDirection.Input;
param.Value = Convert.ToDateTime(inputdate);
da.SelectCommand.Parameters.Add(param)。
這就增加了一個輸入引數。如果你需要添加輸出引數:
param = new SqlParameter("@ParameterName", SqlDbType.DateTime) 。
param.Direction = ParameterDirection.Output;
param.Value = Convert.ToDateTime(inputdate);
da.SelectCommand.Parameters.Add(param)。
要獲得存盤程序的回傳值:
param = new SqlParameter("@ParameterName"/span>, SqlDbType.DateTime)
param.Direction = ParameterDirection.ReturnValue;
param.Value = Convert.ToDateTime(inputdate);
da.SelectCommand.Parameters.Add(param)。
更多資訊,請參考此帖:如何在Entity Framework Core中運行存盤程序?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/319228.html
標籤:
上一篇:在C#中反序列化復雜物件
