100 lines
3.8 KiB
C#
100 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using DataAccessLayer;
|
|
using System.Configuration;
|
|
using System.Xml.Linq;
|
|
|
|
namespace AppConfigParser
|
|
{
|
|
public class SqlConfigurationParser
|
|
{
|
|
public void Dispose()
|
|
{
|
|
|
|
}
|
|
|
|
public void LoadFromSql(DBProvider provider, string type)
|
|
{
|
|
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
|
|
|
|
using (DataAccessDE de = new DataAccessDE(provider))
|
|
{
|
|
using (var r = de.ExecuteDataReaderStoredProcedure(provider, "dbo.GetTrimestraleSettings", new List<Parametro>() { new Parametro() { ParameterName = "Type", Value = type } }))
|
|
{
|
|
while (r.Read())
|
|
{
|
|
bool isConnectionString = r["IsConnection"].ToString() == "1";
|
|
|
|
string name = r["ParameterName"].ToString();
|
|
string value = r["ParameterValue"].ToString();
|
|
string description = r["ParameterDescription"].ToString();
|
|
|
|
if (!isConnectionString)
|
|
{
|
|
ConfigurationManager.AppSettings[name] = value;
|
|
if (config.AppSettings.Settings[name] != null)
|
|
{
|
|
config.AppSettings.Settings[name].Value = value;
|
|
}
|
|
else
|
|
{
|
|
config.AppSettings.Settings.Add(name, value);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (config.ConnectionStrings.ConnectionStrings[name] != null)
|
|
{
|
|
config.ConnectionStrings.ConnectionStrings[name].ConnectionString = value;
|
|
}
|
|
else
|
|
{
|
|
config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(name, value));
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
config.Save(ConfigurationSaveMode.Modified);
|
|
ConfigurationManager.RefreshSection("appConfig");
|
|
ConfigurationManager.RefreshSection("connectionStrings");
|
|
}
|
|
|
|
public void SaveToSql(DBProvider provider, string type)
|
|
{
|
|
foreach (string key in ConfigurationManager.AppSettings.AllKeys)
|
|
{
|
|
string value = ConfigurationManager.AppSettings[key];
|
|
string name = key;
|
|
SaveToSql(provider, type, name, value, "0");
|
|
}
|
|
foreach (ConnectionStringSettings key in ConfigurationManager.ConnectionStrings)
|
|
{
|
|
string value = key.ConnectionString;
|
|
string name = key.Name;
|
|
SaveToSql(provider, type, name, value, "1");
|
|
}
|
|
}
|
|
|
|
private void SaveToSql(DBProvider provider, string type, string name, string value, string isConnection)
|
|
{
|
|
List<Parametro> p = new List<Parametro>();
|
|
p.Add(new Parametro() { ParameterName = "type", Value = type });
|
|
p.Add(new Parametro() { ParameterName = "name", Value = name });
|
|
p.Add(new Parametro() { ParameterName = "value", Value = value });
|
|
p.Add(new Parametro() { ParameterName = "isConnection", Value = isConnection });
|
|
|
|
using (DataAccessDE de = new DataAccessDE(provider))
|
|
{
|
|
de.ExecuteNonQueryStoredProcedure(provider, "dbo.SetTrimestraleSettings", p);
|
|
}
|
|
}
|
|
}
|
|
}
|