c# - 可以在 C# 连接器中使用 MySQL @session 变量吗?

标签 c# asp.net mysql mysql-connector

我有一个使用 MySQL session 变量的查询(注意@rank 变量)

SELECT Rank, UserId, CurrentVDOT
FROM
    (
        SELECT @rank := @rank + 1 AS Rank, UserId, MaxVDOT AS CurrentVDOT
        FROM
            (
                SELECT UserId, MAX(VDOT) AS MaxVDOT
                FROM
                    (
                        SELECT U.UserId, U.VDOT
                        FROM
                            (
                                SELECT UserId, MAX(Created) AS Created
                                FROM UserVDOT
                                GROUP BY UserId
                            ) G
                        INNER JOIN UserVDOT U
                        ON U.UserId = G.UserId
                            AND U.Created = G.Created
                    ) M
                GROUP BY UserId
                ORDER BY MaxVDOT DESC
            ) R, (SELECT @rank := 0) foo
    ) F
WHERE F.UserId = @UserId;

如果我尝试针对 C# MySQL 连接器执行此操作,它会尝试告诉我需要将 @rank 声明为输入参数变量。

有什么办法解决这个问题吗?

谢谢。

最佳答案

Hello Allison!

I was facing the same problem recently, but I found that by default, the SQL connection disables the use of variables in queries. To solve this problem you can enable the connection string as follows:

连接字符串:

server=192.168.0.0;password=root;User Id=root;Persist Security Info=True;database=my_db;Allow User Variables=True

您必须输入连接参数 Allow User Variables=True 才能使其正常工作。

I hope I've helped.

Hiago Takaki

关于c# - 可以在 C# 连接器中使用 MySQL @session 变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16348028/

相关文章:

mysql 插入 - 跳过重复语法错误 - sprintf(不存在的地方)

c# - System.Security.Principal.WindowsIdentity 和 WinForms 身份验证

C# Moq Entity Framework 6 数据上下文添加操作

asp.net - 从 Application_BeginRequest() 中设置后,AsyncLocal 值为 Null

c# - 未知服务器标记 'rsweb:ReportViewer'

MySQL LOAD DATA LOCAL INFILE 当第一行被 "包围时什么都不做

c# - C#中的内存异常

c# - C# 中的复合赋值 ^= 是原子的吗?

asp.net - Application_Start() 在 IIS7.5 托管的 MVC 5 应用程序中调用两次

mysql - 在 Mysql 中使用正则表达式替换