private void ImportFromBakup(string[] BakFiles, StatsDatabase Database)
{
// Clear old database records
TaskForm.Progress.Report(new TaskProgressUpdate("Removing old stats data"));
Database.Truncate();
// Let the database update itself
Thread.Sleep(500);
// Begin transaction
using (DbTransaction Transaction = Database.BeginTransaction())
{
// import each table
foreach (string file in BakFiles)
{
// Get table name
string table = Path.GetFileNameWithoutExtension(file);
TaskForm.Progress.Report(new TaskProgressUpdate("Processing stats table: " + table));
// Import table data
try
{
// Sqlite kinda sucks... no import methods
if (Database.DatabaseEngine == DatabaseEngine.Sqlite)
{
string[] Lines = File.ReadAllLines(file);
foreach (string line in Lines)
{
string[] Values = line.Split('\t');
Database.Execute(
String.Format("INSERT INTO {0} VALUES({1})", table, "\"" + String.Join("\", \"", Values) + "\"")
);
}
}
else
Database.Execute(String.Format("LOAD DATA LOCAL INFILE '{0}' INTO TABLE {1};", file.Replace('\\', '/'), table));
}
catch (Exception Ex)
{
// Show exception error
using (ExceptionForm Form = new ExceptionForm(Ex, false))
{
Form.Message = String.Format("Failed to import data into table {0}!{2}{2}Error: {1}", table, Ex.Message, Environment.NewLine);
DialogResult Result = Form.ShowDialog();
// Rollback!
TaskForm.Progress.Report(new TaskProgressUpdate("Rolling back stats data"));
Transaction.Rollback();
return;
}
}
}
// Commit the transaction
Transaction.Commit();
}
}