我的任务是,我正在尝试连接到 mysql 数据库并查找表的记录数。
首先,我正在建立与数据库的连接并在列表对象中查找表列表。然后遍历表列表并找到每个表的记录数。数据库中大约有80个表,其中4到5个表的名称以“blacklist_”开头。
除了以“blacklist_”开头的表名外,表的记录计数已正确返回。得到以下错误。
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“?blacklist?_card”附近使用正确的语法
我尝试了各种传递表名的方法。在前面加上@,?等等。我怀疑这是否被识别为关键字/保留字,因为我看到了“?” 这个词两侧的符号,当我将表名“blacklist_card”作为一个整体传递时。
它是否可能与某些 REGEX 模式匹配。? 因为我看到了?黑名单这个词两边的符号,这不是我通过的东西。
当我在 VS 2017 中调试时,表名正确显示在表列表中。问题仅在我使用此名称查询表时发生。
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();
}
}
期望是必须返回所有表记录计数。但是那些名称以“blacklist_”开头的表会附加错误。