Created
September 2, 2011 07:35
-
-
Save SuperYeti/1188103 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Drawing; | |
using System.IO; | |
using MonoTouch.Foundation; | |
using MonoTouch.UIKit; | |
using MonoTouch.Dialog.Extensions; | |
using SQLite; | |
namespace BizRX | |
{ | |
public static class DAL | |
{ | |
static YesNoAlertView alert; | |
public static void DeleteDatabase () | |
{ | |
alert = new YesNoAlertView (); | |
alert.Accepted += delegate { | |
DoDeleteDatabase (); | |
}; | |
alert.ShowAlert ("This will delete ALL data from existing database. You CANNOT UNDO this operation. Proceed?"); | |
} | |
static void DoDeleteDatabase () | |
{ | |
try { | |
var documents = Environment.GetFolderPath (Environment.SpecialFolder.Personal); | |
string db = Path.Combine (documents, "BizRX.db3"); | |
if (File.Exists (db)) { | |
if (_Connection != null) { | |
_Connection.Close (); | |
_Connection.Dispose (); | |
_Connection = null; | |
} | |
File.Delete (db); | |
} | |
Util.SuccessfulMessage ("Database Deleted Successfully"); | |
LoadDefaultData (); | |
} catch (Exception) { | |
} | |
} | |
static void LoadDefaultData () | |
{ | |
//TODO: Put any default data loads here. | |
} | |
static void InitializeDatabase (SQLiteConnection db) | |
{ | |
//TODO: Must enter all tables that are used in the database here. I've used a methodology where | |
//i have a base class that stores the object definition. Then i subclass that for the required show | |
//form buttons etc for the UI. This way the form can just call DAL.saveblah and pass itself in. | |
//This seems like the cleanest method, with only minor manual mapping needed here. | |
db.CreateTable<AnswersDAL> (); | |
db.CreateTable<SessionDAL> (); | |
} | |
static SQLiteConnection _Connection; | |
public static SQLiteConnection Connection { | |
get { | |
if (_Connection == null) | |
_Connection = GetConnection (); | |
return _Connection; | |
} | |
} | |
static SQLiteConnection GetConnection () | |
{ | |
SQLiteConnection conn; | |
var documents = Environment.GetFolderPath (Environment.SpecialFolder.Personal); | |
string dbPath = Path.Combine (documents, "BizRX.db3"); | |
conn = new SQLiteConnection (dbPath); | |
if (conn != null) | |
InitializeDatabase (conn); | |
return conn; | |
} | |
/* | |
public static void SetupDB() | |
{ | |
var documents = Environment.GetFolderPath (Environment.SpecialFolder.Personal); | |
string db = Path.Combine (documents, "Orders.db3"); | |
if (!File.Exists (db)) | |
LoadDefaultData(); | |
} | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment