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 OUTPUT parameters in Dapper?
<p>To use a stored procedure with an output parameter, you need to follow these steps:</p> <ol> <li>Create your stored procedure in your database with an output parameter:</li> </ol> <pre><code class="language-sql">CREATE PROCEDURE MyStoredProcedure @InputParam1 INT, @OutputParam2 INT OUTPUT AS BEGIN -- Your stored procedure logic here END </code></pre> <ol start="2"> <li>Create a <code>DynamicParameters</code> object and add parameter to it. For an output parameter, you must specify <code>ParameterDirection.Output</code></li> <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 your parameters</li> <li>Retrieve the parameter value from the parameters passed to the stored procedure:</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("@OutputParam2", dbType: DbType.Int32, direction: ParameterDirection.Output); // Execute the stored procedure var result = connection.Execute( "MyStoredProcedure", parameters, commandType: CommandType.StoredProcedure ); // Get the output parameter value var outputParam2Value = parameters.Get<int>("@OutputParam2"); // Do something with the output parameter value.. Console.WriteLine($"Output parameter value: {outputParam2Value}"); } </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
?