My task is, I am trying to connect to a mysql DB and find the record count of the table.
First, I am making a connection to the DB and finding the list of tables in to a list object. Then iterating through the table list and finding the record count of each table. There are about 80 tables in the database among which 4 to 5 table's name starts with "blacklist_".
The record count is correctly getting returned for the tables except for the table name starting with "blacklist_". Getting the below error.
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 '?blacklist?_card' at line 1
I tried various ways of passing the table names.. Prepending with @, ? etc. I suspect if this is getting recognized as a keyword / reserved word because I see a '?' symbol on both side of this word where as I am passing the table name "blacklist_card" as a whole.
Is it possible that it is matching some REGEX pattern. ? Because I see ? symbol on either side of the word blacklist which is not something that I passed.
The table name appears proper in the list of table when I debug in VS 2017. The problem happens only when I query the table with this name.
foreach (string table in tableList)
{
using (DC1con)
{
if (DC1con.State != ConnectionState.Open)
{
DC1con.Open();
}
// MySqlCommand DC1cmd = new MySqlCommand("select count(1) from `" + table + "`;", DC1con);
MySqlCommand DC1cmd = new MySqlCommand("select count(1) from '@table_name'", DC1con);
DC1cmd.Prepare();
DC1cmd.Parameters.AddWithValue("@table_name", table);
DC1cmd.Parameters["@table_name"].Value = table;
DC1cmd.CommandTimeout = 20000;
MessageBox.Show(DC1cmd.CommandText.ToString());
int DC1Count = Convert.ToInt32(DC1cmd.ExecuteScalar());
DC1con.Close();
DC1con.Dispose();
}
}
The expectation is all the table record count must be returned. But getting attached error those tables where the name starts with "blacklist_".