一、无参存储过程
第一步:创建一个不带参数的存储过程,代码如下:
1 CREATE PROCEDURE [dbo].[QueryRoleNoParms] 2 3 AS 4 BEGIN 5 SELECT * FROM T_Role; 6 END
第二步:执行存储过程,返回结果,代码如下:
1 private List<Role> ExecuteStoredProcedureNoParms() 2 { 3 using (IDbConnection con = OpenConnection()) 4 { 5 List<Role> roleList = new List<Role>(); 6 roleList = con.Query<Role>("QueryRoleNoParms", 7 null, 8 null, 9 true, 10 null, 11 CommandType.StoredProcedure).ToList(); 12 return roleList; 13 } 14 }
二、带输入输出参数的存储过程
第一步:创建一个带输入输出参数的存储过程,代码如下:
1 CREATE PROCEDURE [dbo].[QueryRoleWithParms] 2 @RoleId int, 3 @RoleName nvarchar(256)='' out 4 AS 5 BEGIN 6 SELECT @RoleName = RoleName FROM T_Role WHERE RoleId =@RoleId 7 END
第二步:执行存储过程,返回执行结果,代码如下:
1 private string ExecuteStoredProcedureWithParms() 2 { 3 DynamicParameters dp = new DynamicParameters(); 4 dp.Add("@RoleId", "1"); 5 dp.Add("@RoleName", "", DbType.String, ParameterDirection.Output); 6 using (IDbConnection con = OpenConnection()) 7 { 8 con.Execute("QueryRoleWithParms", dp, null, null, CommandType.StoredProcedure); 9 string roleName = dp.Get<string>("@RoleName"); 10 return roleName; 11 } 12 }