Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

WPF Daten in der Datenbank speichern

25.03.2019 (👁987)


Das folgende Script in C#, WPF zeigt, wie man Settings Werte in einer Datenbank speichern und wieder lesen kann.

Dabei werden die Settings Daten einer Anwendung wie üblich übe rein Einstellungs-Formular verändert und gespeichert.

Der Unterschied ist aber, dass die Settings werte nicht wie üblich in der WPF Settings gepeichert und verwaltet werden, sondern über eine zentrale Datenbank, welche von mehreren Positionen angesprochen werden kann.

Verwendet XCeed ColorPicker, Textbox, Linq als Datenbank mit 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>

Die Daten in der Datenbank gespeichert