Click here to Skip to main content
15,748,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using an Access database. Since my database doesn’t sometimes close I can’t restore my database. How can I force the database to close? My codes are below.

private void Button2_Click(object sender, EventArgs e)
                foreach (System.Diagnostics.Process process in System.Diagnostics.Process.GetProcessesByName("MSACCESS"))
                    if (process.MainWindowTitle.Contains("fatih2541.accdb"))
                    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 restore " + dlg.FileName);
                    string PathToRestoreDB = Environment.CurrentDirectory + @"\fatih2541.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("It was replaced with the database backup.", "The new information has been transfered.", MessageBoxButtons.OK, MessageBoxIcon.Information); 
            catch (Exception ex)
                MessageBox.Show("The process was canceled " + ex.Message);


What I have tried:

I can't kill the process. How can we do?
Dave Kreskowiak 26-Apr-23 9:01am    
Chances are good that it has nothing to do with killing the Access process. If YOUR OWN CODE has a connection to the database that as not properly Disposed, that will hold the database file open, preventing it from being replaced.
Gerry Schmitz 26-Apr-23 10:47am    
You need a protocol.
mtoha 27-Apr-23 5:15am    
Gerry giving correct direction. you should close the access connection after finish querying

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900