1

I'm learning C#. The code as I show it works, but I want to pass the name of the table "city" as a parameter (@paramTable for example). None of the code I've found on the internet worked. Any suggestion is welcome, thanks!

     public DataTable LoadGrid()
     {
        DataTable DTabla = new DataTable();

        string str = "SELECT * FROM  city WHERE ID < 10;";
        MySqlCommand comando = new MySqlCommand(str, conexion);


        if (this.AbrirConexion())
        {

            try
            {

                MySqlDataAdapter adapter = new MySqlDataAdapter(comando);

                adapter.Fill(DTabla);

            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message);
            }

            this.CerrarConexion();
        }

        return DTabla;
    }
Gutblender
  • 1,340
  • 1
  • 12
  • 25
niutudis
  • 35
  • 1
  • 7
  • How and where would the variable come from? – FRANCISCO J. BLANCO Jul 23 '19 at 22:31
  • 3
    @ChetanRanpariya SqlParameters can't be used for the table name. – Jonathon Chase Jul 23 '19 at 22:47
  • 1
    That's right I was little too quick to mark it as duplicate. Parameter can not be used for table name. I think in that case OP needs to use string concatenation. – Chetan Jul 23 '19 at 23:00
  • 1
    As others have already pointed out, you cannot parameterize table names, or other identifiers; this is because doing this kind of thing in the first place is generally unnecessary outside of improperly designed databases, and generalized database management applications (like MySQL Workbench). – Uueerdo Jul 23 '19 at 23:17

0 Answers0