简体   繁体   中英

Why does this dapper query return null?

I have a mysql statement that I am passing to the dapper query method and it is returning a null object. I have created a POCO class to hold the returned result. I tested the query directly through a sql editor and it returned a result. Here is the code:

public ViewEmail GetViewEmail(string xId, int headerId)
{
    using (IDbConnection connection = new MySqlConnection(_connectionString))
    {
        string sql = "SELECT * FROM ViewEmail " +
                     "WHERE Header_id = @Header_id " +
                     "AND x_id= '@x_id'";

        var viewEmail = connection
            .Query<ViewEmail>(sql, new { Header_id = HeaderId, x_id = xId })
            .SingleOrDefault();

        return viewEmail;
    }
}

When using Prepared Statements , no need to wrap the parameter with ' . Try this instead:

string sql = "SELECT * FROM ViewEmail " +
             "WHERE Header_id = @Header_id " +
             "AND x_id= @x_id";

See Documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM