Je crée une méthode pour sélectionner l'ID de n'importe quelle table en passant un champ de recherche.Comment passer une table en tant que paramètre à MySqlCommand?
private int SelectId(string tabela, string campo, string valor)
{
int id = 0;
using (command = new MySqlCommand())
{
command.Connection = conn;
command.Parameters.Add("@tabela", MySqlDbType.).Value = tabela;
command.Parameters.Add("@campo", MySqlDbType.Text).Value = campo;
command.Parameters.Add("@valor", MySqlDbType.VarChar).Value = valor;
command.CommandText = "SELECT `id` FROM @tabela WHERE @[email protected];";
try
{
id = (int)command.ExecuteScalar();
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Number + " : " + ex.Message + command.CommandText);
}
catch (Exception)
{
throw;
}
}
return id;
}
Mais j'obtiens une exception MySqlException à propos de l'erreur de syntaxe. Quand je regarde le message d'exception, il me montre la requête avec le tableau cité! Comment passer la table en paramètre sans guillemets?
Merci beaucoup, les gars. Maintenant, j'ai une meilleure compréhension de SQL paramétré. – programad