如何将sqlrebuilt索引转换为c#
我正在尝试使用数据库查询中的C#运行重建索引,但是我遇到了这个问题吗?有什么想法吗?
这是我在C#中的重建索引查询:
public static string rebuildIndex = "ALTER INDEX ALL @tablename REBUILD PARTITION = @partition_number WITH (ONLINE = ON)";
这是来自相应函数的代码:
static void rebuildIndex(string query,string tablename, int parNumber)
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@tablename", tablename);
cmd.Parameters.AddWithValue("@partition_number", parNumber);
cmd.ExecuteNonQuery();
}
}
这是错误msg。
*ERROR yourmessage Incorrect syntax near '@tablename'. [HouseKeep_VC.Program] [1]
System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near '@tablename'.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
I am trying to run rebuild index using C# from my database query, but I've encountered this problem? Any ideas?
Here is my rebuild index query in C#:
public static string rebuildIndex = "ALTER INDEX ALL @tablename REBUILD PARTITION = @partition_number WITH (ONLINE = ON)";
Here is the code from the respective function:
static void rebuildIndex(string query,string tablename, int parNumber)
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@tablename", tablename);
cmd.Parameters.AddWithValue("@partition_number", parNumber);
cmd.ExecuteNonQuery();
}
}
And here is the error msg.
*ERROR yourmessage Incorrect syntax near '@tablename'. [HouseKeep_VC.Program] [1]
System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near '@tablename'.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将动态SQL用于表名称,它不能被参数化。确保使用
定制
正确报价名称,You need to use dynamic SQL for the table name, it cannot be parameterized. Make sure to use
QUOTENAME
to quote the name correctly