// Загрузить сценарии из файла на диске static public void LoadListFromText(ScenarioList list) { if (File.Exists(CurrentFileName)) { list.Load(CurrentFileName); } }
static public void LoadList(ScenarioList list) { list.list.Clear(); if (!File.Exists(CurrentFileName)) { Scenario s = new Scenario(); s.Title = "Демо 1"; s.Zip = true; s.Destination = @"C:\Users\Юлия\Desktop\Backup"; s.Source.Add(@"C:\Users\Юлия\Documents\Python\program1"); s.Source.Add(@"C:\Users\Юлия\Pictures\Wallpapers"); list.Add(s); s = new Scenario(); s.Title = "Демо 2"; s.scenarioType = ScenarioType.зеркальный; s.Destination = @"H:\Backup"; s.Source.Add(@"D:\Films"); list.Add(s); s = new Scenario(); s.Title = "Демо 3"; s.scenarioType = ScenarioType.полный; s.Destination = @"\\User2\Backup"; s.Source.Add(@"\\User2\Documents\"); list.Add(s); } else { list.Load(CurrentFileName); } }
static public void SaveList(ScenarioList list, bool AsText) { if (AsText) { SaveListToText(list); } else { SaveListToSQLite(list); } }
static public void LoadList(ScenarioList list, bool AsText) { list.list.Clear(); if (AsText) { LoadListFromText(list); } else { LoadListFromSQLite(list); } }
// Занрузить сценарии из SQLite static public void LoadListFromSQLite(ScenarioList list) { list.list.Clear(); // Прочитать таблицу m_dbConn = new SQLiteConnection("Data Source = " + CurrentFileName + "; Version = 3;"); m_dbConn.Open(); m_sqlCmd = new SQLiteCommand(m_dbConn); m_sqlCmd.Connection = m_dbConn; string sqlQuery = "SELECT Title FROM Scenario"; SQLiteDataAdapter adapter = new SQLiteDataAdapter(sqlQuery, m_dbConn); DataTable dTable = new DataTable(); // Получить данные adapter.Fill(dTable); string[] temp = new string[dTable.Rows.Count]; for (int i = 0; i < dTable.Rows.Count; i++) { temp[i] = dTable.Rows[i].ItemArray[0].ToString(); } list.Load(ref temp); }
// Сохранить сценарии в SQLite static public void SaveListToSQLite(ScenarioList list) { string[] temp = new string[0]; list.Save(ref temp); // Если база не существует - создать базу if (!File.Exists(CurrentFileName)) { SQLiteConnection.CreateFile(CurrentFileName); SQLiteConnection.Shutdown(true, true); // Пересоздать таблицу m_dbConn = new SQLiteConnection("Data Source = " + CurrentFileName + "; Version = 3;"); m_dbConn.Open(); m_sqlCmd = new SQLiteCommand(m_dbConn); m_sqlCmd.Connection = m_dbConn; //m_sqlCmd.CommandText = "CREATE TABLE IF NOT EXISTS Scenario (id INTEGER PRIMARY KEY AUTOINCREMENT, Title TEXT)"; m_sqlCmd.CommandText = "CREATE TABLE IF NOT EXISTS Scenario (id INTEGER PRIMARY KEY AUTOINCREMENT, Title TEXT," + "ScenarioName TEXT," + "ScenarioType TEXT," + "ScenarioPack TEXT," + "ScenarioDestination TEXT" + ")"; m_sqlCmd.ExecuteNonQuery(); m_dbConn.Close(); } m_dbConn = new SQLiteConnection("Data Source = " + CurrentFileName + "; Version = 3;"); m_dbConn.Open(); // Очистить таблицу m_sqlCmd = new SQLiteCommand(m_dbConn); m_sqlCmd.Connection = m_dbConn; m_sqlCmd.CommandText = "DELETE FROM Scenario"; m_sqlCmd.ExecuteNonQuery(); // Сохранить все temp int index = 0; foreach (string s in temp) { //m_sqlCmd.CommandText = "INSERT INTO Scenario (Title) VALUES ('" + s + "')"; m_sqlCmd.CommandText = "INSERT INTO Scenario " + "(" + "Title," + "ScenarioName," + "ScenarioType," + "ScenarioPack," + "ScenarioDestination" + ") values (" + "'" + s + "'," + "'" + list.list[index].Title + "'," + "'" + list.list[index].scenarioType.ToString() + "'," + "'" + list.list[index].Zip.ToString() + "'," + "'" + list.list[index].Destination + "'" + ")"; m_sqlCmd.ExecuteNonQuery(); } m_dbConn.Close(); }
// Сохранить сценарии в файл на диске static public void SaveListToText(ScenarioList list) { list.Save(CurrentFileName); }