简体   繁体   English

c#MySqlCommand似乎没有解析参数

[英]c# MySqlCommand doesn't appear to be resolving Parameters

I'm trying to run a MySqlCommand with a parameter, but when I look in the SQL logs, the parameter is showing up. 我正在尝试使用参数运行MySqlCommand,但是当我查看SQL日志时,参数显示出来。

I've tried with both @ and ?, tried with 'old syntax=yes' in my sqlconnection string also. 我已尝试使用@和?,在我的sqlconnection字符串中尝试使用'old syntax = yes'。

Here's my code. 这是我的代码。

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

Here's the SQL logs. 这是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'

I'm using .net 2.0 and I'm unsure of the mysql dll version, I found that here: http://forum.unity3d.com/threads/reading-database-and-or-spreadsheets.11466/ 我正在使用.net 2.0而且我不确定mysql dll版本,我发现这里: http//forum.unity3d.com/threads/reading-database-and-or-spreadsheets.11466/

I'm unable to upgrade .net due to Unity. 由于Unity,我无法升级.net。 Any advice would be greatly appreciated! 任何建议将不胜感激!

Suggestion (not tested): 建议(未测试):

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

The main point is that "?" 重点是“?” should work. 应该管用。

Try command.Parameters.Add("@username", MysqlDbType.Varchar).Value = username; 尝试command.Parameters.Add(“@ username”,MysqlDbType.Varchar).Value = username;

Should work ! 应该管用 !

Try this, change 试试这个,改变

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

to

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

Try this(suggestion) 试试这个(建议)

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM