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)