• SQLite multiple threads


    const int loops = 1000;
    
    public void DatabaseThreadSafetyTest()
    {
        var backgroundThread = new Thread(new System.Threading.ThreadStart(() =>
        {
            for (int i = 1; i <= loops; i++)
            {
                Console.WriteLine("Background thread loop " + i);
                using (var db = new SQLiteConnection(DbPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.SharedCache)) {
                    db.Insert (new MyClass());
                }
            }
        }));
        backgroundThread.Start();
    
        for (int i = 1; i <= loops; i++)
        {
            Console.WriteLine("Main thread loop " + i);
            using (var db = new SQLiteConnection(DbPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.SharedCache)) {
                db.Insert (new MyClass());
            }
        }
    }
    

     

    using System;
    using System.Data.SQLite;
    using System.Threading.Tasks;
    
    namespace SQLiteTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                var tasks = new Task[100];
    
                for (int i = 0; i < 100; i++)
                {
                    tasks[i] = new Task(new Program().WriteToDB);
                    tasks[i].Start();
                }
    
                foreach (var task in tasks)
                    task.Wait();
            }
    
            public void WriteToDB()
            {
                try
                {
                    using (SQLiteConnection myconnection = new SQLiteConnection(@"Data Source=c:123.db"))
                    {
                        myconnection.Open();
                        using (SQLiteTransaction mytransaction = myconnection.BeginTransaction())
                        {
                            using (SQLiteCommand mycommand = new SQLiteCommand(myconnection))
                            {
                                Guid id = Guid.NewGuid();
    
                                mycommand.CommandText = "INSERT INTO Categories(ID, Name) VALUES ('" + id.ToString() + "', '111')";
                                mycommand.ExecuteNonQuery();
    
                                mycommand.CommandText = "UPDATE Categories SET Name='222' WHERE ID='" + id.ToString() + "'";
                                mycommand.ExecuteNonQuery();
    
                                mycommand.CommandText = "DELETE FROM Categories WHERE ID='" + id.ToString() + "'";
                                mycommand.ExecuteNonQuery();
                            }
                            mytransaction.Commit();
                        }
                    }
                }
                catch (SQLiteException ex)
                {
                    if (ex.ReturnCode == SQLiteErrorCode.Busy)
                        Console.WriteLine("Database is locked by another process!");
                }
            }
        }
    }
    

      

  • 相关阅读:
    VC 6.0 编译器的使用帮助
    生成可执行jar文件(转)
    DataGridView填充、更新、删除(多行)Sql Express 2005数据库
    访问域服务器修改密码,报“未知的身份验证机制”的错误搞定!
    尝试用Word2007发布博客
    iBatisNet Tutorial
    Asp.net 技巧集合
    TableAdapter 无法插入或更新Access数据库
    OrcasBeta1 installed 玩一玩Silverlight
    Microsoft .NET Compact Framework 开发常见问题解答从msdn上找到。
  • 原文地址:https://www.cnblogs.com/mschen/p/8268494.html
Copyright © 2020-2023  润新知