Freelance Project Requests info@CodeDocu.de Software Development in C# WPF Asp.Net Core Vba Excel Word SQL-Server EF Linq, UWP Net
#

 

 

The following script in C #, WPF shows how to store And read SETTINGS values in a Database.

As usual, the Settings data of an Application As usual are changed and stored purely settings form.

The Difference, however, is that the Settings values are not stored and managed as usual in the WPF Settings, but via a central Database, which can be addressed from several Positions.

 

Uses XCeed ColorPicker, Textbox, Linq as Database with EF EntityFramework

 

 

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 = (ColorColorConverter.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

 

 

    }

}

 

 

 

Xaml Code

<Page

      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 

      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

      xmlns:local="clr-namespace:Schichtplaner"

      xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" x:Class="Schichtplaner.Page_Einstellungen"

      mc:Ignorable="d" 

      d:DesignHeight="450" d:DesignWidth="800"

      Title="Page_Einstellungen" Unloaded="Page_Unloaded">

 

    <Grid Margin="30,30,0,0">

        <Grid.RowDefinitions>

            <RowDefinition Height="33"/>

            <RowDefinition Height="33"/>

            <RowDefinition Height="33"/>

            <RowDefinition Height="33"/>

            <RowDefinition Height="33"/>

            <RowDefinition Height="33"/>

            <RowDefinition Height="33"/>

            <RowDefinition Height="55"/>

 

            <RowDefinition Height="*"/>

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="150"/>

            <ColumnDefinition Width="113*"/>

        </Grid.ColumnDefinitions>

 

 

        <Label Content="Zeilenhöhe:" HorizontalAlignment="Left" VerticalAlignment="Center" 

               Grid.Row="0"

               />

        <TextBox x:Name="ctlZeilenhoehe" HorizontalAlignment="Left" 

                 Grid.Row="0" Grid.Column="1" 

                 Width="50" Height="30"

                 ></TextBox>

 

 

 

        <Label Content="Breite Maschinen-Spalte:" HorizontalAlignment="Left" VerticalAlignment="Center" 

               Grid.Row="1"

               />

        <TextBox x:Name="ctlBreite_Maschinen" HorizontalAlignment="Left" 

                 Grid.Row="1" Grid.Column="1" 

                 Width="50" Height="30"

                 ></TextBox>

 

 

        <Label Content="Breite Schicht:" HorizontalAlignment="Left" VerticalAlignment="Center" 

               Grid.Row="2"

               />

        <TextBox x:Name="ctlBreite_Schicht" HorizontalAlignment="Left" 

                 Grid.Row="2" Grid.Column="1" 

                 Width="50" Height="30"

                 ></TextBox>

 

 

 

        <Label Content="Farbe Tage:" HorizontalAlignment="Left"  VerticalAlignment="Center"  

               Grid.Row="3"/>

        <xctk:ColorPicker x:Name="ctlFarbe_Tage" HorizontalAlignment="Left" 

                          Grid.Row="3" Grid.Column="1"

                          Width="97" Height="30"/>

 

        <Label Content="Farbe Schicht:" HorizontalAlignment="Left"  VerticalAlignment="Center"  

               Grid.Row="4"/>

        <xctk:ColorPicker x:Name="ctlFarbe_Schicht" HorizontalAlignment="Left" 

                          Grid.Row="4" Grid.Column="1"

                          Width="97" Height="30"/>

 

        <Label Content="Farbe Tage:" HorizontalAlignment="Left"  VerticalAlignment="Center"  

               Grid.Row="5"/>

        <xctk:ColorPicker x:Name="ctlFarbe_Maschinengruppe" HorizontalAlignment="Left" 

                          Grid.Row="5" Grid.Column="1"

                          Width="97" Height="30"/>

 

        <Label Content="Farbe Maschinen:" HorizontalAlignment="Left"  VerticalAlignment="Center"  

               Grid.Row="6"/>

        <xctk:ColorPicker x:Name="ctlFarbe_Maschinen" HorizontalAlignment="Left" 

                          Grid.Row="6" Grid.Column="1"

                          Width="97" Height="30"/>

 

 

        <Button x:Name="BtnSpeichern" Content="Speichern" 

                HorizontalAlignment="Left" 

                Grid.Row="7" Grid.Column="5"

                Width="140" Height="40"

                Click="BtnSpeichern_Click" 

                />

 

 

 

        <Button x:Name="BtnPfad_oeffnen" Content="Speicherpfad öffnen" 

                HorizontalAlignment="Left" VerticalAlignment="Bottom" 

                Grid.Column="5"

                Width="140" Height="40"

                Click="BtnPfad_oeffnen_Click" Grid.Row="13"

                />

 

 

    </Grid>

</Page>

 

 

 

The Data stored in the Database

Mobile
WPF
»
STORING WPF Data in the Database
»
How to create a database connection in WPF and connect to EF Entity Framework
»
WPF: Sample of FormattedText Outline Text
»
WPF: Add an event function to a UIElement or UserControl
»
WPF: Startwindow einstellen
»
C# Code, WPF: Function zum Bilden eines Hash Werts aus einem String
»
ASP.Net: Webseite von Desktop zu Mobile Seite automatisch wechseln
»
WPF: Eine Anwendung in den Vordergrund Focus stellen mit Topmost
»
WPF: Bitmap aus Clipboard kopieren
»
Gelöst: WPF,C# globale Tastatureingaben in einer WPF Anwendung empfangen

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de