예제 #1
0
 protected System.Data.SQLite.SQLiteDatabase OpenDatabase(string name, string defaultdatabasedirectory, bool inmemory)
 {
     System.Data.SQLite.SQLiteDatabase db = null;
     try
     {
         // check if database path exists --> create if not
         if (!Directory.Exists(defaultdatabasedirectory))
         {
             Directory.CreateDirectory(defaultdatabasedirectory);
         }
         // check if database is already on disk
         DBPath = defaultdatabasedirectory;
         if (!File.Exists(Path.Combine(DBPath, name)))
         {
             // create one on disk
             System.Data.SQLite.SQLiteDatabase dbn = new System.Data.SQLite.SQLiteDatabase(Path.Combine(DBPath, name));
             // open database
             dbn.Open();
             // set user version
             dbn.SetUserVerion(UserVersion);
             // set auto vacuum mode to full
             dbn.SetAutoVacuum(AUTOVACUUMMODE.FULL);
             dbn.Close();
         }
         // check for in-memory database --> open from disk, if not
         if (inmemory)
         {
             db = System.Data.SQLite.SQLiteDatabase.CreateInMemoryDB(Path.Combine(DBPath, name));
         }
         else
         {
             db = new System.Data.SQLite.SQLiteDatabase(Path.Combine(DBPath, name));
             db.Open();
         }
         // get version info
         int v = db.GetUserVersion();
         // do upgrade stuff here
     }
     catch (Exception ex)
     {
         Console.WriteLine("Error initilalizing database: " + ex.Message);
         throw new TypeInitializationException(this.GetType().Name, ex);
     }
     return(db);
 }