使用参数

时间:2018-08-06 17:33:29

标签: tsql sql-server-2005 asp.net-mvc-5 entity-framework-6

我不明白为什么这行代码执行时出现语法错误:

return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreQuery<Kitti_getProductGroupDetail_Result>("Kitti_getProductGroupDetail @ProductGroupId", @params);

这是整个方法:

 public virtual ObjectResult<Kitti_getProductGroupDetail_Result> Kitti_getProductGroupDetail(Guid ProductGroupId)
 {
     var @params = new SqlParameter[]
     {
            new SqlParameter("ProductGroupId", ProductGroupId)
     };

     return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreQuery<Kitti_getProductGroupDetail_Result>("Kitti_getProductGroupDetail @ProductGroupId", @params);
}

这是调用方法:

public System.Collections.Generic.IEnumerable<ProductDetail> getProductDetailsFromSP(Guid ProductGroupId)
{
    System.Collections.Generic.List<ProductDetail> productDetailList = new System.Collections.Generic.List<ProductDetail>();

    using (var context = new EFDbContext())
    {
        var result = context.Kitti_getProductGroupDetail(ProductGroupId);

        foreach (Kitti_getProductGroupDetail_Result res in result)
        {
             ProductDetail productDetail = new ProductDetail();
             productDetail.ProductId = res.productid;
             productDetail.SizeId = res.sizeid;
             productDetail.SizeDescription = res.sizedescription;
             productDetail.SizeRank = res.sizerank;
             productDetail.ColorId = res.colorid;
             productDetail.ColorDescription = res.colordescription;
             productDetail.ColorRank = res.colorrank;
             productDetail.QuantityRemainingInStock = res.quantityremaininginstock;
             productDetail.ProductGroupId = res.productgroupid;
             productDetail.ProductGroupName = "ProductGroupName is currently not in the stored procedure or in EF";
             productDetail.Description = res.description;

             productDetailList.Add(productDetail);
         }
     }

     return productDetailList;
 }

结果对象:

public partial class Kitti_getProductGroupDetail_Result
{
    public System.Guid productid { get; set; }
    public System.Guid productgroupid { get; set; }
    public string description { get; set; }
    public decimal price { get; set; }
    public Nullable<System.Guid> colorid { get; set; }
    public string colordescription { get; set; }
    public int colorrank { get; set; }
    public Nullable<System.Guid> sizeid { get; set; }
    public string sizedescription { get; set; }
    public int sizerank { get; set; }
    public int quantityremaininginstock { get; set; }
    public string productname { get; set; }
    public string imagesrc { get; set; }
}

最后是我的存储过程:

ALTER PROCEDURE [dbo].[Kitti_getProductGroupDetail] 
@ProductGroupId uniqueidentifier



-- Insert statements for procedure here
SELECT product.ProductId as 'productid', product.ProductGroupId as 'productgroupid', product.[Description] as 'description'
, product.Price as 'price', product.ColorId as 'colorid', color.ColorDescription as 'colordescription', color.ColorRank as 'colorrank', 
product.SizeId as 'sizeid', size.SizeDescription as 'sizedescription', size.SizeRank as 'sizerank', product.QuantityRemainingInStock as 'quantityremaininginstock',
product.Name as 'productname', product.ProductImageSrc as 'imagesrc'
FROM dbo.Product product
INNER JOIN dbo.Color color
    on color.ColorId = product.ColorId
INNER JOIN dbo.Size size
    on size.SizeId = product.SizeId
INNER JOIN dbo.ProductGroup productgroup
    on productgroup.ProductGroupId = product.ProductGroupId
WHERE product.ProductGroupId = @ProductGroupId
AND product.QuantityRemainingInStock > 0

ProductGroupId肯定会被填充。 EF对我的代码不满意吗?我有一个类似的方法,不需要参数即可执行存储过程-可以正常工作

 // this works just fine
    public virtual ObjectResult<Kitti_getProductDetailsAndCategory_Result> Kitti_getProductDetailsAndCategory()
    {
        return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreQuery<Kitti_getProductDetailsAndCategory_Result>("Kitti_getProductDetailsAndCategory");

        //ExecuteFunction changed to ExecuteStoreQuery
    }

堆栈跟踪:

  

“ Kitti_getProductGroupDetail”附近的语法不正确。     说明:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

异常详细信息:System.Data.SqlClient.SqlException:'Kitti_getProductGroupDetail'附近的语法不正确。

源错误:

第40行:}; 第41行: 第42行:return(((IObjectContextAdapter)this).ObjectContext.ExecuteStoreQuery(“ Kitti_getProductGroupDetail @ProductGroupId”,@params); 第43行:} 第44行:

源文件:C:\ Concrete \ EFDbContext.cs行:42

堆栈跟踪:

[SqlException(0x80131904):“ Kitti_getProductGroupDetail”附近的语法不正确。]    System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔值breakConnection,操作1 wrapCloseInAction) +2444082 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action 1 wrapCloseInAction)+5775560    System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,布尔调用方HasConnectionLock,布尔asyncClose)+285    System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,Boolean&dataReady)+4169    System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()+58    System.Data.SqlClient.SqlDataReader.get_MetaData()+89    System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,字符串resetOptionsString,布尔值isInternal,布尔值forDescribeParameterEncryption)+409    System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔Boolean ReturnStream,布尔异步,Int32超时,任务和任务,布尔asyncWrite,布尔inRetry,SqlDataReader ds,布尔describeParameterEncryptionRequest)+2127    System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔值returnStream,字符串方法,TaskCompletionSource 1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) +911 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +64 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +240 System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +41 System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +12 System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__c(DbCommand t, DbCommandInterceptionContext 1 c)+14    System.Data.Entity.Infrastructure.Interception.InternalDispatcher 1.Dispatch(TTarget target, Func 3操作,TInterceptionContext拦截上下文,Action 3 executing, Action 3执行)+72    System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand命令,DbCommandInterceptionContext拦截上下文)+402    System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior行为)+166    System.Data.Common.DbCommand.ExecuteReader(CommandBehavior行为)+12    System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryInternal(字符串commandText,字符串entitySetName,ExecutionOptions执行选项,Object []参数)+266    System.Data.Entity.Core.Objects。<> c__DisplayClass69 1.<ExecuteStoreQueryReliably>b__68() +44 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func 1函数,IDbExecutionStrategy执行策略,布尔值startLocalTransaction,布尔值releaseConnectionOnSuccess)+288    System.Data.Entity.Core.Objects。<> c__DisplayClass69 1.<ExecuteStoreQueryReliably>b__67() +167 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func 1操作)+190    System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryReliably(字符串commandText,字符串entitySetName,ExecutionOptions执行选项,Object []参数)+462    System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQuery(String commandText,Object [] parameters)+83    C:\ KittiCutieFashion \ KittiCutieFashion.Domain \ Concrete \ EFDbContext.cs:42中的KittiCutieFashion.Domain.Concrete.EFDbContext.Kitti_getProductGroupDetail(Guid ProductGroupId)    C:\ KittiCutieFashion \ KittiCutieFashion.Domain \ Concrete \ EFDbContext.cs:82中的Kit(Cuuid ProductGroupId)中的KittiCutieFashion.Domain.Concrete.EFDbContext.getProductDetailsFromSP    C:\ KittiCutieFashion \ KittiCutieFashion.Domain \ Concrete \ EFProductDetailRepository.cs:33中的KittiCutieFashion.Domain.Concrete.EFProductDetailRepository.get_ProductDetail()    C:\ KittiCutieFashion \ KittiCutieFashion.WebUI \ Controllers \ ProductDetailController.cs:43中的KittiCutieFashion.WebUI.Controllers.ProductDetailController.ProductDetailView(String returnUrl)    lambda_method(Closure,ControllerBase,Object [])+103    System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,Object []参数)+14    System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary 2 parameters) +157 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary 2个参数)+27    System.Web.Mvc.Async。<> c.b__9_0(IAsyncResult asyncResult,ActionInvocation innerInvokeState)+22    System.Web.Mvc.Async.WrappedAsyncResult 2.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase 1.End()+49    System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)+32    System.Web.Mvc.Async.AsyncInvocationWithFilters.b__11_0()+50    System.Web.Mvc.Async。<> c__DisplayClass11_1.b__2()+228    System.Web.Mvc.Async。<> c__DisplayClass7_0.b__1(IAsyncResult asyncResult)+10    System.Web.Mvc.Async.WrappedAsyncResult 1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase 1.End()+49    System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)+34    System.Web.Mvc.Async。<> c__DisplayClass3_6.b__3()+35    System.Web.Mvc.Async。<> c__DisplayClass3_1.b__5(IAsyncResult asyncResult)+100    System.Web.Mvc.Async.WrappedAsyncResult 1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase 1.End()+49    System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)+27    System.Web.Mvc。<> c.b__152_1(IAsyncResult asyncResult,ExecuteCoreState innerState)+11    System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase 1.End()+49    System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)+45    System.Web.Mvc。<> c.b__151_2(IAsyncResult asyncResult,控制器控制器)+13    System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +22 System.Web.Mvc.Async.WrappedAsyncResultBase 1.End()+49    System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)+26    System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)+10    System.Web.Mvc。<> c.b__20_1(IAsyncResult asyncResult,ProcessRequestState innerState)+28    System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase 1.End()+49    System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)+28    System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult结果)+9    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+9748493    System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep步骤)+48    System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值并已完成同步)+159


版本信息:Microsoft .NET Framework版本:4.0.30319; ASP.NET版本:4.7.2558.0

我正在使用的sql server版本恰好是2005,不确定是否与此有关。

0 个答案:

没有答案