Solved

Could you point how to programatically change an Access database using C#?

Posted on 2016-09-09
13
54 Views
Last Modified: 2016-09-13
Hi Experts

Could you point how to programatically change an Access database using C#?

I'm involved in develop an application that I expect, from time to time, for this to work out, changes in an Access database  must have to be done.

The app is distributed and installed at user's machine. So I need to programatically apply the changes when needed. When a new release is published and installed, and then at the .exe very first run, it automatically find the necessary changes to be made at Access database and do it, making the app runs perfectly.

Thanks in advance.
0
Comment
Question by:Eduardo Fuerte
13 Comments
 
LVL 13

Expert Comment

by:John Tsioumpris
Comment Utility
Well how easy is this it depends on the kind of you manipulation you need to perform.If it is on table/query level then you easily "send" CRUD commands to Access to do what you want.
In forms/reports it seems there is a bit more complicated although it there are available "tools" ( Access.Controls, VBIDE.VBComponent,VBIDE.VBProject,VBIDE.Properties) to manipulate them.As a last resort you can use the undocument command "Application.SaveAsText" to export the full definition of your application and perform all kind of manipulation.
0
 
LVL 5

Expert Comment

by:Anders Ebro (Microsoft MVP)
Comment Utility
The regular approach to this is:
Split your Access application into a "app/client part" and a "backend/server part".
Note that the backend can be either an access database, SQL server db or some other database.

Now distribute the client part to your users (preferably as a accde file, so they can't mess with the code), and when you need to do updates, you simple distribute a new accde file to everyone.
A Version check on startup can ensure that everyone is using the latest version.
0
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
You have two options:

1) Updating an existing database. Depending on the kind of changes you need either Access-SQL and/or ADOX.

Caveat: applying changes to an Access database so that the result is an exact copy is under some circumstances not possible, cause not all internals are public accessible.

2) Always deploy the actual version of your database and import the data from the old version.

Caveat: this can be a multistep operation.

And as always: Create backups before applying updates.
0
 

Author Comment

by:Eduardo Fuerte
Comment Utility
Hi for all and thank you for your replies.

ADOX is something similar to MS-SQLServer DDL isn't it?

I guess an approach could be an Access app responsible for all needed table creation, tables' structure changes and so on, no C# codes used for this, am I right?
0
 
LVL 32

Assisted Solution

by:Stefan Hoffmann
Stefan Hoffmann earned 150 total points
Comment Utility
ADOX is equivalent to SQL Server Management Objects (SMO), but it is only an abstraction on the OLE DB layer.

In the beginning I used Access SQL DDL scripts to adjust the structures and Access SQL DML to adjust the data.

But in the end I often distributed a new database and let the installer or application on the first run import the data from the old database. So I only need Access SQL DML scripts.
And in the I even used an "update" Access database to host the migration queries and necessary procedures. Then the update process linked the tables from the old database and the new database. Then the queries and when needed macros where executed.

Both ways a viable.

The first can lead to much shorter and faster scripts in many cases. But you need to script out every change, which can be a bit of a problem, when you don't do it always on the fly. And it needs serious testing of each step.

The second approach has a simpler logic. Thus fewer pit falls, cause not everything can be scripted as DDL. Also it is easier to test and you can write your migration scripts in the end.
0
 
LVL 34

Expert Comment

by:PatHartman
Comment Utility
Back to Ander's point:
Is the database split into FE and BE?  If not, why not?  All shared applications should be split as well as any application you create for another person.  I occasionally create apps for my own use that are not split but it would be very poor practice for me to create monolithic apps for the use of others - shared or not shared because it would make updating the application objects very difficult.

Splitting the app allows you to simply replace the FE with an updated version when you want to push out a code update.  When you make schema changes, you can use DAO or ADO to make the changes after first backing up the client's data file.

For my apps that are distributed to the public, I create an Access database with VBA and DAO to update the BE or run scripts should the BE be SQL Server.  But I would NEVER attempt to poke changes into a FE.  In fact, if you distribute the FE as an .accde, you would not be able to update forms/reports/code anyway.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:Eduardo Fuerte
Comment Utility
At first sight It looks something really new for me, just a moment to reflect a little.
0
 

Author Comment

by:Eduardo Fuerte
Comment Utility
After a more appurated reading to everyone posts.

The suggestion is to mantain an "app/client part (FE - frontend)" and a "backend/server part (BE)" of Access database, isn't it?

I'm certainly misconcepting concepts but what I catch is:

If the client app (C#) identifies an database version change it automatically asks for download the newer version of database from internet server.

After download,  a mechanism(*) first backups the current database and starts to migrate the current data to the newer database just downloaded. Then the  FE app could start now.

(*)VBA and DAO to update the BE or run scripts should the BE be SQL Server.

Could you check my interpretation?
0
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
Ignore the BE/FE part. This is about plain Access-Access applications. Where the UI is also in Access.

Where does SQL Server comes into play now?
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 350 total points
Comment Utility
It looks like you have a C# app that is connecting to an Access database. If so, then the repeated suggestions to split that Access database are unnecessary - you don't split a data-only database.

If your C# application needs to make structural changes to the Access database, DAO is generally the preferred method for doing so. Jet/ACE does support a subset of DDL commands, so you could certainly use those if all you're doing is adding columns and such.

But you don't need to split your Access database if all it does is host your data.
0
 

Author Comment

by:Eduardo Fuerte
Comment Utility
@ste5an
I mentioned "SQL Server" only to reinforce my interpretation on what @PatHartman had posted.
It doesn't came into play, sorry. "Ignore the BE/FE part. This is about plain Access-Access applications. Where the UI is also in Access" cleared my overall comprehension.

@Scott
Your considerattions are very closer on what I expect to do since the start. I did it similar with other systems I distributed before, using another tecnology.

In this case it's just a matter of construct a class to deal with the changes needed, basically:  to create a newer table or add new columns to existing tables. Using " Jet/ACE  subset of DDL commands"as you mentioned.

Everytime the C# system starts it checks if it's necessary to actualize Access database.

I guess @ste5an and @Scott approaches are aligned to what I need to do.

Now it's up to me to find "Jet/ACE  subset of DDL commands" to fit my purposes. Maybe you could possibly post an small example on how to do this.
0
 

Author Comment

by:Eduardo Fuerte
Comment Utility
In truth, it looks easy an easy task, I tryed that way, with success.

  public void CriaTabela() // Creates a table
        {
            try
            {
                string strTemp = " [KEY] Text, [VALUE] Text ";
                OleDbCommand myCommand = new OleDbCommand();

                myCommand.Connection = clsConnections.CN;
                myCommand.CommandText = "CREATE TABLE table1(" + strTemp + ")";
                myCommand.ExecuteNonQuery();
                myCommand.Connection.Close();
            }
                 catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        public void CriaColuna() // Creates a column
        {
            try
            {
                OleDbCommand myCommand = new OleDbCommand();
                myCommand.Connection = clsConnections.CN;
                myCommand.CommandText = "ALTER TABLE table1 ADD COLUMN SignIn DATETIME"; 
                myCommand.ExecuteNonQuery();
                myCommand.Connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
    }

Open in new window


img001
0
 

Author Closing Comment

by:Eduardo Fuerte
Comment Utility
Thank you for the assistance!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now