一、無參存儲過程
第一步:創建一個不帶參數的存儲過程,代碼如下:
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 }