C#SQLite删除不起作用

Mar*_*o M 0 c# sqlite

我有这个辅助功能:

public bool Delete(String tableName, String where)
    {
        Boolean returnCode = true;
        try
        {
            this.ExecuteNonQuery(String.Format("delete from {0} where {1};", tableName, where));                
        }
        catch (Exception fail)
        {
            MessageBox.Show(fail.Message);
            returnCode = false;
        }
        return returnCode;
    }
Run Code Online (Sandbox Code Playgroud)

TableName包含"[MyTable]",其中包含"[MyTable ID] ='4ffbd580-b17d-4731-b162-ede8d698e026'",它是表示行ID的唯一guid.

该函数返回true,就像它成功一样,并且没有异常,但是行不会从DB中删除,那是什么?

这是ExecuteNonQuery函数

 public int ExecuteNonQuery(string sql)
    {
        SQLiteConnection cnn = new SQLiteConnection(dbConnection);
        cnn.Open();
        SQLiteCommand mycommand = new SQLiteCommand(cnn);
        mycommand.CommandText = sql;
        int rowsUpdated = mycommand.ExecuteNonQuery();
        cnn.Close();
        return rowsUpdated;
    }
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 6

首先,你不应该像这样嵌入SQL.您应该使用参数化 SQL,以避免SQL注入攻击.

接下来,你应该看一下返回值ExecuteNonQuery.假设它遵循正常模式ExecuteNonQuery,它应该返回受影响的行数.我怀疑它返回0,这意味着没有任何东西符合你的where条款.

(我也会停止捕捉Exception- 可能会停止捕捉任何东西,但至少要抓住一些特定的东西,如果你必须的话.我也会摆脱局部变量,当你知道你想要归来的时候直接回来...... )