整个逻辑十分简单,不再赘述,仅作为记录!
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.效果