Search code examples
c#sqlitedapper

Dapper query with SQLite in C# throws error "'Must add values for the following parameters"


I am trying to execute the following Dapper query against a SQLite 3 database:

conn.QuerySingleOrDefault<DalTableConfig>
     ("select id, modelId, tableName, schemaName 
      from tables where tableName = $tableName;", new {tableName});

Executing this line, I get this error:

System.InvalidOperationException: 'Must add values for the following parameters: $tableName'

Google brings up quite a few results, a common one being that the parameters are case sensitive, but that doesn't apply here, the cases do match.

StackOverflow has one other question that has an unaccepted yet valid answer, which is a workaround as follows:

var parameters = new DynamicParameters();
parameters.Add("@tableName" , tableName);
conn.QuerySingleOrDefault<DalTableConfig>("select id, modelId, tableName, schemaName from tables where tableName = $tableName;", parameters);

This works without the exception I was getting before, but I still want to know why the simpler anonymous object parameter doesn't work.

I have debugged into the Dapper source and come across where the problem seems to arise. The exception is thrown on the second of these lines from the SqlMapper class's QuerySingleOrDefault method:

var command = new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.None);
return QueryRowImpl<T>(cnn, Row.SingleOrDefault, ref command, typeof(T));

Here param contains the correct tableName property, and command has this in its Parameters collection. When I examine the QueryRowImpl method I find this code:

private static T QueryRowImpl<T>(IDbConnection cnn, Row row, ref CommandDefinition command, Type effectiveType)
{
    object param = command.Parameters;
    var identity = new Identity(command.CommandText, command.CommandType, cnn, effectiveType, param?.GetType());
    var info = GetCacheInfo(identity, param, command.AddToCache);

    IDbCommand cmd = null;
    IDataReader reader = null;

    bool wasClosed = cnn.State == ConnectionState.Closed;
    try
    {
        cmd = command.SetupCommand(cnn, info.ParamReader);

        if (wasClosed) cnn.Open();
        reader = ExecuteReaderWithFlagsFallback(cmd, wasClosed, (row & Row.Single) != 0
            ? CommandBehavior.SequentialAccess | CommandBehavior.SingleResult // need to allow multiple rows, to check fail condition
            : CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow);

This fails on the below line. It is this method that throws the exception:

ExecuteReaderWithFlagsFallback

In the above code, the CommandDefinition command argument still has a valid Parameters collection, but the local variable IDbCommand cmd comes out of SetupCommand with, strangely enough, two empty Parameters collections. So there seems to be some fault with SetupCommand or info.ParamReader that causes the parameters to be lost


Solution

  • I was able to reproduce this issue in a test. The test below shows that OP's original code works when using @tableName instead of $tableName inside SQL.

    public class DalTableConfig
    {
        public int Id { get; set; }
        public string ModelId { get; set; }
        public string TableName { get; set; }
        public string SchemaName { get; set; }
    }
    
    public class DapperTests
    {
        [Fact]
        public void StackOverflowDapperTest()
        {
            using var conn = new SqliteConnection("Data Source=test.db");
    
            conn.Execute("drop table if exists tables");
            conn.Execute("create table tables(id int, modelId varchar(255), tableName varchar(255), schemaName varchar(255))");
            conn.Execute("insert into tables select 1, 'modelId1', 'tableName1', 'schemaName1'");
    
            const string sql = @"
                select 
                    id
                    ,modelId
                    ,tableName
                    ,schemaName 
                from 
                    tables 
                where 
                   tableName = @tableName"; // here's the fix
    
            const string tableName = "tableName1";
    
            var actual = conn.QuerySingleOrDefault<DalTableConfig>(sql, new {tableName});
    
            Assert.NotNull(actual);
            Assert.Equal(tableName, actual.TableName);
        }
    }