using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Data.SqlClient;
using System.Diagnostics; //*Process
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Schichtplaner
{
public partial class Page_Einstellungen : Page
{
private dbConnectionString db;
#region Formular
public Page_Einstellungen()
{
InitializeComponent();
db = new dbConnectionString();
if (db.Database.Connection.State == System.Data.ConnectionState.Closed || db.Database.Connection.State == System.Data.ConnectionState.Broken)
db.Database.Connection.Open();
Einstellungen_laden();
}
private void Page_Unloaded(object sender, RoutedEventArgs e)
{
Einstellungen_speichern();
}
#endregion /Formular
#region Buttons
private void BtnDatenbank_sichern_Click(object sender, RoutedEventArgs e)
{
Save_Database();
}
private void BtnDatenbank_einlesen_Click(object sender, RoutedEventArgs e)
{
Load_Database();
}
private void BtnSpeichern_Click(object sender, RoutedEventArgs e)
{
Einstellungen_speichern();
}
private void BtnPfad_oeffnen_Click(object sender, RoutedEventArgs e)
{
string path = System.AppDomain.CurrentDomain.BaseDirectory;
Process.Start(path);
}
#endregion
#region Methods
//----------------------< region: Methods >-------------------
public void Save_Database()
{
//------------< Save_Database() >------------
//< close DB >
//System.Data.Entity.DbContext db = new dbConnectionString();
//db.Database.Connection.Dispose();
//db.Database.Connection.Close();
try
{
string sDB_Name = "DB_Schichtplaner.mdf";
string sBackup_Name = "DB_Schichtplaner_Backup.mdf";
//< get current path >
string path = System.AppDomain.CurrentDomain.BaseDirectory;
//Process.Start(path);
//</ get current path >
//--< Backup_MDF_File >--
if (File.Exists(sDB_Name) == true)
{
//-< Sicherung loeschen >-
if (File.Exists(sBackup_Name) == true)
{
File.Delete(sBackup_Name);
}
File.Copy(sDB_Name, sBackup_Name);
//-</ Sicherung loeschen >-
}
//--</ Backup_MDF_File >--
//--< Backup_LDF_File >--
sDB_Name = sDB_Name.Replace(".mdf", "_log.ldf");
sBackup_Name = sBackup_Name.Replace(".mdf", "_log.ldf");
if (File.Exists(sDB_Name) == true)
{
//-< Sicherung loeschen >-
if (File.Exists(sBackup_Name) == true)
{
File.Delete(sBackup_Name);
}
File.Copy(sDB_Name, sBackup_Name);
//-</ Sicherung loeschen >-
}
//--</ Backup_LDF_File >--
MessageBox.Show("OK, Datenbank wurde gesichert");
}
catch (Exception ex)
{
MessageBox.Show("Problem, Datenbank konnte nicht gesichert werden." + Environment.NewLine + ex.Message);
}
//------------</ Save_Database() >------------
}
public void Load_Database()
{
//------------< Load_Database() >------------
try
{
string sDB_Name = "DB_Schichtplaner.mdf";
string sBackup_Name = "DB_Schichtplaner_Backup.mdf";
//< get current path >
string path = System.AppDomain.CurrentDomain.BaseDirectory;
//</ get current path >
//--< MDF_File >--
if (File.Exists(sBackup_Name) == true)
{
//-< Datanbank loeschen >-
if (File.Exists(sDB_Name) == true)
{
File.Delete(sDB_Name);
}
File.Copy(sBackup_Name, sDB_Name);
//-</ Datanbank loeschen >-
}
//--</ MDF_File >--
//--< LDF_File >--
sDB_Name = sDB_Name.Replace(".mdf", "_log.ldf");
sBackup_Name = sBackup_Name.Replace(".mdf", "_log.ldf");
if (File.Exists(sBackup_Name) == true)
{
//-< Datanbank loeschen >-
if (File.Exists(sDB_Name) == true)
{
File.Delete(sDB_Name);
}
File.Copy(sBackup_Name, sDB_Name);
//-</ Datanbank loeschen >-
}
//--</ LDF_File >--
MessageBox.Show("OK, Datenbank wurde geladen");
}
catch (Exception ex)
{
MessageBox.Show("Problem, Datenbank konnte nicht geladen werden." + Environment.NewLine + ex.Message);
}
////< close DB >
//dbConnectionString db = new dbConnectionString();
//if (db.Database.Connection.State != System.Data.ConnectionState.Closed) db.Database.Connection.Close();
////< close DB >
//------------</ Load_Database() >------------
}
public void Einstellungen_laden()
{
//--------< Einstellungen_laden >--------
Textfeld_laden(ctlBreite_Schicht, "Breite_Schicht", "60");
Textfeld_laden(ctlBreite_Maschinen, "Breite_Maschinen", "100");
Textfeld_laden(ctlZeilenhoehe, "Zeilenhoehe", "30");
Farbwert_laden(ctlFarbe_Schicht, "Farbe_Schicht");
Farbwert_laden(ctlFarbe_Tage, "Farbe_Tage");
Farbwert_laden(ctlFarbe_Maschinengruppe, "Farbe_Maschinengruppe");
Farbwert_laden(ctlFarbe_Maschinen , "Farbe_Maschinen");
//--------</ Einstellungen_laden >--------
}
public void Einstellungen_speichern()
{
//--------< Einstellungen_speichern >--------
Textfeld_speichern(ctlBreite_Schicht, "Breite_Schicht", "60");
Textfeld_speichern(ctlBreite_Maschinen, "Breite_Maschinen", "100");
Textfeld_speichern(ctlZeilenhoehe, "Zeilenhoehe", "30");
Farbwert_speicher(ctlFarbe_Schicht, "Farbe_Schicht");
Farbwert_speicher(ctlFarbe_Tage, "Farbe_Tage");
Farbwert_speicher(ctlFarbe_Maschinengruppe, "Farbe_Maschinengruppe");
Farbwert_speicher(ctlFarbe_Maschinen, "Farbe_Maschinen");
//--------</ Einstellungen_speichern >--------
}
private void Textfeld_laden(TextBox textBox,string sSetting,string sDefault)
{
//------------< Textfeld_laden() >-----------------
string sWert = db.tblSys_Einstellungen.FirstOrDefault(rec => rec.Einstellung == sSetting).Wert;
if (sWert == null)
{
textBox.Text = sDefault;
}
else
{
textBox.Text = sWert;
}
//------------</ Textfeld_laden() >-----------------
}
private void Textfeld_speichern(TextBox textBox, string sSetting, string sDefault)
{
//------------< Textfeld_laden() >-----------------
//< Zeilenhoehe >
tblSys_Einstellungen setup = db.tblSys_Einstellungen.FirstOrDefault(rec => rec.Einstellung == sSetting);
if (setup == null)
{
setup = new tblSys_Einstellungen();
setup.Wert = sDefault;
}
setup.Wert = textBox.Text;
db.SaveChanges();
//</ Zeilenhoehe >
//------------</ Textfeld_laden() >-----------------
}
private void Farbwert_laden(Xceed.Wpf.Toolkit.ColorPicker picker,string sFarbwert)
{
//------------< Farbwert_speicher() >-----------------
//--< setup speichern >--
tblSys_Einstellungen setup = db.tblSys_Einstellungen.FirstOrDefault(rec => rec.Einstellung == sFarbwert);
if (setup != null)
{
picker.SelectedColor = (Color) ColorConverter.ConvertFromString( setup.Wert);
}
//--</ setup speichern >--
//------------</ Farbwert_speicher() >-----------------
}
private void Farbwert_speicher(Xceed.Wpf.Toolkit.ColorPicker picker, string sFarbwert)
{
//------------< Farbwert_speicher() >-----------------
string sWert = picker.SelectedColorText;
if (sWert != "")
{
//--< setup speichern >--
tblSys_Einstellungen setup = db.tblSys_Einstellungen.FirstOrDefault(rec => rec.Einstellung == sFarbwert);
if (setup != null)
{
setup.Wert = sWert;
db.SaveChanges();
}
//--</ setup speichern >--
}
//------------</ Farbwert_speicher() >-----------------
}
//----------------------</ region: Methods >-------------------
#endregion
}
}
|