简体   繁体   中英

unable to use mysql assigning operator in c#

I am trying to use assigning operator @a := 0 for crossjoin (mysql) in my c# program. But it throws error saying

Fatal error encountered during command execution. Parameter '@a' must be defined.

the below query executes well in my mysql tool. but fails to perform while using it in my C# programming.

string str = "select t1.x1,(@a := @a + t1.x1) x2 from table1 t1 cross join (select @a := 0) params";
MySqlCommand cmd = new MySqlCommand(str, connection);
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();

Output should be:

x1 x2

1 1

2 3

3 6

4 10

5 15

How do i solve this? or is there a work around for this?

By default, MySQL Connector/NET does not allow variables in SQL statements, only command parameters. If you use MySqlConnector you will get a more helpful error message:

Parameter '@a' must be defined. To use this as a variable, set 'Allow User Variables=true' in the connection string.

As this states, add AllowUserVariables=true to your connection string to permit the use of variables in SQL commands.

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