Click here to Skip to main content
15,917,731 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
dear all
i have win form application using entity framework and create a connection string then export this connection to c:/folder name/CN.txt as " Server =CAVE; Database = IT_Manager; Trusted_Connection = True ;" i need to read the connection from text file when load login form if folder , file and valid connection exist run app with this connection else load connection string form to save the connection

What I have tried:

//app config 
< connectionStrings>
     <add name="GYM_AdminEntities"  connectionString="metadata=res://*/Entities.Entities.csdl|res://*/Entities.Entities.ssdl|res://*/Entities.Entities.msl;provider=System.Data.SqlClient;provider connection 
     string="data source=CAVE;initial catalog=GYM_Admin;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
    providerName="System.Data.EntityClient" />
  </connectionStrings>
// class
public SetConnection()
        {
            config = 
               ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        }


        public string getconnectionstring(string key)
        {
            return config.ConnectionStrings.ConnectionStrings[key].ConnectionString;
        }


        public void saveconnection(string key , string value)
        {
            config.ConnectionStrings.ConnectionStrings[key].ConnectionString = value;
            config.ConnectionStrings.ConnectionStrings[key].ProviderName = 
            "System.Data.EntityClient";
            config.Save(ConfigurationSaveMode.Modified);
        }
//   code
       string txtcn = File.ReadAllText(@"C:\GYM Admin\CN.txt");
                    Controls.SetConnection conn = new Controls.SetConnection();
                    conn.saveconnection("GYM_AdminEntities", txtcn);
Posted
Updated 18-Sep-18 12:08pm
v8
Comments
[no name] 16-Sep-18 20:26pm    
You need to show what you've tried. I don't see anything in that question that shows that you've attempted to solve this yourself. Use string[] lines = File.ReadAllLines("Path To File"); to read the file. using System.IO required.
Please update your question to show some effort.
Wessam A Halim 16-Sep-18 21:33pm    
sorry i tried more than 10 code to update app config with new connection string text but i have error in all so i past the last one ... i'm update it now

This is a bit overkill for your application, but it's the way I do it: Instance Storage - A Simple Way to Share Configuration Data among Applications[^]

The simple way is just to add a Settings string to your app and load it from Properties.Settings.Default.MyConnectionString: Using Settings in C#[^]
 
Share this answer
 
Comments
Wessam A Halim 17-Sep-18 7:09am    
i tried to create class to get and set connection from app config the ((code updated ))
but still didn't change anything in app config without errors
this is the best solution to save the connection in local config file
using the same class (set connection )


          conn.Open();
      Controls.SetConnection con = new Controls.SetConnection();

con.saveconnection("Name",string.Format(@"metadata=res://*/Entities.Entities.csdl|res://*/Entities.Entities.ssdl|res://*/Entities.Entities.msl;provider=System.Data.SqlClient;provider connection string=""{0}; integrated security = True; MultipleActiveResultSets = True; App = EntityFramework""", ConnectionString));

      Application.Restart();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900