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.效果
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.效果
相关文章推荐
- MySQL存储过程
- ASP程序与SQL存储过程结合使用详解
- Oracle存储过程之数据库中获取数据实例
- MSSQL自身存储过程的一个注入漏洞
- SQLServer 2005 列所有存储过程的语句
- SQLServer 2008中的代码安全(一) 存储过程加密与安全上下文
- SQL Server存储过程的基础说明
- sqlserver存储过程语法详解
- MSSQL事务的存储过程
- SQL Server 存储过程解析
- sql server动态存储过程按日期保存数据示例
- SQLserver 数据库危险存储过程删除与恢复方法
- SQL Server存储过程中使用表值作为输入参数示例
- SQLServer用存储过程实现插入更新数据示例
- sqlserver关于分页存储过程的优化【让数据库按我们的意思执行查询计划】
- sql存储过程的使用和介绍
- Sql语句与存储过程查询数据的性能测试实现代码
- 浅析SQL Server中包含事务的存储过程
- SQL Server数据库中的存储过程介绍
- mysql心得分享:存储过程