2

I've been trying to run this code:

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();
        }
    }
}

But I get the following exception:

System.NullReferenceException: 'Object reference not set to an instance of an object.'
This exception was originally thrown at this call stack:
  MySql.Data.MySqlClient.MySqlConnection.Reader.set(MySql.Data.MySqlClient.MySqlDataReader)

My first guess is that the variable is being treated as a SqlParameter, and since I'm not passing any argument to it, my code fails. Is there a way to run a query like that using Dapper?

3
  • It's probably bugging out because the column has no name. Perhaps just use db.ExecuteScalar(sql) as there is only one column in the SELECT Commented Mar 11, 2022 at 16:23
  • There are more efficient ways of setting an integer to 0 in c#. Can you make the example less contrived/post a query that is reasonable? As is I'd vote to close it as a typo because the code is of very little practical use to anyone Commented Mar 11, 2022 at 16:30
  • @CaiusJard I've just edited the example to more clear on what I want to accomplish, although the previous one ran in the same exception. I need count the registers in table1 and return it as a column in the query of table2. I have some constraints due the version of the MySql server I'm using, so my query have to be like that. The query runs as expected, but when I try to run it in C#, I get that error. Commented Mar 11, 2022 at 16:50

3 Answers 3

6

I've found this in the Dapper documentation:

In order to use Non-parameter SQL variables with MySql Connector, you have to add the following option to your connection string:

Allow User Variables=True

Make sure you don't provide Dapper with a property to map.

So all I needed to do was to Allow User Variables=True to the connection string. It worked.

Sign up to request clarification or add additional context in comments.

Comments

0

Or you can else write your sql with a valid sql variable declaration :

var sql = @"declare @foo int = 0; select @foo;";

Note : tested with Sql Server, not MySql. I don't use it.

1 Comment

Your answer is essentially "Don't use MySQL, use something else"; you're not answering OP's question. answering on SO isn't about telling others what you think is valid or not, it's about solving someone's problem with their constraints in mind. OP's syntax is valid SQL for MySQL, your syntax is for SQL Server. Neither of which are more valid than the other. Besides, OP is asking about an issue with Dapper, which you aren't even mentionning here. Please edit your answer accordingly.
-1

The NullReferenceException you are receiving is likely because you have not defined foo as a parameter.

  using (var db = new MySqlConnection(@"mysql_connstr_here"))
  {
    var sql = dbConnection.QueryFirstOrDefault<int>(
      @" 
      select @foo;",
      new
      {
        foo = userAssignedVariable
      });
  }

2 Comments

In my case, @foo is not supposed to be a parameter. It's a user defined variable that is going to be set within the SQL statement, and then returned alongside some other data in the select.
I've updated my answer to assist with that. Set @foo with the user assigned variable as a new parameter. This will work :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.