我有应用程序在MVC中运行,我打算将数据从MVC应用程序C#发送到存储过程。我已经测试了我的存储过程,如果我从SQL Server Management Studio执行它就可以工作,而在C#中,我从网页(视图)获取所有值,以便在预期调用存储过程的地方运行,所以我猜我是在参数传递中做错了什么!!!
它不在数据库中存储数据
非常感谢提前
USE [MySolution01_DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[CreateNewFunctionsNavigation]
@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
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; }
}
public void CreateFunctionNavigation(CreateFunctionNavigation_SP_Map _entity)
{
using(var dbContext = new FunctionContext())
{
CreateFunctionNavigation_SP_Map modelObj = new CreateFunctionNavigation_SP_Map();
var parameters = new[] {
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 )
};
dbContext.Database.SqlQuery<CreateFunctionNavigation_SP_Map>("exec CreateNewFunctionsNavigation", parameters);
}
}
我已更改C#代码但仍无法存储数据
dbContext.Database.SqlQuery<CreateFunctionNavigation_SP_Map>("EXEC CreateNewFunctionsNavigation @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)
);
答案 0 :(得分:0)
您可以使用SQL Profiler调试它,该命令显示针对服务器执行的命令。无论如何,请尝试以下
dbContext.Database.SqlQuery(“CreateNewFunctionsNavigation @FunctionName,@ FunctionHierarchy_Level,@ ControllerName,@ ActionName, @Function_ParentsFunctionID“,参数);
<强>更新强>
由于上面的答案对你没有用,你可以尝试像这样手动创建查询字符串..
var query = string.Format("EXEC CreateNewFunctionsNavigation " +
"@FunctionName = {0}, " +
"@FunctionHierarchy_Level = {1}, " +
"@ControllerName = {2}, " +
"@ActionName = {3}, " +
"@Function_ParentsFunctionID = {4}",
_entity.FunctionName,
_entity.FunctionHierarchy_Level,
entity.ControllerName,
_entity.ActionName,
_entity.Function_ParentsFunctionID);
dbContext.Database.SqlQuery<CreateFunctionNavigation_SP_Map>(query);