Dapper Study Notes (5)-stored procedures, dapper Study Notes

Source: Internet
Author: User

Dapper Study Notes (5)-stored procedures, dapper Study Notes

1. No parameter Stored Procedure

Step 1: Create a stored procedure without parameters. The Code is as follows:

1 CREATE PROCEDURE [dbo].[QueryRoleNoParms] 2     3 AS4 BEGIN5     SELECT * FROM T_Role;6 END

Step 2: Execute the stored procedure and return the result. The Code is as follows:

 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         }

 

2. Stored Procedures with Input and Output Parameters

Step 1: Create a stored procedure with input and output parameters. The Code is as follows:

1 CREATE PROCEDURE [dbo].[QueryRoleWithParms]2     @RoleId int,3     @RoleName nvarchar(256)='' out4 AS5 BEGIN6     SELECT @RoleName = RoleName FROM T_Role WHERE RoleId =@RoleId7 END

Step 2: Execute the stored procedure and return the execution result. The Code is as follows:

 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         }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.