i have select query which i made a method off so i can call it anywhere instead of writing query command again and again
public string mysql_execute_selectfromwhere(string select ,string from, string where, string equalsto)
{
ConnMySql.Open();
MySqlCommand com = ConnMySql.CreateCommand();
com.CommandText = "SELECT @1 FROM @2 WHERE @3=@4";
com.Parameters.AddWithValue("@1", select);
com.Parameters.AddWithValue("@2", from);
com.Parameters.AddWithValue("@3", where);
com.Parameters.AddWithValue("@4", equalsto);
string returnstring = Convert.ToString(com.ExecuteScalar());
ConnMySql.Close();
return returnstring;
}
this is how im calling this method
string get = mysql_execute_selectfromwhere("label_name", "label_fields", "lable_id", "17");
im getting following mysql syntax error and i cant seem to understand it properly
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near "label_fields' WHERE 'lable_id'='17" at line 1
please also highlight if there is any other problem with my procedure. Thank you
CodePudding user response:
Try:
com.CommandText = String.Format("SELECT {0} FROM {1} WHERE {2}=@equalsto", select, from, where);
command.Parameters.Add("@equalsto", SqlDbType.Int);
command.Parameters["@equalsto"].Value = equalsto;
CodePudding user response:
com.CommandText = "SELECT @1 FROM @2 WHERE @3=@4"; try this instead of com.CommandText = "SELECT '@1' FROM '@2' WHERE '@3' ='@4'"; single quotation with variable which are you using in command text.