如何使用现有存储过程MVC插入数据 [英] How to insert data using existing stored procedure MVC
本文介绍了如何使用现有存储过程MVC插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在visual studio 2015中工作mvc 5 entityframework 6代码优先来自现有数据库
我需要根据现有的存储过程插入数据,但我不知道如何做到这一点
下面的代码有什么问题
第1步
USE [EmployeeSystem]
GO
/ ******对象:StoredProcedure[dbo]。[spAddDepartment]脚本日期:13/02/2018 12:32:38 AM ****** /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER程序[dbo]。[spAddDepartment]
@DepartmentName nvarchar(50)
as
开始
插入Departments值(@ DepartmentName,1)
结束
解决方案
嗨ahmedabosalah,
根据你的说法说明,我创建了一个演示,它抛出异常"存储更新,插入或删除语句影响了意外的行数(0)。",当我更改下面的存储过程时,它运行良好
CREATE PROCEDURE [dbo]。[spAddDepartment]
@DepartmentName [nvarchar](max)
AS
BEGIN
INSERT [dbo ]。[部门]([部门名称],GroupId)
VALUES(@ DepartmentName,1)
DECLARE @DepartmentId int
SELECT @DepartmentId = [DepartmentId]
FROM [dbo]。[部门]
WHERE @@ ROWCOUNT> 0和[DepartmentId] = scope_identity()
SELECT t0。[DepartmentId]
FROM [dbo]。 [部门] AS t0
WHERE @@ ROWCOUNT> 0和t0。[DepartmentId] = @DepartmentId
END祝你好运,
张龙
I work in visual studio 2015 mvc 5 entityframework 6 code first from existing database
I need to insert data based on existing stored procedure but i dont know How to do that
what is wrong in code below
step 1 USE [EmployeeSystem] GO /****** Object: StoredProcedure [dbo].[spAddDepartment] Script Date: 13/02/2018 12:32:38 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER Procedure [dbo].[spAddDepartment] @DepartmentName nvarchar(50) as Begin Insert into Departments values(@DepartmentName,1) End
解决方案
Hi ahmedabosalah,
According to your description, I create a demo, which throw a exception "Store update, insert, or delete statement affected an unexpected number of rows (0).", when I change the store procedure like below, it works well
CREATE PROCEDURE [dbo].[spAddDepartment] @DepartmentName [nvarchar](max) AS BEGIN INSERT [dbo].[Departments]([DepartmentName],GroupId) VALUES (@DepartmentName,1) DECLARE @DepartmentId int SELECT @DepartmentId = [DepartmentId] FROM [dbo].[Departments] WHERE @@ROWCOUNT > 0 AND [DepartmentId] = scope_identity() SELECT t0.[DepartmentId] FROM [dbo].[Departments] AS t0 WHERE @@ROWCOUNT > 0 AND t0.[DepartmentId] = @DepartmentId ENDBest regards,
Zhanglong
这篇关于如何使用现有存储过程MVC插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文