我在C#中有一个ASP.NET MVC应用程序,我在调用存储过程CreateFunctionNavigation
。我有问题调用该存储过程以及参数。我的模型类为;
Model
上课
public class CreateFunctionNavigation_SP_Map
{
public CreateFunctionNavigation_SP_Map()
{
}
[StringLength(250)]
[Required(ErrorMessage = "Required Function Title")]
[Display(Name = "Function Title")]
public string FunctionName { get; set; }
[Required(ErrorMessage = "Required Function Hierarchy; i.e Where Function Exists In Hierarchy Tree \n Top-Level Start From 1 ")]
[Display(Name = "Function Hierarchy Level")]
public int FunctionHierarchy_Level { get; set; }
[StringLength(250)]
[Required(ErrorMessage = "Required Controller Title")]
[Display(Name = "Controller Title")]
public string ControllerName { get; set; }
[StringLength(250)]
[Required(ErrorMessage = "Required Action Title")]
[Display(Name = "Action Title")]
public string ActionName { get; set; }
[Required(ErrorMessage = "Required Function Parent - Child Relation ID \n Put 0 In Case Given Function doesn't Have Any Parent Function ")]
[Display(Name = "Function Parent's FunctionID")]
public int Function_ParentsFunctionID { get; set; }
}
存储过程:
ALTER PROCEDURE [dbo].[CreateFunctionNavigation]
@FunctionName nvarchar(250),
@Hierarchy_Level INT,
@Function_identity INT OUTPUT,
@ControllerName nvarchar(250),
@Controller_identity INT OUTPUT,
@ControllerInFunction_identity INT OUTPUT,
@ActionName nvarchar(250),
@Action_identity INT OUTPUT,
@ActionInFunction_identity INT OUTPUT,
@Function_ParentsFunctionID INT,
@Function_ParentsFunction_identity INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [dbo].[Navigation_Functions] ([FunctionName],[Hierarchy_Level])
VALUES(@FunctionName, @Hierarchy_Level)
SET @Function_identity = SCOPE_IDENTITY()
INSERT INTO [dbo].[Navigation_FunctionController] ([ControllerName])
VALUES(@ControllerName)
SET @Controller_identity = SCOPE_IDENTITY()
INSERT INTO [dbo].[Navigation_FunctionInController] ([Function_ID], [ControllerID])
VALUES (@Function_identity, @Controller_identity)
SET @ControllerInFunction_identity = SCOPE_IDENTITY()
INSERT INTO [dbo].[Navigation_FunctionAction] ([ActionName], [ControllerID])
VALUES (@ActionName, @Controller_identity)
SET @Action_identity = SCOPE_IDENTITY()
INSERT INTO [dbo].[Navigation_FunctionInAction] ([ActionID], [Function_ID])
VALUES (@Action_identity, @Function_identity)
SET @ActionInFunction_identity = SCOPE_IDENTITY()
INSERT INTO [dbo].[Navigation_FunctionHierarchy] ([Function_IDs], [Parent_Function_ID])
VALUES (@Function_identity, @Function_ParentsFunctionID)
SET @Function_ParentsFunction_identity = SCOPE_IDENTITY()
RETURN
END
现在在C#类中我尝试使用传递参数来运行此存储过程但在SQL Server Profiler中我无法看到是否未调用此存储过程。
运行存储过程的C#代码
var _result = dbContext.Database.SqlQuery<CreateFunctionNavigation_SP_Map>("exec CreateFunctionNavigation @FunctionName @FunctionHierarchy_Level @ControllerName @ActionName @Function_ParentsFunctionID",
new SqlParameter("FunctionName",_entity.FunctionName),
new SqlParameter("FunctionHierarchy_Level",_entity.FunctionHierarchy_Level),
new SqlParameter("ControllerName", _entity.ControllerName),
new SqlParameter("ActionName", _entity.ActionName),
new SqlParameter("Function_ParentsFunctionID",_entity.Function_ParentsFunctionID)
);
但是,如果我通过只调用没有参数来运行这个存储过程,当然只是使用select语句存储过程然后它可以工作,我也可以在SQL事件探查器中看到调用存储过程。
使用C#代码
List<CreateFunctionNavigation_SP_Map> query;
query = dbContext.Database.SqlQuery<CreateFunctionNavigation_SP_Map>("exec CreateFunctionNavigation").ToList();
所以我认为问题是C#类,我试图用参数调用SP。我严重困难,尝试了不同的选择,但不知道我做错了什么。非常感谢提前
答案 0 :(得分:1)
您是否尝试在参数名称之间添加逗号?
dbContext.Database.SqlQuery<CreateFunctionNavigation_SP_Map>("exec CreateFunctionNavigation @FunctionName, @FunctionHierarchy_Level, @ControllerName, @ActionName, @Function_ParentsFunctionID"
更新:刚检出你的存储过程代码,看到它有一些问题。
以下是我将如何更改
ALTER PROCEDURE [dbo].[CreateFunctionNavigation]
@FunctionName nvarchar(250),
@Hierarchy_Level INT,
@ControllerName nvarchar(250),
@ActionName nvarchar(250),
@Function_ParentsFunctionID INT,
@Function_identity INT OUTPUT,
@Controller_identity INT OUTPUT,
@ControllerInFunction_identity INT OUTPUT,
@Action_identity INT OUTPUT,
@ActionInFunction_identity INT OUTPUT,
@Function_ParentsFunction_identity INT OUTPUT
这会将所有输出放在函数的末尾。
是的,在您的c#代码
上var function_identity new SqlParameter() {ParameterName = "Function_identity", Direction = ParameterDirection.Output};
var controller_identity new SqlParameter() {ParameterName = "Controller_identity", Direction = ParameterDirection.Output};
var controllerInFunction_identity new SqlParameter() {ParameterName = "ControllerInFunction_identity", Direction = ParameterDirection.Output};
var action_identity new SqlParameter() {ParameterName = "Action_identity", Direction = ParameterDirection.Output};
var actionInFunction_identity new SqlParameter() {ParameterName = "ActionInFunction_identity", Direction = ParameterDirection.Output};
var function_ParentsFunction_identity new SqlParameter() {ParameterName = "Function_ParentsFunction_identity", Direction = ParameterDirection.Output};
var _result = dbContext.Database.SqlQuery<CreateFunctionNavigation_SP_Map>("exec CreateFunctionNavigation @FunctionName, @FunctionHierarchy_Level, @ControllerName, @ActionName, @Function_ParentsFunctionID, @Function_identity out, @Controller_identity out, @ControllerInFunction_identity out, @Action_identity out, @ActionInFunction_identity out, @Function_ParentsFunction_identity out",
new SqlParameter("FunctionName",_entity.FunctionName),
new SqlParameter("FunctionHierarchy_Level",_entity.FunctionHierarchy_Level),
new SqlParameter("ControllerName", _entity.ControllerName),
new SqlParameter("ActionName", _entity.ActionName),
new SqlParameter("Function_ParentsFunctionID",_entity.Function_ParentsFunctionID),
function_identity ,
controller_identity ,
controllerInFunction_identity ,
action_identity ,
actionInFunction_identity,
function_ParentsFunction_identity
);
这可能会让你在某个地方接近。
答案 1 :(得分:0)
这是我的答案;它正在运作
List<GetNewlyCreatedNavigationsFunction_SP_Map> _query;
var function_identity_out = new SqlParameter("Function_identity", SqlDbType.Int) { Direction = System.Data.ParameterDirection.Output };
var controller_identity_out = new SqlParameter("Controller_identity", SqlDbType.Int) { Direction = System.Data.ParameterDirection.Output };
var controllerInFunction_identity_out = new SqlParameter("ControllerInFunction_identity", SqlDbType.Int) { Direction = System.Data.ParameterDirection.Output };
var action_identity_out = new SqlParameter("Action_identity", SqlDbType.Int) { Direction = System.Data.ParameterDirection.Output };
var actionInFunction_identity_out = new SqlParameter("ActionInFunction_identity", SqlDbType.Int) { Direction = System.Data.ParameterDirection.Output };
var function_ParentsFunction_identity_out = new SqlParameter("Function_ParentsFunction_identity", SqlDbType.Int) { Direction = System.Data.ParameterDirection.Output };
_query = dbContext.Database.SqlQuery<GetNewlyCreatedNavigationsFunction_SP_Map>("exec CreateFunctionNavigation @FunctionName, @Hierarchy_Level, @ControllerName, @ActionName, @Function_ParentsFunctionID, @Function_identity out, @Controller_identity out, @ControllerInFunction_identity out, @Action_identity out, @ActionInFunction_identity out, @Function_ParentsFunction_identity out",
new SqlParameter("@FunctionName", _entity.FunctionName),
new SqlParameter("@Hierarchy_Level", _entity.FunctionHierarchy_Level),
new SqlParameter("@ControllerName", _entity.ControllerName),
new SqlParameter("@ActionName", _entity.ActionName),
new SqlParameter("@Function_ParentsFunctionID", _entity.Function_ParentsFunctionID),
function_identity_out,
controller_identity_out,
controllerInFunction_identity_out,
action_identity_out,
actionInFunction_identity_out,
function_ParentsFunction_identity_out
).ToList();