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

How to add a database functionally to a WPF application

16.11.2018 (👁16551)


 

If you already have a WPF application with a local database, then you go in the Solution Explorer

 

Add data model connection

At Project-> Add-> New Item

 

 

Add New Item

Installed-> Visual C # -> Data

ADO NET Entity Data Model

Although you only want to install Entity Framework

Name: dbModel

 

Entity Data Model Wizard->

Choose Model Content

Here is a recommendation: EF Designer from database

Creates a model in the EF Designer based on an existing database. You can choose the database connection, settings for the model, and database objects to include in the model.

The classes your application will interact with are generated from the model.

 

Entity Data Model Wizard->

Choose Your Data Connection

Under Save connection settings in App.Config as

dbConnectionString (as a suggestion)

 

Entity Data Model Wizard

Choose Your Database Objects and Settings

Under Tables -> dbo mark all tables, which should be taken from the server database into the WPF project as data classes or data model

Under Model Namespace: dbModels for database db models (tables, views queries everything in the tree)

dbModels

 

Data model in the WPF project

Then all tables are available directly in the database as a data model

You can find the tables and queries in the data model as in MVC Model

In the .edmx file.

Here is the nickname dbModel.edmx, derived from the model view controller: data model of the database

 

DbContext

Under the file dbModel.Context.cs you will now find the database connection as a ConnectionsString and the connection to the data model, ie local WPF tables to the SQL Server database.

This is called: DbContext (in WPF, Asp.Net MVC and .Core)

 

The DbContext can be found in the file:

dbModel.edmx-> dbModel.Context.ttt-> dbModel.Context.cs

 

Automatically generated connection dbContext

The name dbConnectionString I rename then usually in the usual Application_Database_Context or here App_DbContext

Here in the project named App_dbContext

{

    using System;

    using System.Data.Entity;

    using System.Data.Entity.Infrastructure;

    

    public partial class App_dbContext : DbContext

    {

        public App_dbContext()

            : base("name=dbConnectionString")

        {

        }

    

        protected override void OnModelCreating(DbModelBuilder modelBuilder)

        {

            throw new UnintentionalCodeFirstException();

        }

    

        public virtual DbSet<tblBase_Abwesenheitsgruende> tblBase_Abwesenheitsgruende { getset; }

        public virtual DbSet<tblBase_Feiertage> tblBase_Feiertage { getset; }

        public virtual DbSet<tblBase_Maschinen> tblBase_Maschinen { getset; }

        public virtual DbSet<tblBase_Mitarbeitergruppen> tblBase_Mitarbeitergruppen { getset; }

        public virtual DbSet<tblBase_Schichtmodell> tblBase_Schichtmodell { getset; }

    }

}

 

 

 

Access to data and tables

Through the upper connection you now have the complete easy access from WPF application to the SQL Server tables

 

How to create a database access

If there is no connection from the WPF application to the SQL Server via Compact Database, a connection is opened

App_dbContext db = new App_dbContext();

if (db.Database.Connection.State == System.Data.ConnectionState.Closed || 

db.Database.Connection.State == System.Data.ConnectionState.Broken) await db.Database.Connection.OpenAsync();

 

It's so easy to access a spreadsheet

var data = db.tblBase_Feiertage.FirstOrDefault(); //id => id.IDFeiertag == 1

if (data == null)

{

..

}

 

And that's how you just add a record to the table

//--< Add() >--

data = new tblBase_Feiertage();

data.Feiertag = "neu";

data.dtCreated = DateTime.Now;

db.tblBase_Feiertage.Add(data);

//--</ Add() >--

 

 

 

Data Access as Code Example in C #

With Linq

public async void load_Data()

        {

            App_dbContext db = new App_dbContext();

            if (db.Database.Connection.State == System.Data.ConnectionState.Closed || db.Database.Connection.State == System.Data.ConnectionState.Broken) await db.Database.Connection.OpenAsync();

 

            try

            {

                var data = db.tblBase_Feiertage.FirstOrDefault(); //id => id.IDFeiertag == 1

                if (data == null)

                {

                    //--< Add() >--

                    data = new tblBase_Feiertage();

                    data.Feiertag = "neu";

                    data.dtCreated = DateTime.Now;

                    db.tblBase_Feiertage.Add(data);

                    //--</ Add() >--

                }

                else

                {

                    ctlFeiertag.Text = data.F;

                    //--< Update() >--

                    //data.dtEdit = DateTime.Now;

                    //--</ Update() >--

                }

                await db.SaveChangesAsync();

 

            }

            catch (Exception ex)

            {

 

            }

 

 

        }