Free Code Snippets in C#, Net Framework, Office 365, ASP.Net,WPF, Visual Studio, SQL Server, Antivirus free
#

 

 

If you need a database in WPF that only serves as a data store for the WPF application, then you should create a local database.

The database is created and linked with Add New Item-> Service Based Database.

This is a local SQL Server database that places the file in the project directory.

 

add

Only under the project with Add new Item

 

 

Then you insert a Service Based Database element

 

This inserts a database of mdf into the project

 

Also located as SQL Server .mdf database in the file directory.

 

This database can then be opened by double-clicking in the SQL Server Explorer, which roughly corresponds to the SQL Server Management Studio.

 

 

Add a table

With Database-> Tables-> Add New Table, a new table is created with an ID field as a template

 

 

When the table is rebuilt, the T-SQL script is automatically generated

 

Attention to changes

However, you should urgently only change data in the design area, because changes in the T-Sql script also delete complete data

 

CREATE TABLE [dbo].[Table]

(

       [IDDetail] INT NOT NULL PRIMARY KEY,

    [URL] NVARCHAR(255) NULL,

    [Title] NVARCHAR(255) NULL,

    [Text] NVARCHAR(MAX) NULL

)

 

 

Saving changes

The changes are saved with an update in the menu area

 

 

Naming a table

The name of a table is entered in the script below

If you subsequently rename or rename the table in the script, a completely new table is created as a copy.

 

 

Connection string to the database

The connection to the database can be found by opening the Database-> Properties and taking the values ​​under Connenction String.

In this example, on Data Source, it shows local DB with the file path

Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C..\_data\dbWebRobot_ComputerFutures.mdf;Integrated Security=True

 

 

This connection as a string is best copied into the settings of your own project

 

This allows you to connect the database with a SQLClient and the settings in the project.

//< db oeffnen >

string cn_String = Properties.Settings.Default.connection_String ;

SqlConnection cn = new SqlConnection(cn_String);

//</ db oeffnen >

 

 

By outsourcing the database system accesses to a clsDB class, one can simplify the essential access to the database on read and write operations like here:

private void db_Update_Add_Record(string sURL, string sTitle)

        {

            //--------< db_Update_Add_Record() >--------

            //*Update or add Record

            //< correct>

            sURL = sURL.Replace("'", "''");

            sTitle = sTitle.Replace("'", "''");

            //</ correct>

 

            //< find record >

            string sSQL = "SELECT TOP 1 * FROM tbl_Details WHERE [URL] Like '" + sURL + "'";

            DataTable tbl= clsDB.Get_DataTable(sSQL);

            //</ find record >

 

            if (tbl.Rows.Count==0)

            {

                //< add >

                string sql_Add = "INSERT INTO tbl_Details ([URL],[Title],[dtScan]) VALUES('" + sURL + "','" + sTitle + "',SYSDATETIME())";

                clsDB.Execute_SQL(sql_Add);

                //</ add >

            }

            else

               {

                //< update >

                string ID = tbl.Rows[0]["IDDetail"].ToString();

                string sql_Update = "UPDATE tbl_Details SET [dtScan] = SYSDATETIME() WHERE IDDetail = " + ID;

                clsDB.Execute_SQL(sql_Update);

                //</ update >

            }

            //--------</ db_Update_Add_Record() >--------

        }

 

For this you should move all database specific tasks to their own static or shared class

 

 

Code of shared class (in C # as static)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

//< add using >

using System.Data.SqlClient;    //*local DB

using System.Data;              //*ConnectionState, DataTable

//</ add using >

 

namespace WebRobot_ComputerFutures

{

    public static class clsDB

    {

        //-------------------< Class: DB >-------------------

 

        public static SqlConnection Get_DB_Connection()

        {

            //--------< db_Get_Connection() >--------

            //< db oeffnen >

            string cn_String = Properties.Settings.Default.connection_String;

            SqlConnection cn_connection = new SqlConnection(cn_String);

            if (cn_connection.State != ConnectionState.Open) cn_connection.Open();

            //</ db oeffnen >

 

            //< output >

            return cn_connection;

            //</ output >

            //--------</ db_Get_Connection() >--------

        }

 

        public static DataTable Get_DataTable(string SQL_Text)

        {

            //--------< db_Get_DataTable() >--------

            SqlConnection cn_connection = Get_DB_Connection();

 

            //< get Table >

            DataTable table = new DataTable();

            SqlDataAdapter adapter = new SqlDataAdapter(SQL_Text, cn_connection);

            adapter.Fill(table);

            //</ get Table >

 

            //< output >

            return table;

            //</ output >

            //--------</ db_Get_DataTable() >--------

        }

 

        public static void Execute_SQL(string SQL_Text)

        {

            //--------< Execute_SQL() >--------

            SqlConnection cn_connection = Get_DB_Connection();

 

            //< get Table >

            SqlCommand cmd_Command = new SqlCommand(SQL_Text, cn_connection);

            cmd_Command.ExecuteNonQuery();

            //</ get Table >

 

            //--------</ Execute_SQL() >--------

        }

 

 

 

        public static void Close_DB_Connection()

        {

            //--------< Close_DB_Connection() >--------

            //< db oeffnen >

            string cn_String = Properties.Settings.Default.connection_String;

            SqlConnection cn_connection = new SqlConnection(cn_String);

            if (cn_connection.State != ConnectionState.Closed ) cn_connection.Close ();

            //</ db oeffnen >

 

            //--------</ Close_DB_Connection() >--------

        }

        //-------------------</ Class: DB >-------------------

    }

 

 

Through the top application, as shown here, all entries of a web page are added or updated in a table.

Mobile
»
C # Application: Webrobot to read all Ebay Kleinanzeigen
»
WPF: Navigate to a page
»
WPF: create a selectable list
»
WPF: Set Startup Window
»
Add WPF Local SQL Database to Application and Connect Data
»
WPF / UWP: Canvas with relative positions and sizes
»
C #, WPF, UWP: ScreenShot to save an app or window area as a file
»
C #: Windows ScreenShot save as file
»
WPF Color Picker on Color Cube
»
WPF: Draw ColorWheel in C# by drawing Lines

.

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