Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-01-31
3
Medium Priority
?
250 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

598 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