当前位置: 首页>>代码示例>>C#>>正文


C# PetaPoco.Database.Dispose方法代码示例

本文整理汇总了C#中PetaPoco.Database.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# PetaPoco.Database.Dispose方法的具体用法?C# PetaPoco.Database.Dispose怎么用?C# PetaPoco.Database.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PetaPoco.Database的用法示例。


在下文中一共展示了PetaPoco.Database.Dispose方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetDatabase

        public static PetaPoco.Database GetDatabase(bool ValidateDatabase = true)
        {
            dbMutex.WaitOne();

            if (!System.IO.File.Exists(DbFile))
                CreateDatabase(DbFile);

            try
            {
                var db = new PetaPoco.Database(@"Data Source={0};Version=3;".FormatStr(DbFile), "System.Data.SQLite");
                if (ValidateDatabase)
                {
                    int version = 0;
                    try
                    {
                        version = db.ExecuteScalar<int>("select databaseversion from [version]");
            #if(DEBUG)
                        if (version != Globals.DB_VERSION)
                        {
                            db.Dispose();
                            // recreate db
                            System.IO.File.Delete(DbFile);
                            CreateDatabase(DbFile);
                            db = new PetaPoco.Database(@"Data Source={0};Version=3;".FormatStr(DbFile), "System.Data.SQLite");
                        }
            #endif
                    }
                    catch (Exception)
                    {
                        throw new SetupException();
                    }
                }

                return db;
            }
            finally
            {
                dbMutex.ReleaseMutex();
            }
        }
开发者ID:revenz,项目名称:NextPvrWebConsole,代码行数:40,代码来源:DbHelper.cs

示例2: SettingsProvider

        public SettingsProvider(string scope)
        {
            _currentSettings = new Hashtable();
            _scopelessSettings = new Hashtable();

            string p = System.IO.Path.Combine(new string[] { System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "GAPP" });
            if (!Directory.Exists(p))
            {
                Directory.CreateDirectory(p);
            }
            string fn = System.IO.Path.Combine(p, "settings.db3");

            //create backup first
            List<string> availableBackups = new List<string>();
            if (File.Exists(fn))
            {
                File.Copy(fn, string.Format("{0}.{1}.bak", fn, DateTime.Now.ToString("yyyyMMddHHmmss")));

                //keep maximum of X backups
                availableBackups = Directory.GetFiles(p, "settings.db3.*.bak").OrderBy(x => x).ToList();
                while (availableBackups.Count > 20)
                {
                    File.Delete(availableBackups[0]);
                    availableBackups.RemoveAt(0);
                }
            }

            bool dbOK;
            bool backupUsed = false;
            do
            {
                try
                {
                    _database = new PetaPoco.Database(string.Format("data source=file:{0}", fn), Community.CsharpSqlite.SQLiteClient.SqliteClientFactory.Instance);
                    dbOK = _database.ExecuteScalar<string>("PRAGMA integrity_check") == "ok";
                    //if (availableBackups.Count > 2) dbOK = false; //test
                }
                catch
                {
                    dbOK = false;
                }
                if (!dbOK)
                {
                    backupUsed = true;
                    try
                    {
                        _database.Dispose();
                    }
                    catch
                    {
                    }
                    _database = null;

                    //delete settings and move to latest
                    File.Delete(fn);
                    if (availableBackups.Count > 0)
                    {
                        File.Move(availableBackups[availableBackups.Count - 1], fn);
                        availableBackups.RemoveAt(availableBackups.Count - 1);
                    }
                }

            } while (!dbOK);

            if (backupUsed)
            {
                System.Windows.Forms.MessageBox.Show("The settings file was corrupt and a backup file is restored.", "Settings");
            }

            if (!TableExists("Settings"))
            {
                _database.Execute("create table 'Settings' (Name text, Value text)");
                _database.Execute("insert into Settings (Name, Value) values ('Scope', 'default')");
            }

            if (!TableExists("SettingsScope"))
            {
                _database.Execute("create table 'SettingsScope' (ID integer primary key autoincrement, Name text)");
            }

            if (string.IsNullOrEmpty(scope))
            {
                scope = _database.ExecuteScalar<string>("select Value from Settings where [email protected]", "Scope");
                if (string.IsNullOrEmpty(scope))
                {
                    scope = "default";
                }
            }

            _scope = _database.FirstOrDefault<SettingsScope>("where [email protected]", scope);
            if (_scope == null)
            {
                _scope = new SettingsScope();
                _scope.Name = scope;
                _database.Save(_scope);
            }

            if (!TableExists(string.Format("Settings_{0}",_scope.ID)))
            {
                _database.Execute(string.Format("create table 'Settings_{0}' (Name text, Value text)", _scope.ID));
//.........这里部分代码省略.........
开发者ID:gahadzikwa,项目名称:GAPP,代码行数:101,代码来源:SettingsProvider.cs


注:本文中的PetaPoco.Database.Dispose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。