public static void DeleteObject(DataObject obj) { if (obj.Id > 0) { SqlCeConnection con = GetNewConnection(); con.Open(); SqlCeCommand command = null; try { if (obj is Account) { Account account = (Account)obj; command = new SqlCeCommand("DELETE FROM accounts WHERE id = @id", con); command.Parameters.AddWithValue("@id", account.Id); command.ExecuteNonQuery(); } else if (obj is ScheduleTask) { ScheduleTask task = (ScheduleTask)obj; command = new SqlCeCommand("DELETE FROM schedules WHERE id = @id", con); command.Parameters.AddWithValue("@id", task.Id); command.ExecuteNonQuery(); } else if (obj is EmulatorInstance) { EmulatorInstance emulator = (EmulatorInstance)obj; command = new SqlCeCommand("DELETE FROM emulators WHERE id = @id", con); command.Parameters.AddWithValue("@id", emulator.Id); command.ExecuteNonQuery(); } } catch (SqlCeException e) { } command.Dispose(); con.Close(); } }
public static List <DataObject> GetObjects <T>() { List <DataObject> list = new List <DataObject>(); SqlCeConnection con = GetNewConnection(); con.Open(); SqlCeCommand command = null; SqlCeDataReader reader = null; try { if (typeof(T) == typeof(ScheduleTask)) { command = new SqlCeCommand("SELECT id, accountId, type, interval, amount, count, x, y, backupX, backupY, lastAction, appId FROM schedules", con); reader = command.ExecuteReader(); while (reader.Read()) { ScheduleTask task = new ScheduleTask(reader.GetInt32(0), new Account(reader.GetInt32(1)), (ScheduleType)reader.GetInt32(2), reader.GetInt32(3), reader.GetInt32(4), reader.GetInt32(5), reader.GetInt32(6), reader.GetInt32(7), reader.GetInt32(8), reader.GetInt32(9), reader.GetDateTime(10), new App(reader.GetInt32(11))); list.Add(task); } } else if (typeof(T) == typeof(Account)) { command = new SqlCeCommand("SELECT id, name, username, email, password, pinCode, priority, foodNegativeAmt, lastLogin, lastLogout, appId FROM accounts", con); reader = command.ExecuteReader(); while (reader.Read()) { Account account = new Account(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetString(4), reader.GetInt32(5), (AccountPriority)reader.GetInt32(6), reader.GetInt32(7), reader.GetDateTime(8), reader.GetDateTime(9), new App(reader.GetInt32(10))); list.Add(account); } } else if (typeof(T) == typeof(EmulatorInstance)) { command = new SqlCeCommand("SELECT id, type, windowName, command, lastKnownAccountId, appId FROM emulators", con); reader = command.ExecuteReader(); while (reader.Read()) { EmulatorInstance emulator = new EmulatorInstance(reader.GetInt32(0), (EmulatorType)reader.GetInt32(1), reader.GetString(2), reader.GetString(3), new Account(reader.GetInt32(4)), new App(reader.GetInt32(5))); list.Add(emulator); } } else if (typeof(T) == typeof(App)) { command = new SqlCeCommand("SELECT id, name, shortName FROM apps", con); reader = command.ExecuteReader(); while (reader.Read()) { App app = new App(reader.GetInt32(0), reader.GetString(1), reader.GetString(2)); list.Add(app); } } else if (typeof(T) == typeof(Settings)) { command = new SqlCeCommand("SELECT version, emulatorId1, emulatorId2, emulatorId3, emulatorId4, mapDir, screenshotDir, slackURL, pushoverAPIKey, pushoverUserKey FROM settings", con); reader = command.ExecuteReader(); reader.Read(); Settings settings = new Settings(reader.GetInt32(0), reader.GetInt32(1), reader.GetInt32(2), reader.GetInt32(3), reader.GetInt32(4), reader.GetString(5), reader.GetString(6), reader.GetString(7), reader.GetString(8), reader.GetString(9)); list.Add(settings); } } catch (SqlCeException e) { /*if (e.ErrorCode == Constraint_Check) * { * //name already in use * }*/ } catch (Exception e) { e = e; } finally { command.Dispose(); if (reader != null) { reader.Dispose(); } } con.Close(); return(list); }
public static DataObject SaveObject(DataObject obj) { SqlCeConnection con = GetNewConnection(); con.Open(); SqlCeCommand command = null; try { if (obj is Account) { Account account = (Account)obj; if (account.Id == 0) { command = new SqlCeCommand("INSERT INTO accounts (name, username, email, password, pinCode, priority, foodNegativeAmt, lastLogin, lastLogout, appId) VALUES (@name, @username, @email, @password, @pinCode, @priority, @foodNegAmt, @lastLogin, @lastLogout, @appId)", con); command.Parameters.AddWithValue("@lastLogin", new DateTime().AddYears(1972)); command.Parameters.AddWithValue("@lastLogout", new DateTime().AddYears(1972)); command.Parameters.AddWithValue("@appId", account.App.Id); } else { command = new SqlCeCommand("UPDATE accounts SET name = @name, username = @username, email = @email, password = @password, pinCode = @pinCode, priority = @priority, foodNegativeAmt = @foodNegAmt, lastLogin = @lastLogin, lastLogout = @lastLogout WHERE id = @id", con); command.Parameters.AddWithValue("@lastLogin", account.LastLogin); command.Parameters.AddWithValue("@lastLogout", account.LastLogout); command.Parameters.AddWithValue("@id", account.Id); } command.Parameters.AddWithValue("@name", account.Name); command.Parameters.AddWithValue("@username", account.UserName); command.Parameters.AddWithValue("@email", account.Email); command.Parameters.AddWithValue("@password", account.Password); command.Parameters.AddWithValue("@pinCode", account.PinCode); command.Parameters.AddWithValue("@priority", (int)account.Priority); command.Parameters.AddWithValue("@foodNegAmt", account.FoodNegativeAmount); command.ExecuteNonQuery(); if (account.Id == 0) { command = new SqlCeCommand("SELECT last_insert_rowid()", con); account.Id = Convert.ToInt32(command.ExecuteScalar()); } obj = account; } else if (obj is ScheduleTask) { ScheduleTask task = (ScheduleTask)obj; if (task.Id == 0) { command = new SqlCeCommand("INSERT INTO schedules (accountId, type, interval, amount, count, x, y, backupX, backupY, lastAction, appId) VALUES (@accountId, @type, @interval, @amt, @count, @x, @y, @altX, @altY, @lastAction, @appId)", con); command.Parameters.AddWithValue("@lastAction", DateTime.Now); command.Parameters.AddWithValue("@appId", task.App.Id); } else { command = new SqlCeCommand("UPDATE schedules SET accountId = @accountId, type = @type, interval = @interval, amount = @amt, count = @count, x = @x, y = @y, backupX = @altX, backupY = @altY, lastAction = @lastAction WHERE id = @id", con); command.Parameters.AddWithValue("@lastAction", task.LastAction); command.Parameters.AddWithValue("@id", task.Id); } command.Parameters.AddWithValue("@accountId", task.Account.Id); command.Parameters.AddWithValue("@type", (int)task.Type); command.Parameters.AddWithValue("@interval", task.Interval); command.Parameters.AddWithValue("@amt", task.Amount); command.Parameters.AddWithValue("@count", task.Count); command.Parameters.AddWithValue("@x", task.X); command.Parameters.AddWithValue("@y", task.Y); command.Parameters.AddWithValue("@altX", task.BackupX); command.Parameters.AddWithValue("@altY", task.BackupY); command.ExecuteNonQuery(); if (task.Id == 0) { command = new SqlCeCommand("SELECT last_insert_rowid()", con); task.Id = Convert.ToInt32(command.ExecuteScalar()); } obj = task; } else if (obj is EmulatorInstance) { EmulatorInstance emulator = (EmulatorInstance)obj; if (emulator == null) { emulator = emulator; } if (emulator.Id == 0) { command = new SqlCeCommand("INSERT INTO emulators (type, windowName, command, lastKnownAccountId, appId) VALUES (@type, @windowName, @command, @accountId, @appId)", con); } else { command = new SqlCeCommand("UPDATE emulators SET type = @type, windowName = @windowName, command = @command, lastKnownAccountId = @accountId, appId = @appId WHERE id = @id", con); command.Parameters.AddWithValue("@id", emulator.Id); } command.Parameters.AddWithValue("@type", (int)emulator.Type); command.Parameters.AddWithValue("@windowName", emulator.WindowName); command.Parameters.AddWithValue("@command", emulator.Command); command.Parameters.AddWithValue("@accountId", emulator.LastKnownAccount == null ? 0 : emulator.LastKnownAccount.Id); command.Parameters.AddWithValue("@appId", emulator.App.Id); command.ExecuteNonQuery(); if (emulator.Id == 0) { command = new SqlCeCommand("SELECT last_insert_rowid()", con); emulator.Id = Convert.ToInt32(command.ExecuteScalar()); } obj = emulator; } else if (obj is Settings) { Settings settings = (Settings)obj; command = new SqlCeCommand("UPDATE settings SET version = @version, emulatorId1 = @emulator1, emulatorId2 = @emulator2, emulatorId3 = @emulator3, emulatorId4 = @emulator4, mapDir = @mapDir, screenshotDir = @ssDir, slackURL = @slackUrl, pushoverAPIKey = @pushoverAPI, pushoverUserKey = @pushoverUser", con); command.Parameters.AddWithValue("@version", settings.Version); command.Parameters.AddWithValue("@emulator1", settings.Emulator1); command.Parameters.AddWithValue("@emulator2", settings.Emulator2); command.Parameters.AddWithValue("@emulator3", settings.Emulator3); command.Parameters.AddWithValue("@emulator4", settings.Emulator4); command.Parameters.AddWithValue("@mapDir", settings.MapDir); command.Parameters.AddWithValue("@ssDir", settings.ScreenshotDir); command.Parameters.AddWithValue("@slackUrl", settings.SlackURL); command.Parameters.AddWithValue("@pushoverAPI", settings.PushoverAPIKey); command.Parameters.AddWithValue("@pushoverUser", settings.PushoverUserKey); command.ExecuteNonQuery(); obj = settings; } } catch (SqlCeException e) { /*if (e.ErrorCode == SQLiteErrorCode.Constraint_Check) * { * //name already in use * }*/ } finally { command.Dispose(); } con.Close(); return(obj); }