Solved

What's the right way to connect and disconnect a msaccess-database?

Posted on 2014-01-31
3
247 Views
Last Modified: 2014-01-31
Hi, I have several componentes on my form that are connected to a msaccess-database.
I have made a button an wrote some code to compact the database. But to make the
button function properly I need save all the data from the components back to the database. That I got:

            glucsTA.Update(glucsDS);
            glucsDS.AcceptChanges();
            suppTA.Update(suppDS);
            suppDS.AcceptChanges();
            favorTA.Update(favorDS);
            favorDS.AcceptChanges();
            calenTA.Update(calenDS);
            calenDS.AcceptChanges();

Open in new window


And I need to stop all the connections to the database. I need to stop the threads.
So I wrote this but still got the message: "Database in use". I have marked the lines of code with an arrow.

        private void bbComDb_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = (Properties.Settings.Default.DBRootConnectionString);
            glucsTA.Update(glucsDS);
            glucsDS.AcceptChanges();
            suppTA.Update(suppDS);
            suppDS.AcceptChanges();
            favorTA.Update(favorDS);
            favorDS.AcceptChanges();
            calenTA.Update(calenDS);
            calenDS.AcceptChanges();
            conn.Open();<======================
            CompactJetDatabase();
            conn.Close();<======================
        }

        public void CompactJetDatabase()
        {
            try
            {
                string oldFileName = Application.StartupPath + "\\DBRoot.mdb";
                string newFileName = Application.StartupPath + "\\temp.mdb";
                JRO.JetEngine engine = new JRO.JetEngine();
                engine.CompactDatabase(String.Format(AccessConnectionString, oldFileName),
                String.Format(AccessConnectionString, newFileName));
                File.Delete(oldFileName);
                File.Move(newFileName, oldFileName);
                XtraMessageBox.Show("Database compressed");
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            }
        }

Open in new window


What is the right way to stop all connections to the database. And restablish the connection again when the compressing is done.

Greetings,

Peter Kiers
0
Comment
Question by:peterkiers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 39823615
Well, in fact you don't need the conn.Open() and conn.Close() lines at all.

What you must do is close the connection to which the glucsTA, suppTA... objects are connected to.

Hope that helps.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 39824124
Hi Luis Perez I don't understand. What is my code going to be?
Is this correct what I have done?

      private void bbComDb_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = (Properties.Settings.Default.DBRootConnectionString);
            glucsTA.Update(glucsDS);
            glucsDS.AcceptChanges();
            suppTA.Update(suppDS);
            suppDS.AcceptChanges();
            favorTA.Update(favorDS);
            favorDS.AcceptChanges();
            calenTA.Update(calenDS);
            calenDS.AcceptChanges();
            conn.Close();
            conn.Open() ;
            CompactJetDatabase();
            conn.Close();
        }

Open in new window


Peter Kiers
0
 
LVL 1

Author Comment

by:peterkiers
ID: 39824840
You mean this:

DevExpress.XtraBars.ItemClickEventArgs e)
        {
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = (Properties.Settings.Default.DBRootConnectionString);
            glucsTA.Update(glucsDS);
            glucsDS.AcceptChanges();
            suppTA.Update(suppDS);
            suppDS.AcceptChanges();
            favorTA.Update(favorDS);
            favorDS.AcceptChanges();
            calenTA.Update(calenDS);
            calenDS.AcceptChanges();
            conn.Close();
            CompactJetDatabase();
        }

Peter
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question