So, presence of @whatever in SQL command string gives me a fatal error(Fatal error encountered during command execution.) but not when I execute the same query in MySQL Query Browser.
This is the simple example where I give a row number for every John in my table and show them in datagridview:
private void button1_Click(object sender, EventArgs e)
{
_dataTable.Clear();
try
{
_conn = new MySqlConnection(Cs);
_conn.Open();
string name = "john";
//This is the string that gives me an error
string ConnString = "SELECT name,lastname,(@Row := @Row + 1) AS RowNumber FROM person JOIN (SELECT @Row := 0) r WHERE name = '"+name+"'";
//This one doesn't gives an error
//string ConnString = "SELECT name,lastname FROM person WHERE name = '" + name + "'";
_cmd = new MySqlCommand
{
Connection = _conn,
CommandText = ConnString
};
_cmd.ExecuteNonQuery();
_da = new MySqlDataAdapter(_cmd);
_da.Fill(_dataTable);
_cb = new MySqlCommandBuilder(_da);
dataGridView1.DataSource = _dataTable;
dataGridView1.DataMember = _dataTable.TableName;
dataGridView1.AutoResizeColumns();
_conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (_conn != null) _conn.Close();
}
}
Why is this happening and how can I fix this? I've seen lot of questions about this but not a concrete answer for it.