public static string GetConnectionStringsConfig(string connectionName) { string file = System.Windows.Forms.Application.ExecutablePath; System.Configuration.Configuration sysconfig = ConfigurationManager.OpenExeConfiguration(file); string connectionString = sysconfig.ConnectionStrings.ConnectionStrings[connectionName].ConnectionString.ToString(); return connectionString; } public static void UpdateConnectionStringsConfig(string newName, string newConString) { string file = System.Windows.Forms.Application.ExecutablePath; Configuration sysconfig = ConfigurationManager.OpenExeConfiguration(file); bool exist = false; if (sysconfig.ConnectionStrings.ConnectionStrings[newName] != null) { exist = true; } if (exist) { sysconfig.ConnectionStrings.ConnectionStrings.Remove(newName); } ConnectionStringSettings mySettings = new ConnectionStringSettings(newName, newConString); sysconfig.ConnectionStrings.ConnectionStrings.Add(mySettings); sysconfig.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("ConnectionStrings"); }
时间: 2024-10-11 06:03:54