When using a stored procedure, do I need to add 'EXEC' to the command text in Dapper?
You don't need to add EXEC
to the command text when specifing the command type to CommandType.StoredProcedure
. However, you need add EXEC
if you don't specify a command type or you set the command type to CommandType.Text
.
CommandType.StoredProcedure
, you don't have to use EXEC:language-csharpusing (var connection = new SqlConnection("connectionString"))
{
var result = connection.Query("MyStoredProcedure", commandType: CommandType.StoredProcedure).ToList();
}
CommandType.Text
, you must use EXEC:language-csharpusing (var connection = new SqlConnection("connectionString"))
{
var result = connection.Query("EXEC MyStoredProcedure", commandType: CommandType.Text).ToList();
}
using (var connection = new SqlConnection("connectionString"))
{
var result = connection.Query("EXEC MyStoredProcedure").ToList();
}