views:

13

answers:

0

Hello Friends,

My problem must surely be easy to solve for someone who's more familiar with Visual Studios than I am. In the long run, I'm trying to publish a database application along with a config file that allows the user to change the connection string and connect with whatever database they wish. So, per the recommendations of [this page][1] I have an app.config file which includes a reference to some external XML fragment that holds my connection string:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  ...
  <connectionStrings configSource="connections.config" />
  ...
</configuration>

And then I have a file called connections.config which is saved to the bin/Debug folder:

<connectionStrings>
  <add name="ConsoleApplication1.Properties.Settings.Setting" 
       connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=&quot;C:\Documents and Settings\Administrator\My Documents\OHA_Management\BarronPersonnelDatabase\ManagementApp\BarronPersonnelDatabaseApp\PersonnelDatabase.mdf&quot;;Integrated Security=True;Connect Timeout=30;User Instance=True"
       providerName="System.Data.SqlClient" />
</connectionStrings>

Finally, for my test program, I have the following main function which simply prints the connection string to the screen:

    static void Main(string[] args)
    {
        ConnectionStringSettingsCollection settings =
            ConfigurationManager.ConnectionStrings;

        if (settings != null)
        {
            foreach (ConnectionStringSettings cs in settings)
            {
                Console.WriteLine(cs.Name);
                Console.WriteLine(cs.ProviderName);
                Console.WriteLine(cs.ConnectionString);
            }
        }
        Thread.Sleep(10000);
    }

If I run this in debug mode, it finds the app.config file and from there the connections.config file and prints the connection string to the screen. Problem is, when I publish and install the solution, the connections.config folder doesn't get copied to the correct folder, and I don't know where that folder is. So I run the program and it throws an exception.

How do I make sure that connections.config is placed in the appropriate directory when I publish the solution?

Thanks, JB

UPDATE:

I am part-way toward the answer. If you right-click the project and go to properties->Publish tab->Application Files button you'll see all of the files that will be placed with your installed solution. I needed to get the connections.config into that list. Unfortunately, there's no "add" button in that window. However, if you right-click on the connection.config file in the Solution Exploder and go to properties you'll see a "build action" field. Change this from "None" to "Content". This implies that you are "content" for Visual C# to do with the file as it sees fit.

After publish and install, the string is correctly printed to the screen.

There is one critical thing I still can't do. Once published, I need to allow access to the connection string so that the user can point it anywhere they wish. I don't know where my file went once I installed it, so I don't know how to change that string.

JB