Click here to Skip to main content
15,887,411 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.

C#
private void Button2_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (System.Diagnostics.Process process in System.Diagnostics.Process.GetProcessesByName("MSACCESS"))
                {
                    if (process.MainWindowTitle.Contains("fatih2541.accdb"))
                    {
                        process.Kill();
                    }
                }
                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 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?
Posted
Comments
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.
[no name] 26-Apr-23 10:47am    
You need a protocol.

https://learn.microsoft.com/en-us/answers/questions/893364/how-can-i-check-if-access-db-is-open-then-activate
mtoha 27-Apr-23 5:15am    
Gerry giving correct direction. you should close the access connection after finish querying

1 solution

It depends on method of opening a database...

I guess you're using DAO:
Workspace.OpenDatabase method (DAO) | Microsoft Learn[^] or
DBEngine.OpenDatabase method (DAO) | Microsoft Learn[^]

If i'm right, you have to close this database by using one of the following methods:
DoCmd.CloseDatabase method (Access) | Microsoft Learn[^] or
Application.CloseCurrentDatabase method (Access) | Microsoft Learn[^]
Then, you have to close MS Access application.

I'd suggest to create a copy of database via compact and repair database method: DBEngine.CompactDatabase method (DAO) | Microsoft Learn[^]
 
Share this answer
 

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