Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had this problem with a project... everything is fine, all wonders... When you run the application in you computer, how do you adapt the database connection to the different users?</p> <p>I did everything here and other many post similar to this, and nothing seams to work... Yes, the code works, the data context catch the new info for the connection, but it simply DON'T WORK... :'(</p> <p>I found a lifesaver example that solve my problems, it's a bunch of code but, <code>DBCon</code> has the information of the new connection:</p> <pre><code>Encripter enc = new Encripter(); string strconfig = u.readFile(u.ubicacionActual() + "\\conn.config"); DBCon dbcon = JsonConvert.DeserializeObject&lt;DBCon&gt;(enc.decrypt(strconfig)); Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); EntityConnectionStringBuilder efb = new EntityConnectionStringBuilder(config.ConnectionStrings.ConnectionStrings["ProyectEntities"].ConnectionString); SqlConnectionStringBuilder sqb = new SqlConnectionStringBuilder(efb.ProviderConnectionString); // Now we can set the datasource sqb.DataSource = dbcon.datasource; sqb.InitialCatalog = dbcon.catalog; sqb.UserID = dbcon.user; sqb.Password = dbcon.password; efb.ProviderConnectionString = sqb.ConnectionString; // to rewrite the app.config! ChangeEFConnectionString("ProyectEntities", efb.ProviderConnectionString); ProyectEntities db = new ProyectEntities(); // dbcontext </code></pre> <p>The magic function (Well it's not really magic! Just overwrite the <code>app.config</code>)</p> <pre><code>private bool ChangeEFConnectionString(string connStringName, string newValue) { try { //CreateXDocument and load configuration file XDocument doc = XDocument.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); //Find all connection strings var query1 = from p in doc.Descendants("connectionStrings").Descendants() select p; //Go through each connection string elements find atribute specified by argument and replace its value with newVAlue foreach (var child in query1) { foreach (var atr in child.Attributes()) { if (atr.Name.LocalName == "name" &amp;&amp; atr.Value == connStringName) { if (atr.NextAttribute != null &amp;&amp; atr.NextAttribute.Name == "connectionString") { // Create the EF connection string from existing EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(atr.NextAttribute.Value); // entityBuilder.ProviderConnectionString = newValue; //back the modified connection string to the configuration file atr.NextAttribute.Value = entityBuilder.ToString(); } } } } doc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); return true; } catch (Exception ex) { Console.WriteLine(ex.Message); return false; } } </code></pre> <p>All credits to <a href="http://bhrnjica.net/2011/01/30/handling-with-connection-string-at-run-time-in-developing-the-desktop-applications/" rel="nofollow">a Bahrudin Hrnjica</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload