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
When using a stored procedure, do I need to add parameter names to the command text in Dapper?
<p>You don't need to add parameter names to the command text when specifing the command type to <code>CommandType.StoredProcedure</code>. However, you need add <code>EXEC</code> and parameter names if you don't specify a command type or you set the command type to <code>CommandType.Text</code>.</p> <ol> <li>With <code>CommandType.StoredProcedure</code>, you don't have to use EXEC:</li> </ol> <pre><code class="language-csharp">using (var connection = new SqlConnection("connectionString")) { var parameters = new DynamicParameters(); parameters.Add("@Parameter1", value1); parameters.Add("@Parameter2", value2); var result = connection.Query("MyStoredProcedure", parameters, commandType: CommandType.StoredProcedure).ToList(); } </code></pre> <ol start="2"> <li>With <code>CommandType.Text</code>, you must use EXEC and specify parameter names:</li> </ol> <pre><code class="language-csharp">using (var connection = new SqlConnection("connectionString")) { var parameters = new DynamicParameters(); parameters.Add("@Parameter1", value1); parameters.Add("@Parameter2", value2); var result = connection.Query("EXEC MyStoredProcedure @Parameter1, @Parameter2", parameters, commandType: CommandType.Text).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
?