//Tests the connection public static void testConnection(Form currentForm) { //Connection is read from a textfile in the app domain directory try { Global.setConnectionString("ConnectionString.dat"); } //If file not found loads a new form where you can choose the database from a folder dialog //Text file is found in the app domain base directory catch (FileNotFoundException ex) { currentForm.Close(); Change_Connection change = new Change_Connection(); change.ShowDialog(); change.Focus(); } using (Global.connection = new SQLiteConnection(Global.connectionString)) { try { Global.connection.Open(); Global.connection.Close(); } //Any other outlying exception will be caught here. //A new form will load and allow the user to choose a new database from a file dialog catch (SQLiteException ex) { MessageBox.Show("Error loading database. Please choose another database\n\n" + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); currentForm.Hide(); Change_Connection cc = new Change_Connection(); cc.ShowDialog(); cc.Focus(); } } }
//********************Non Event Methods******************** //There is a Global.TestConnection but this is used because the user hasn't logged in yet private void testConnection() { //Connection is read from a textfile in the app domain directory try { Global.setConnectionString("ConnectionString.dat"); } //If file not found loads a new form where you can choose the database from a folder dialog //Text file is found in the app domain base directory catch (FileNotFoundException ex) { MessageBox.Show("Error loading database. Please choose another database\n\n" + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory.ToString(); ofd.Filter = "Database File (*.db)|*.db|MDF |*mdf"; ofd.Title = "Choose Database to connect to"; DialogResult result = ofd.ShowDialog(); if (result == DialogResult.OK) { Global.connectionPath = ofd.FileName; Global.connectionString = Global.connection1 + Global.connectionPath + Global.connection2; Global.writeDatafile("ConnectionString.dat", Global.connectionPath); } else if (result == DialogResult.Cancel) { return; } } using (Global.connection = new SQLiteConnection(Global.connectionString)) { try { Global.connection.Open(); Global.connection.Close(); } //Any other outlying exception will be caught here. //A new form will load and allow the user to choose a new database from a file dialog catch (SQLiteException ex) { MessageBox.Show("Error loading database. Please choose another database\n\n" + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory.ToString(); ofd.Filter = "Database File (*.db)|*.db|MDF |*mdf"; ofd.Title = "Choose Database to connect to"; DialogResult result = ofd.ShowDialog(); if (result == DialogResult.OK) { Global.connectionPath = ofd.FileName; Global.connectionString = Global.connection1 + Global.connectionPath + Global.connection2; Global.writeDatafile("ConnectionString.dat", Global.connectionPath); } else if (result == DialogResult.Cancel) { return; } } } }