ZZZ Code AI
Report Issues
Suggest Features
Login
Dapper Tools
Chat
Code Explain
Seed Data Generator
Sql Generator
Stored Procedure Generator
Entity to Table Converter
Table to Entity Converter
Sql Injection Detector
More Tools
How to use a stored procedure with INPUT parameters in Dapper
<p>To use a stored procedure with input parameters, you need to follow these steps:</p> <ol> <li>Create your stored procedure in your database with input parameters:</li> </ol> <pre><code class="language-sql">CREATE PROCEDURE MyStoredProcedure @InputParam1 INT, @InputParam2 INT AS BEGIN -- Your stored procedure logic here END </code></pre> <ol start="2"> <li>Use a <a href="https://www.learndapper.com/dapper-query">querying</a> or <a href="https://www.learndapper.com/non-query">execute</a> method with an anonymous types for your parameters</li> </ol> <pre><code class="language-csharp">using(var connection = new SqlConnection(connectionString)) { // Define parameters including your output parameters var parameters = new DynamicParameters(); parameters.Add("@InputParam1", inputParam1Value); parameters.Add("@InputParam2", inputParam2Value); // Execute the stored procedure var result = connection.Query<Customer>( "MyStoredProcedure", new { InputParam1 = inputParam1Value, InputParam2 = inputParam2Value}, commandType: CommandType.StoredProcedure ).ToList(); } </code></pre> <ol start="3"> <li>Or create a <code>DynamicParameters</code> object and add parameter to it.</li> </ol> <pre><code class="language-csharp">using(var connection = new SqlConnection(connectionString)) { // Define parameters including your output parameters var parameters = new DynamicParameters(); parameters.Add("@InputParam1", inputParam1Value); parameters.Add("@InputParam2", inputParam2Value); // Execute the stored procedure var result = connection.Query<Customer>( "MyStoredProcedure", parameters, commandType: CommandType.StoredProcedure ).ToList(); } </code></pre>
Sponsored by
Dapper Plus
This field is required
A text with a minimum of 10 characters is required
Send
Legal & Licensing
Answer generated by AI may produce inaccurate information about code, people, facts, and more.
Advertising Break!
5
seconds left
Did you know...
That you can now sponsor this project on
GitHub
?