当我尝试在WinRT的SQLite上的同一事务中的同一表上进行多个更新时,出现“无法打开”异常。

我为此用例创建了一个示例应用程序。下面的代码在单击第一个按钮时,我正在事务中创建一个表,在单击另一个按钮时,我试图第二次更新同一记录。在那里,它引发“无法打开”异常。

app code

private SQLiteConnection getConnection()
{
    var connection = SQLiteConnectionPool.Shared.GetConnection(
                           new SQLiteConnectionString("sample.db", false));
    return connection;
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    SQLiteConnection con = getConnection();
    con.BeginTransaction();
    {
        try
        {
            var command = new SQLiteCommand(con) {
                CommandText =
                    @"create table konysyncMETAINFO (
                        id bigint not null,
                        versionnumber int,
                        lastserversynccontext nvarchar(1000),
                        filtervalue nvarchar(1000),
                        replaysequencenumber integer,
                        lastgeneratedid integer,
                        scopename nvarchar(100),
                        primary key (id))"
            };
            var xyz = (double)command.ExecuteNonQuery();

            var command2 = new SQLiteCommand(con) {
                CommandText =
                    @"insert into konysyncMETAINFO
                        (id,scopename,versionnumber,lastserversynccontext,
                         replaysequencenumber,lastgeneratedid)
                      values ('1','testscope','0','','0','-1')"
            };
            var xyz2 = (double)command2.ExecuteNonQuery();

            var command3 = new SQLiteCommand(con) {
                CommandText =
                    @"insert into konysyncMETAINFO
                        (id,scopename,versionnumber,lastserversynccontext,
                         replaysequencenumber,lastgeneratedid)
                      values ('2','testscope2','0','','0','-1')"
            };
            var xyz3 = (double)command3.ExecuteNonQuery();
            con.Commit();
        }
        catch (Exception ex)
        {
            con.Rollback();
        }
    }
}

private void Button_Click_2(object sender, RoutedEventArgs e)
{
    SQLiteConnection con = getConnection();
    con.BeginTransaction();
    {
        try
        {
            var command = new SQLiteCommand(con) {
                CommandText =
                    @"Update konysyncMETAINFO
                      set lastgeneratedid='4'
                      WHERE scopename = 'testscope'"
            };
            var xyz = (double)command.ExecuteNonQuery();

            //var command2 = new SQLiteCommand(con) { CommandText = "insert into konysyncMETAINFO (id,scopename,versionnumber,lastserversynccontext,replaysequencenumber,lastgeneratedid) values ('3','testscope3','0','','0','-1')" };
            //var xyz2 = (double)command2.ExecuteNonQuery();

            var command3 = new SQLiteCommand(con) {
                CommandText =
                    @"Update konysyncMETAINFO
                      set lastgeneratedid='3'
                      WHERE scopename = 'testscope'"
            };
            var xyz3 = (double)command3.ExecuteNonQuery();
            con.Commit();
        }
        catch (Exception ex)
        {
            con.Rollback();
        }
    }
}

最佳答案

您可能偶然发现了我拥有的sqlite-net中的the same bug。我已经创建了a fix,它已经被拉回到主分支中,但是从那以后就没有新的release on NuGet了。您可以download the latest sources directly并检查它是否可以解决您的问题。

关于c# - 多个更新语句抛出“无法打开”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13705341/

10-10 07:21