您的位置:首页 > 其它

EntityFramework6 Execute Storeprocedure

2015-10-26 18:14 232 查看
整个逻辑十分简单,不再赘述,仅作为记录!

1.Function

[Route("api/Module/{id}/{name}")]
public object GetApplicationName(int id,string name)
{
System.Data.SqlClient.SqlParameter[] parameters =
{
new System.Data.SqlClient.SqlParameter("@Id",SqlDbType.Int),
new System.Data.SqlClient.SqlParameter("@Name",SqlDbType.VarChar,50),
new System.Data.SqlClient.SqlParameter("@Result",SqlDbType.VarChar,50),
};
parameters[0].SqlValue = id;
parameters[1].SqlValue = name;
parameters[2].SqlValue = "";
parameters[2].Direction = ParameterDirection.Output;
if(entityContext.Database.Connection.State == ConnectionState.Closed)
entityContext.Database.Connection.Open();
System.Data.Common.DbCommand cmd = entityContext.Database.Connection.CreateCommand();
cmd.CommandText = "prc_demo";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(parameters);
int result = cmd.ExecuteNonQuery();
string output = parameters[2].Value.ToString();
cmd.Connection.Close();
return "result:" + result + ",output:" + output;
}2.存储过程
USE [EPDB]
GO

/****** Object: StoredProcedure [dbo].[prc_demo] Script Date: 2015/10/26 16:14:17 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author: justin
-- Create date: 2015-10-26
-- Description: EF work with procedure demo
-- =============================================
ALTER PROCEDURE [dbo].[prc_demo]
-- Add the parameters for the stored procedure here
@Id int,
@Name varchar(50),
@Result varchar(50) output
AS
BEGIN
begin try
begin transaction
update Applications set Applications.Name = @Name where Applications.Id = @Id;
set @Result = 'SUCCESS';
commit transaction
end try
begin catch
rollback transaction
set @Result = 'FAIL';
end catch
END

GO

3.效果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  EF 存储过程