I am using an Access database. Since my database doesn’t sometimes close I can’t backup my database. How can I force the database to close? My codes are below.
private void Button2_Click(object sender, EventArgs e) { try { OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source = fatih4141.accdb; Jet OLEDB:Database Password = 123456; Mode = ReadWrite"); conn.Close(); conn.Dispose(); GC.Collect(); GC.WaitForPendingFinalizers(); OpenFileDialog dlg = new OpenFileDialog { Filter = "Microsoft Access Database(2002-2003) (*.mdb)|*.mdb|Microsoft Access Database(2007-2009) (*.accdb)|*.accdb", // If you want to keep default extensiion *.mdb then FilterIndex = 1 or if you want to keep default extension *.accdb then FilterIndex=2 FilterIndex = 2, InitialDirectory = Environment.CurrentDirectory + @"\Backup" }; if (dlg.ShowDialog() == DialogResult.OK) { MessageBox.Show("Database to be backup " + dlg.FileName); } string PathToRestoreDB = Environment.CurrentDirectory + @"\fatih4141.accdb"; string Filetorestore = dlg.FileName; // Rename Current Database to .Bak File.Move(PathToRestoreDB, PathToRestoreDB); //Restore the Database From Backup Folder File.Copy(Filetorestore, PathToRestoreDB, true); MessageBox.Show("The database has been changed.", "The new information has been transfered.", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("The process has been cancelled " + ex.Message); } }