gpt4 book ai didi

c# - 未解析特定表名的表名

转载 作者:行者123 更新时间:2023-11-29 16:01:56 25 4
gpt4 key购买 nike

我的任务是,我尝试连接到 mysql 数据库并查找表的记录数。

首先,我连接到数据库并在列表对象中查找表列表。然后遍历表列表并找到每个表的记录数。数据库中有大约80张表,其中有4到5张表名以“blacklist_”开头。

正确返回表的记录计数,但以“blacklist_”开头的表名称除外。出现以下错误。

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

我尝试了各种传递表名的方法..前面加上@, ?等等。我怀疑这是否被识别为关键字/保留字,因为我看到一个“?”这个词两侧的符号,因为我将表名“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_”开头的表。

最佳答案

我知道的不多,但你不能使用像这样的字符串:

String query = "select count(1) from ?";
MySqlCommand DC1cmd = new MySqlCommand(query, DC1con);

然后

DC1cmd.Parameter(1,'@table_name');

或者类似的东西?抱歉,我帮不了什么忙。

关于c# - 未解析特定表名的表名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56112746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com