c# MySqlCommand似乎没有解析参数

本文关键字:参数 MySqlCommand | 更新日期: 2023-09-27 18:09:12

我试图运行一个MySqlCommand与一个参数,但当我看在SQL日志,参数显示。

我尝试过@和?,也尝试过在我的sqlconnection字符串中使用'old syntax=yes'。

这是我的代码。

bool CheckLoginDetails(string username, string password){
    DataTable dt = new DataTable ();
    MySqlCommand command = new MySqlCommand ("select * from `accounts` where `username` = '@username';", SL.Database.connection);
    command.Parameters.AddWithValue ("@username", username);
    dt.Load (command.ExecuteReader());
}

这是SQL日志。

150823  3:19:50    22 Connect   root@localhost on unity_test
       22 Query SHOW VARIABLES
       22 Query SHOW COLLATION
       22 Query SET character_set_results=NULL
       22 Init DB   unity_test
150823  3:19:52    22 Query select * from `accounts` where `username` = '@username'

我使用的是。net 2.0,我不确定mysql的dll版本,我发现在这里:http://forum.unity3d.com/threads/reading-database-and-or-spreadsheets.11466/

由于Unity,我无法升级。net。任何建议将非常感激!

c# MySqlCommand似乎没有解析参数

建议(未测试):
bool CheckLoginDetails(string username, string password){
    string sql = select * from accounts where username = ?";
    MySqlCommand command  = new MySqlCommand(sql);
    command.Parameters.Add(new MySqlParameter("", username));
    MySqlDataReader r = command.ExecuteReader();
    ...
}

关键是"?"应该起作用

尝试命令参数。添加("@用户名",MysqlDbType.Varchar)。Value = username;

试试这个,改变

MySqlCommand command = new MySqlCommand ("select * from `accounts` where `username` = '@username';", SL.Database.connection);
command.Parameters.AddWithValue ("@username", username);

MySqlCommand command = new MySqlCommand ("select * from accounts where username = @username;", SL.Database.connection);
command.Parameters.Add("@username", username);

试一下

bool CheckLoginDetails(string username, string password){
DataTable dt = new DataTable ();
MySqlCommand command = new MySqlCommand ("select * from accounts where username = @username;", SL.Database.connection);
command.Parameters.AddWithValue ("@username", username);
dt.Load (command.ExecuteReader());

}