c# - 使用 SQLite 和 C# 收到 "database is locked"错误

标签 c# sqlite

这是我正在使用的代码:

    public bool save(string name, string type, string city, string org)
    {
        BLL.maxkey maxkey = new BLL.maxkey();
        int maxid = maxkey.getMaxKey(1);

        {
            SQLiteConnection sqConnection = new SQLiteConnection(@"Data Source= C:\Users\BigDaddyDuergar\Documents\Visual Studio 2012\Projects\MET Character Manager\MET Character Manager\METCMDB");
            sqConnection.Open();

            SQLiteCommand cmd = new SQLiteCommand();
            cmd.Connection = sqConnection;
            SQLiteTransaction trans;

            trans = sqConnection.BeginTransaction();
            cmd.Transaction = trans;
            int rows = 0;

            try
            {
                cmd.CommandText = "insert into game values(@gameid, @gamename, @gametype, @gamecity, @gameorg)";
                cmd.Parameters.AddWithValue("@gameid", maxid);
                cmd.Parameters.AddWithValue("@gamename", name);
                cmd.Parameters.AddWithValue("@gametype", type);
                cmd.Parameters.AddWithValue("@gamecity", city);
                cmd.Parameters.AddWithValue("@gameorg", org);

                cmd.ExecuteNonQuery();
                trans.Commit();
            }
            catch (Exception ex)
            {
                trans.Rollback();
                error error = new error(ex);
            }
            finally
            {
                sqConnection.Close();
            }

            if (rows == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


    }

还有:

    public void updateMaxKey(int tablenum)
    {
        //using (var conn = new SQLiteConnection(@"Data Source= C:\Users\BigDaddyDuergar\Documents\Visual Studio 2012\Projects\MET Character Manager\MET Character Manager\METCMDB"))
        //using (var cmd = conn.CreateCommand())
        {
            SQLiteConnection sqConnection = new SQLiteConnection(@"Data Source= C:\Users\BigDaddyDuergar\Documents\Visual Studio 2012\Projects\MET Character Manager\MET Character Manager\METCMDB");
            sqConnection.Open();

            SQLiteCommand cmd = new SQLiteCommand();
            cmd.Connection = sqConnection;
            SQLiteTransaction trans;

            trans = sqConnection.BeginTransaction();
            cmd.Transaction = trans;

            try
            {

                cmd.CommandText = "update maxnumkey set maxnum = (maxnum + 1) where tableid = @tableid";
                cmd.Parameters.AddWithValue("@tableid", tablenum);
                cmd.ExecuteNonQuery();
                trans.Commit();
            }
            catch (Exception ex)
            {
                trans.Rollback();
                error error = new error(ex);
            }
            finally
            {
                sqConnection.Close();
            }
        }
    }

    public int returnMaxKey(int tablenum)
    {
        int maxrows = 0;

        {
            SQLiteConnection sqConnection = new SQLiteConnection(@"Data Source= C:\Users\BigDaddyDuergar\Documents\Visual Studio 2012\Projects\MET Character Manager\MET Character Manager\METCMDB");
            sqConnection.Open();

            SQLiteCommand cmd = new SQLiteCommand();
            cmd.Connection = sqConnection;
            SQLiteTransaction trans;

            trans = sqConnection.BeginTransaction();
            cmd.Transaction = trans;


            try
            {
                cmd.CommandText = "select maxnum from maxnumkey where tableid = @tableid";
                cmd.Parameters.AddWithValue("@tableid", tablenum);

                SQLiteDataReader sqReader = cmd.ExecuteReader();
                sqReader.Read();
                maxrows = sqReader.GetInt32(0);
                trans.Commit();


            }
            catch (Exception ex)
            {
                trans.Rollback();
                error error = new error(ex);
            }
            finally
            {
                sqConnection.Close();
            }

            if (maxrows > 0)
            {
                return maxrows;
            }
            else
            {
                return -1;
            }
        }


    }

我可以运行 updatemaxkey 函数,然后运行 ​​returnmaxkey 函数,没有错误。我在数据库中验证更新是否已正确处理,并且返回的信息是否匹配。我尝试进入 SAVE 函数,并在 trans.Commit() 上收到“数据库已锁定”错误。

我似乎无法在任何地方找到任何详细说明这一点并为如何缓解此问题提供任何指导的内容。

如果您需要任何进一步的说明,请询问。我大约 12 小时前刚刚开始使用 sqlite,所以我希望它是一些简单的东西,我只是忽略了。

谢谢!

最佳答案

基本的.NET:

  • 丢弃一次性元素。命令、连接都是这样。

  • 使用“using”语句是最简单的。杀死一半的代码。

  • 这对于交易来说绝对至关重要。

我只能打赌,SqlConncetion - 由于违反了基本的 .NET 原则,要求您配置实现 IDisposable 的对象 - 不会释放文件共享,以防您重新打开它 - 这会阻塞数据库(这可以不能很好地处理多个并行访问)。

没有什么真正具体的 SqlCe - 您的 C# 代码违反了核心 .NET 原则。

关于c# - 使用 SQLite 和 C# 收到 "database is locked"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22384460/

相关文章:

c# - 可空变量类型 - .value 成员

C# 控制台获取 Windows 10 强调色

sql - 对于单个应用程序来说拥有多个 SQL 数据库是一个坏主意吗?

ios - SQLite 不支持嵌套查询

java - 我应该使用什么分辨率的图像?

c# - 如何在 C# 中使用 Quickbase API 上传文件?

c# - 处理样本 c#

c# - NuGet 包版本与引用版本不匹配

Android从设备sqlite到服务器的数据同步

sqlite - 你如何创建像图表/流程图这样的 sqlite 帮助?