How to run Dapper/MySql queries in C# using user-defined variables
<p>I've been trying to run this code: </p>
<pre class="brush:php;toolbar:false;">using System;
using Dapper;
using MySql.Data.MySqlClient;
namespace DapperTests
{
class Program
{
static void Main(string[] args)
{
using (var db = new MySqlConnection(@"mysql_connstr_here"))
{
var sql = @"
set @foo := (select count(*) from table1);
select table2.*, @foo from table2;";
var result = db.Query(sql);
}
Console.ReadLine();
}
}
}</pre>
<p>But I get the following exception: </p>
<pre class="brush:php;toolbar:false;">System.NullReferenceException: 'The object reference is not set to an instance of the object. '
This exception was initially thrown in this call stack:
MySql.Data.MySqlClient.MySqlConnection.Reader.set(MySql.Data.MySqlClient.MySqlDataReader)</pre>
<p>My initial guess was that the variable was treated as a SqlParameter, and since I wasn't passing any parameters, my code failed.
Is there a way to run a query like this using Dapper? </p>