How to connect with a database even if it's original path is changed? RRS feed

  • Question

  • 1. I have a database (Database1.mdf)

    2. I set the connection string: 

     SqlConnection con = new SqlConnection(Properties.Settings.Default.Database1ConnectionString);

    3. And I tried to modify the database (inserting some data in a table)

    (X) The problem is that the database table is not modified after INSERT statement is executed. (X)

    If I use the original path (D:\MyCSharpPrograms\WindowsFormApplication1\WindowsFormApplication1\Database1.mdf) it works (I can insert data); but the idea is that if I want to change the application location, moving it to another directory, the database path won't be recognized.

    What should I do? Please help! :(

    Wednesday, April 30, 2014 5:04 PM


  • Put the database in a common location on the computer. You can use an environmental variable on the computer like ALLUserProfile, CommonProgramFiles, or LocalAppData to get the path of where the database is stored.  You can get a list of all environmental variables by either going to MyComputer : Advanced : Environmental Variables; or open a cmd.exe and type SET (set will just give a list of variables and not make any changes when typed by itself).


    • Proposed as answer by Caillen Tuesday, May 6, 2014 9:59 AM
    • Marked as answer by Caillen Sunday, May 11, 2014 11:23 AM
    Wednesday, April 30, 2014 11:49 PM