-2

When i installed myProg.exe into Client computer, I have to adapt the connectionString.

So i open the file MyProg.exe.config, and modified the connectionString, but it not work.

Then I found this article . The problem now when I modified access into public, I can not compile / deploy it.

my App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>

    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="FrontEnd_Offline.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <system.windows.forms jitDebugging="true" />
  <connectionStrings>
    <add name="Soft8Exp_ClientConnStr" connectionString="Data Source=xxxxx;Initial Catalog=xxxxx;User ID=sa;Password=xxxxx" providerName="System.Data.SqlClient" />
    <add name="Soft8Exp_ClientEntities" connectionString="metadata=res://*/Domain.Entite_T.csdl|res://*/Domain.Entite_T.ssdl|res://*/Domain.Entite_T.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=xxxxx;initial catalog=xxxxx;user id=sa;password=xxxxx;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="FrontEnd_Offline.Properties.Settings.Soft8Exp_ClientConnectionString" connectionString="Data Source=xxxxx;Initial Catalog=xxxxx;User ID=sa;Password=xxxxx" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <applicationSettings>
    <FrontEnd_Offline.Properties.Settings>
      <setting name="NO_ORDRE" serializeAs="String">
        <value />
      </setting>
    </FrontEnd_Offline.Properties.Settings>
  </applicationSettings>
</configuration>

and class to connect:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

using System.Data;
using System.Configuration;

namespace FrontEnd_Offline.Domain
{
    class Connexion : IDisposable 
    {
        public SqlConnection conn;
        public SqlConnection GetConnected()
        {
            try
            {
                String strConnectionString = ConfigurationManager.ConnectionStrings["Soft8Exp_ClientConnStr"].ConnectionString;
                conn = new SqlConnection(strConnectionString);
            }
            catch (Exception excThrown)
            {
                conn = null;
                throw new Exception(excThrown.InnerException.Message, excThrown);
            }

            // Ouverture et restitution de la connexion en cours
            if (conn.State == ConnectionState.Closed) conn.Open();
            return conn;
        }

        public Boolean IsConnected
        {
            get { return (conn != null) && (conn.State != ConnectionState.Closed) && (conn.State != ConnectionState.Broken); }
        }

        public void CloseConnection()
        {
            // Libération de la connexion si elle existe
            if (IsConnected)
            {
                conn.Close();
                conn = null;

            }

        }

        public void Dispose()
        {
            CloseConnection();
        }
    }
}
user609511
  • 4,091
  • 12
  • 54
  • 86
  • 2
    What do you mean: "but it not work"? Are you getting errors? What's the problem? Usually, when the connection string is stored in the exe.config file it is being used. When you modify it, the modified connection string is being used. We do it all the time for Windows Services. – Thorsten Dittmar May 15 '12 at 14:16
  • Have you tried [Slow Cheetah XML Transforms](http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5)? – jrummell May 15 '12 at 14:25
  • Could you show the relevant part of your app.config where you save/set the connectionstring? I suspect that you have something wrong there. – Steve May 15 '12 at 15:50
  • @ThorstenDittmar not work, it mean that it not read the Myprog.exe.config. because i have make a experiment: i adapt the connectionString client before ClickOnce deploy, and it work well. and then i modified Myprof.exe.config with other variable to make it not work, but it still work...that prove Myprog.exe.config it has not been read at all. – user609511 May 16 '12 at 08:32
  • Ahaa! You're talking about ClickOnce deployment! You could really modify your question accordingly, because things are a bit different here. The configuration file *is* being read, but it is being copied to your user folder. To incorporate changes you make to values when re-deploying (upgrades), you need to call `Properties.Settings.Default.Upgrade` somewhere in your code. See my answer. – Thorsten Dittmar May 16 '12 at 09:02
  • I also wonder how you did your "experiment": In which folder did you change the Myprof.exe.config "with other variable to make it not work"? You must have searched deeply into your user profile to find the configuration file. If you did not, then your "experiment" was wrong. If you really ClickOnce-deployed your application, you can not just open the configuration file like that anymore. – Thorsten Dittmar May 16 '12 at 09:11
  • my "experiment" i change in source code it self (that mean, i cannot connect with my own database, because i set to client connectionstring) and then i deploy with clickonce. is not a good solution. when i have 100 client, then i have to deploy each of them with their propriate connectionString. what i want is, i will create with my connectionString, and then my employe will change the Myprog.exe.config. – user609511 May 16 '12 at 09:16
  • 1
    Ok. The connection string section of a config file *can not be changed* no matter whether ClickOnce or normal installation through the application. I suggest you read a) the documentation on the .NET settings mechanism (esp. user settings) and **b) my answer**. In short: Create user settings for each connection parameter. Create a dialog where the user can change the user settings. Use `SqlConnectionStringBuilder` to create a connection string from the user settings. See my edited answer. – Thorsten Dittmar May 16 '12 at 09:19

2 Answers2

1

You didn't say you were using ClickOnce in the beginning. Things are different there: The configuration file is stored in the user's profile. You can not change the file afterwords (only user settings through your application).

To update a ClickOnce program with a new connection string I suggest the following:

  1. Create a new user setting called "SettingsUpgradeNeeded" and set it to true in the settings designer
  2. Add the following code to your application's startup code (Program.cs or App.cs)

    if (Properties.Settings.Default.SettingsUpgradeNeeded)
    {
        Properties.Settings.Default.Upgrade();
        Properties.Settings.Default.SettingsUpgradeNeeded = false;
        Properties.Settings.Default.Save();
    }
    
  3. Publish a new update.

Another way to change the connection is (and this is even more preferrable seeing that you only use SqlConnection in your code):

  1. Stop using the ConnectionStrings section
  2. For every connection parameter, create a setting (for example: Database server, Database name, etc.)
  3. Use the SqlConnectionStringBuilder to generate a connection string from these setting

EDIT
You want the user to be able to change the connection parameters:

  1. Create a user setting for each parameter you need to connect to the database (for example: server, database, failover partner, user name, authentication method, etc.)
  2. Create a dialog where you can configure these values
  3. Use the SqlConnectionStringBuilder to create a connection string from these values
  4. Use that connection string for all your database connections

Example:

SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder();
sb.DataSource = Properties.Settings.Default.DBServerName;
sb.InitialCatalog = Properties.Settings.Default.DBDatabaseName;
sb.IntegratedSecurity = Properties.Settings.Default.DBUseIntegratedSecurity;
if (!sb.IntegratedSecurity)
{
    sb.UserId = Properties.Settings.Default.DBUserName;
    sb.Password = Properties.Settings.Default.DBPassword;
}


using (SqlConnection conn = new SqlConnect(sb.ConnectionString))
{
    ...
}

I'd generate a static property either in Program.cs or the App class that can return the connection string from the SqlConnectionStringBuilder.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
-1

Do you mean the connectionString to your database? If so, it should be changed in your database connection class. The connectionString on a local device should be similar to a normal path if your database is also stored locally. Like this:

"C:\\Some_location\\database.extension"

The extension depends on the type of database you are using, change this string to the location of your database.

Tim Kranen
  • 4,202
  • 4
  • 26
  • 49