我试图在使用SQLite.NET和VS2008创建的简单数据库中为表重新索引。我需要在每个DELETE命令之后重新索引表,这是我编写的代码片段(不起作用):

SQLiteCommand currentCommand;
String tempString = "REINDEX tf_questions";
//String tempString = "REINDEX [main].tf_questions";
//String tempString = "REINDEX main.tf_questions";

currentCommand = new SQLiteCommand(myConnection);
currentCommand.CommandText = tempString;
currentCommand.ExecuteNonQuery()


在我的程序中运行时,代码不会产生任何错误,但是也不会为“ tf_questions”表重新编制索引。在上面的示例中,您还将看到我尝试过的其他查询字符串也无法正常工作。

请帮忙,

谢谢

最佳答案

我想出了解决此问题的方法。考虑以下代码:

SQLiteCommand currentCommand;
String tempString;
String currentTableName = "tf_questions";
DataTable currentDataTable;
SQLiteDataAdapter myDataAdapter;

currentDataTable = new DataTable();
myDataAdapter = new SQLiteDataAdapter("SELECT * FROM " + currentTableName, myConnection);
myDataAdapter.Fill(currentDataTable);


//"tf_questions" is the name of the table
//"question_id" is the name of the primary key column in "tf_questions" (set to auto inc.)
//"myConnection" is and already open SQLiteConnection pointing to the db file

for (int i = currentDataTable.Rows.Count-1; i >=0 ; i--)
{
    currentCommand = new SQLiteCommand(myConnection);
    tempString = "UPDATE "+ currentTableName +"\nSET question_id=\'"+(i+1)+"\'\nWHERE (question_id=\'" +
        currentDataTable.Rows[i][currentDataTable.Columns.IndexOf("question_id")]+"\')";
    currentCommand.CommandText = tempString;

    if( currentCommand.ExecuteNonQuery() < 1 )
    {
        throw new Exception("There was an error executing the REINDEX protion of the code...");
    }
}


该代码有效,尽管我希望使用内置的SQL REINDEX命令。

关于c# - C#/。NET SQLite — REINDEX不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2424165/

10-09 00:58