none
which is the Best way to deploy a WPF Database Application RRS feed

  • Question

  • I have a WPF application which interacts with a local sql server db (local to every user). The Utility is provided to the user as an installer. So, If the user is installing the utility for the first time, then the db should be created but when a new version is updated(i.e. the user upgrades his installation), the data (which is specific to the user) should not be overwritten, it should be retained while the changes to the db(if any) should be incorporated (Its OK if this is done programmatically). So how should i go about this ??

    I have gone through lots of WPF DB application examples but none of them mention how to retain user data when updating your application....please suggest me the best way to go for this or the best practices that i should be aware of ??

    Also, if its a local database, does it matter if it is .sdf or .mdf ?? that should not be a problem, right ??

    Tuesday, September 11, 2012 11:36 AM

Answers

  • I believe the best way is to use the database in the application as just an empty template database.

    Your application should check for a "live" version of the database, or pull a copy of the template version.

    class DatabaseProvider
    {
        public static void CheckDatabaseExists()
        {
            var applicationFolder = System.AppDomain.CurrentDomain.BaseDirectory;
            var databaseFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "MyApplication");
            var databaseTemplateFile = Path.Combine(applicationFolder, "ApplicationDatabaseTemplate.sdf");
            var databaseFile = Path.Combine(databaseFolder, "ApplicationDatabase.sdf");
    
            if (!Directory.Exists(databaseFolder))
                Directory.CreateDirectory(databaseFolder);
    
            if (!File.Exists(databaseFile))
                File.Copy(databaseTemplateFile, databaseFile);
        }
    }

    If the folder or file doesn't exist, they are created, otherwise no action (leaving existing database files after an upgrade). Your database connection string should point to this "live" version of the database, not the template.

    This specific code snippet assumes the sdf file is in the root directory of the application, and properties for the sdf are Build Action="Content" & "Copy if newer". It is up to you where you store the actual "live" copy of the database, either in the same place as the template, or as I have shown, in a user friendly place like MyDocuments.

     

    Regards,
    Pete


    #PEJL


    Tuesday, September 11, 2012 1:39 PM
    Moderator

All replies

  • I believe the best way is to use the database in the application as just an empty template database.

    Your application should check for a "live" version of the database, or pull a copy of the template version.

    class DatabaseProvider
    {
        public static void CheckDatabaseExists()
        {
            var applicationFolder = System.AppDomain.CurrentDomain.BaseDirectory;
            var databaseFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "MyApplication");
            var databaseTemplateFile = Path.Combine(applicationFolder, "ApplicationDatabaseTemplate.sdf");
            var databaseFile = Path.Combine(databaseFolder, "ApplicationDatabase.sdf");
    
            if (!Directory.Exists(databaseFolder))
                Directory.CreateDirectory(databaseFolder);
    
            if (!File.Exists(databaseFile))
                File.Copy(databaseTemplateFile, databaseFile);
        }
    }

    If the folder or file doesn't exist, they are created, otherwise no action (leaving existing database files after an upgrade). Your database connection string should point to this "live" version of the database, not the template.

    This specific code snippet assumes the sdf file is in the root directory of the application, and properties for the sdf are Build Action="Content" & "Copy if newer". It is up to you where you store the actual "live" copy of the database, either in the same place as the template, or as I have shown, in a user friendly place like MyDocuments.

     

    Regards,
    Pete


    #PEJL


    Tuesday, September 11, 2012 1:39 PM
    Moderator
  • Thank you....that is a unique approach than what i have been trying and also makes sense.....one more thing though, you say "pull a copy of the template version"....what exactly do you mean....run the db creation or modification script (as applicable) ?? or is this some feature i have yet to learn about :) ??

    Tuesday, September 11, 2012 4:30 PM
  • Sorry, "pull a copy" is just slang for make a copy, as in File.Copy. I didn't mean anything by it.

    The template version is always just a raw empty copy that gets deployed each time you update, that you can copy out again if you want a reset/patch feature.

    Regards,
    Pete


    #PEJL

    Tuesday, September 11, 2012 6:05 PM
    Moderator