简体   繁体   English

C#MVC数据类型传递给存储过程输出参数

[英]C# MVC Datatype to pass for stored procedure output parameter

I'm trying to call a SQL server stored procedure from a C# MVC controller. 我正在尝试从C#MVC控制器调用SQL Server存储过程。 The parameters for the stored procedure are as follows: 存储过程的参数如下:

ALTER procedure [dbo].[spVehicleSearch360]
    @strLocale          nvarchar(10),
    @xmlSearchCriteria  xml,
    @strSortBy          nvarchar(30),
    @strSortDir         nvarchar(5),
    @uidSessionId       uniqueidentifier,
    @iPage                int = 1,
    @iPageSize          int = 10,
    @iCount               int output,
    @iTotalCount        int output,
    @bShowResults       bit = 1,
    @xmlResults         xml = null output,
    @dtLastItemModified datetime = null output,
    @uidVehicleInList uniqueidentifier = null
as
begin 
.........
.........
.........

When I call this from my controller: 当我从控制器调用此命令时:

var search_results = db.spVehicleSearch(strLocale, xmlSearchCriteria, strSortBy, strSortDir, uidSessionId, iPage, iPageSize, iCount, iTotalCount, bShowResults, xmlResults, dtLastItemModified, uidVehicleInList);

I am getting the following error for the stored procedure's output parameters (arguments 8,9,11,12) 对于存储过程的输出参数,我遇到以下错误(参数8、9、11、12)

Arugment [n]: cannot convert from [int/string/system.DateTime] to 'System.Data.Objects.ObjectParameter' 延期[n]:无法从[int / string / system.DateTime]转换为'System.Data.Objects.ObjectParameter'

What should I be passing in for these arguments? 这些论点我应该传递什么?

(Apologies if this is a very elementary question, I am very new to .net) (很抱歉,如果这是一个非常基本的问题,那么我对.net还是很陌生的)

Thank you 谢谢

The easiest way to address this is to just wrap the parameters that are declared as ObjectParameter in instances of that class, like this: 解决此问题的最简单方法是将包装声明为ObjectParameter的参数包装在该类的实例中,如下所示:

var iCountParam = new ObjectParameter("iCount", typeof(int));
var iTotalCountParam = new ObjectParameter("iTotalCount", typeof(int));
var xmlResultsParam = new ObjectParameter("xmlResults", typeof(string));
var dtLastItemModifiedParam = new ObjectParameter("dtLastItemModified", typeof(DateTime));

var search_results = db.spVehicleSearch(strLocale, xmlSearchCriteria, strSortBy, strSortDir, uidSessionId, iPage, iPageSize, iCountParam, iTotalCountParam, bShowResults, xmlResultsParam, dtLastItemModifiedParam, uidVehicleInList);
//using your previously declared variables...
iCount = (int)iCountParam.Value;
iTotalCount = (int)iTotalCountParam.Value;
//since these are nullable params, gotta check before casting
//you can obviously use whatever you want for the value if it is indeed null
xmlResults = Convert.IsDBNull(xmlResultsParam.Value) ? null : (string)xmlResultsParam.Value;
dtLastItemModified = Convert.IsDBNull(dtLastItemModifiedParam.Value) ? DateTime.MinValue : (DateTime)dtLastItemModifiedParam.Value;

Entity Framework has a hard time dealing with output parameters in this way, and wraps them in the generic ObjectParameter type to make things easier, although it means more boiler-plate code for you. 实体框架很难以这种方式处理输出参数,并且将它们包装在通用的ObjectParameter类型中使事情变得更容易,尽管这对您来说意味着更多样板代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM