I'm trying to run a SQL query using MySqlAdapter so it returns the data in a DataTable.
I'm having troubles with the parametrization. Basically it doesn't return anything and it should be returning 2 rows.
I have this code:
using (MySqlConnection connection = new MySqlConnection(CONSTANTS.dbCONNECTSTRING))
{
string sqlQuery = $"SELECT * FROM tfg_bcovi.User where userName= '@userName'";
MySqlCommand command = new MySqlCommand(sqlQuery, connection);
command.Parameters.Add(new MySqlParameter("@userName", userCtrl.UserName));
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(command);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
DataTable dtResults = dataSet.Tables[0];
}
I checked and userCtrl.UserName have the correct value so I'm asuming something is wrong with parametrization.
The problem as I said before, is that dtResults ends up empty when it whould have at least 2 rows. I checked running the query in SQL.
What am I doing wrong?
Thanks!
Just remove the single quotes around the parameter. Your drive handles that for you already.
This:
string sqlQuery = $"SELECT * FROM tfg_bcovi.User where userName= '@userName'";
Should be:
string sqlQuery = $"SELECT * FROM tfg_bcovi.User where userName= @userName";
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.