private void CloseButton_Click(object sender, EventArgs e) { DialogResult dialog = MessageBox.Show("Вы действительно хотите сохранить изменения в подключении к БД?", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); if (dialog == DialogResult.OK) { try { var pgSQLConnect = new PgSQLConnectionSettings() { HostName = HostTextBox.Text, PortAddress = PortTextBox.Text, DataBaseName = NameDbTextBox.Text, UserName = LoginTextBox.Text, Password = PasswordTextBox.Text }; var xmlSer = new XmlSerializer(typeof(PgSQLConnectionSettings)); using (var stream = new FileStream(XML_SQL_CONNECT_FILE_NAME, FileMode.Create)) { xmlSer.Serialize(stream, pgSQLConnect); } this.Close(); MessageBox.Show("Перезапустите приложения для инициализации подключения к БД", "", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception err) { _nlog.Error(err.Message); MessageBox.Show(err.Message, "Не удалось сохранить изменения, повторите попытку.", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private void SQLSettingsForm_Load(object sender, EventArgs e) { try { _pgSQLConnect = DeserializeXML(_nlog); HostTextBox.Text = _pgSQLConnect.HostName; PortTextBox.Text = _pgSQLConnect.PortAddress; NameDbTextBox.Text = _pgSQLConnect.DataBaseName; LoginTextBox.Text = _pgSQLConnect.UserName; PasswordTextBox.Text = _pgSQLConnect.Password; } catch (Exception err) { _nlog.Error(err.Message); MessageBox.Show(err.Message, "Не удалось прочитать настройки подключения", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { } }
public MainForm(ConfigApp configApp, PgSQLConnectionSettings connectSettings, Logger nlog) { InitializeComponent(); _configApp = configApp; _connectSettings = connectSettings; _nlog = nlog; ErrorPLCLabel.Text = ""; ErrorSQLLabel.Text = ""; _timer = new Timer(); if (_configApp.CheckTime == 0 || _configApp.CheckTime <= 10) { _timer.Interval = 30000; } else { _timer.Interval = _configApp.CheckTime * 1000; } _timer.Tick += _timer_Tick; _timer.Start(); }
public SQLSettingsForm(Logger nlog, PgSQLConnectionSettings pgSQLConnect) { InitializeComponent(); _nlog = nlog ?? throw new ArgumentNullException(nameof(nlog)); _pgSQLConnect = pgSQLConnect ?? throw new ArgumentNullException(nameof(pgSQLConnect)); }
public PostgresDataContext(PgSQLConnectionSettings settings) { _settings = settings ?? throw new ArgumentNullException(nameof(settings)); Database.EnsureCreated(); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var connectSettings = new PgSQLConnectionSettings(); var configApp = new ConfigApp(); var nlog = LogManager.GetCurrentClassLogger(); try { var config = DeserializeXML <ConfigApp>(XML_CONFIG_APP_FILE_NAME); configApp.AutoConnectionDataBase = config.AutoConnectionDataBase; configApp.IpAddress = config.IpAddress; configApp.PortAddress = config.PortAddress; configApp.CheckTime = config.CheckTime; } catch (Exception e) { nlog.Error(e.Message); // MessageBox.Show(e.Message, "Системная ошибка, обратитесь к системному администратору", MessageBoxButtons.OK, MessageBoxIcon.Error); configApp.AutoConnectionDataBase = false; configApp.IpAddress = ""; configApp.PortAddress = 9600; configApp.CheckTime = 20; var xmlSer = new XmlSerializer(typeof(ConfigApp)); using (var stream = new FileStream(XML_CONFIG_APP_FILE_NAME, FileMode.Create)) { xmlSer.Serialize(stream, configApp); } } try { var set = DeserializeXML <PgSQLConnectionSettings>(XML_SQL_CONNECT_FILE_NAME); connectSettings.HostName = set.HostName; connectSettings.PortAddress = set.PortAddress; connectSettings.DataBaseName = set.DataBaseName; connectSettings.UserName = set.UserName; connectSettings.Password = set.Password; } catch (Exception e) { nlog.Error(e.Message); //MessageBox.Show(e.Message, "Системная ошибка, обратитесь к системному администратору", MessageBoxButtons.OK, MessageBoxIcon.Error); connectSettings.HostName = ""; connectSettings.PortAddress = ""; connectSettings.DataBaseName = ""; connectSettings.UserName = ""; connectSettings.Password = ""; var xmlSer = new XmlSerializer(typeof(PgSQLConnectionSettings)); using (var stream = new FileStream(XML_SQL_CONNECT_FILE_NAME, FileMode.Create)) { xmlSer.Serialize(stream, connectSettings); } } finally { Application.Run(new MainForm(configApp, connectSettings, nlog)); } }