1

I want to change the Connection String parameters that created by Entity Framework in "app.config" file in Runtime. I already can Make another Connection String and use that , But I need to save this connections string in app.config file so that from now on, the program can use the Connection string that saved in app.config file as default connection string.

Thanks

MohsenCs
  • 56
  • 2
  • 8

2 Answers2

0

You can do that by having a code which will open the file, find the place where the connection string is present and replace it with the new value and then save the file.

Community
  • 1
  • 1
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Thanks for your answer, Lajos. Yes I can open that xml file and modify it and save it. but I think there might be another way to change the parameters of connection string not only changing the text. – MohsenCs Mar 25 '17 at 09:13
0

First of all you should set the connection string in the App.Config file. For example I set the connection string for my database as you see here

<configuration>
  <connectionstrings>
    <add name="TestConnectionstring"
    connectionString="Data Source=.;Initial Catalog=CharityManagement;Integrated Security=True"/>
  </add></connectionstrings>
</configuration>

After that you use the connection string in your forms using this code: In your forms you set references that you want to use:

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

Then you can get the Connection String from the App.Config by using the ConnectionStrings property.

var connectionString=ConfigurationManager.ConnectionStrings["TestConnectionstring"].ConnectionString;

You can use this method in both Windows Forms and ASP.NET projects.

anis programmer
  • 989
  • 1
  • 10
  • 25
  • 1
    I Already can use the Connection String everywhere in program. what I need is a way that I can Change the Connections String such that the old connection string in app,config replace by new modified one. thanks anyway – MohsenCs Mar 25 '17 at 09:32