codedocu.de

Code zum Video-Tutorial Beispiel:

 

#CRUD Beispiel, Anzeigen, Anfügen, Löschen und Update von Daten in einer SQL Datenbank in Winforms, WPF

 

 

Beispiel Winform,  einfache lokale Datenbank

Winforms: Lokale Datenbank, erstellen, anbinden, anzeigen, Insert Update, Delete

 

 

Video Tutorial unter Youtube

 

Local Datenbank in SQL Server

Die BeispielDaten wurden in einer kleine SQL Datenbank erstellte

Winforms: Lokale Datenbank, erstellen, anbinden, anzeigen, Insert Update, Delete

 

Mit der Tabellenstruktur in SQL Server local

CREATE TABLE [dbo].[tbl_Autos] (

    [IDAuto] INT            IDENTITY (1, 1) NOT NULL,

    [Auto]   NVARCHAR (255) NULL,

    PRIMARY KEY CLUSTERED ([IDAuto] ASC)

);

 

 

 

C# Code zum Beispiel in Video Tutorial

In Visual Studio

Code zu Form1.cs

Winforms: Lokale Datenbank, erstellen, anbinden, anzeigen, Insert Update, Delete

 

Kompletter C# Code unter Form1.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace dbTest_02

{

    public partial class Form1 : Form

    {

 

        #region Form

        public Form1()

        {

            InitializeComponent();

        }

 

 

        private void Form1_Load(object sender, EventArgs e)

        {

            liste_laden();

        }

        #endregion /Form

 

 

        #region  Buttons 

        //---------------------------< #region Buttons >---------------------------

        private void btnAdd_Click(object sender, EventArgs e)

        {

            eintrag_anfuegen();

        }

        private void btnDelete_Click(object sender, EventArgs e)

        {

            eintrag_loeschen();

        }

        private void btnUpdate_Click(object sender, EventArgs e)

        {

            eintrag_Updaten();

        }

        //---------------------------</ #region Buttons >---------------------------

        #endregion /Buttons

 

        #region  Controls 

        //---------------------------< #region Controls >---------------------------

        private void lstAutos_SelectedIndexChanged(object sender, EventArgs e)

        {

            DataRowView row = lstAutos.SelectedItem as DataRowView;

            string sText = row["Auto"].ToString();

 

            tbxEingabe.Text = sText;

            //MessageBox.Show(sText);

            //---------------------------< #region Controls >---------------------------

        }

        #endregion /Controls

 

 

 

        #region Methods

        //---------------------------< #region Methods >---------------------------

        private void liste_laden()

        {

            //--------------< liste_laden() >--------------

            //< Daten >

            string sql_Text = "SELECT * FROM tbl_Autos";

            DataTable tblData =  db_get_Table(sql_Text);

            //</ Daten >

 

            //-------< Anzeigen >-------

            lstAutos.DisplayMember = "Auto";

            lstAutos.ValueMember = "[IDAuto]";

 

            lstAutos.DataSource = tblData;

            //-------</ Anzeigen >-------

            //--------------</ liste_laden() >--------------

        }

 

        private void eintrag_anfuegen()

        {

            //--------------< liste_laden() >--------------

            //< Daten >

 

            string sNeuer_Wert = tbxEingabe.Text;

 

            string sql_Text = "INSERT INTO tbl_Autos ([Auto]) VALUES('" + sNeuer_Wert + "')";

            db_execute(sql_Text);

 

            liste_laden();

            //</ Daten >

            //--------------</ liste_laden() >--------------

        }

 

        private void eintrag_loeschen()

        {

            //--------------< liste_laden() >--------------

            //< Daten >

 

            DataRowView row = lstAutos.SelectedItem as DataRowView;

            string IDAuto = row["IDAuto"].ToString();

            string sql_Text = "DELETE FROM tbl_Autos WHERE(IDAuto = " + IDAuto + ")";

 

            db_execute(sql_Text);

 

            liste_laden();

            //</ Daten >

 

            //--------------</ liste_laden() >--------------

        }

 

        private void eintrag_Updaten()

        {

            //--------------< liste_laden() >--------------

            //< Daten >

            DataRowView row = lstAutos.SelectedItem as DataRowView;

            string IDAuto = row["IDAuto"].ToString();

            string sql_Text = "UPDATE tbl_Autos SET [Auto] = '" + tbxEingabe.Text + "' WHERE IDAuto = " + IDAuto;

            db_execute(sql_Text);

 

            liste_laden();

            //</ Daten >

 

            //--------------</ liste_laden() >--------------

        }

        //---------------------------</ #region Methods >---------------------------

        #endregion /Methods

 

 

 

 

 

 

        #region Methods: Database

        //---------------------------< #region Methods: Database >---------------------------

        private DataTable db_get_Table(string sql_Text)

        {

            //--------------------< db_get_Table() >---------------

            //*Anzeigen von Daten

            //< init >

            //via app-Settings

            string cn_string = Properties.Settings.Default.app_ConnectionString;

            //</ init >

 

            SqlConnection cn = new SqlConnection(cn_string);

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

            SqlDataAdapter sql_adapt = new SqlDataAdapter(sql_Text, cn);

 

            DataTable tblData = new DataTable();

            sql_adapt.Fill(tblData);

            cn.Close();

            return tblData;

            //--------------------</ db_get_Table() >---------------

        }

 

        private int db_execute(string sql_Text)

        {

            //--------------------< db_get_Table() >---------------

            //*Ausfuehren von SQL Befehlen wie INSERT, DELETE, UPDATE

            //< init >

            //via app-Settings

            string cn_string = Properties.Settings.Default.app_ConnectionString;

            //</ init >

 

            SqlConnection cn = new SqlConnection(cn_string);

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

 

            SqlCommand cmd = new SqlCommand(sql_Text, cn);

            int intResult = cmd.ExecuteNonQuery();

            cn.Close();

 

            return intResult;           

            //--------------------</ db_get_Table() >---------------

        }

        //---------------------------</ #region Methods: Database >---------------------------

        #endregion /Methods: Database

 

 

 

 

    }

}

 

 

 

Connectionstring

Die Connection ist unter app.config

Winforms: Lokale Datenbank, erstellen, anbinden, anzeigen, Insert Update, Delete

 

XML Code unter app.config

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

    <configSections>

    </configSections>

    <connectionStrings>

        <add name="dbTest_02.Properties.Settings.app_ConnectionString"

            connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\dbAutos.mdf;Integrated Security=True"

            providerName="System.Data.SqlClient" />

    </connectionStrings>

    <startup>

        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />

    </startup>

</configuration>

 

<!--|DataDirectory|-->

 

 

 

Form1

Form1.Designer Code

namespace dbTest_02

{

    partial class Form1

    {

        /// <summary>

        /// Required designer variable.

        /// </summary>

        private System.ComponentModel.IContainer components = null;

 

        /// <summary>

        /// Clean up any resources being used.

        /// </summary>

        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }

 

        #region Windows Form Designer generated code

 

        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()

        {

            this.lblListe = new System.Windows.Forms.Label();

            this.lstAutos = new System.Windows.Forms.ListBox();

            this.tbxEingabe = new System.Windows.Forms.TextBox();

            this.btnAdd = new System.Windows.Forms.Button();

            this.btnDelete = new System.Windows.Forms.Button();

            this.btnUpdate = new System.Windows.Forms.Button();

            this.SuspendLayout();

            //

            // lblListe

            //

            this.lblListe.AutoSize = true;

            this.lblListe.Location = new System.Drawing.Point(27, 22);

            this.lblListe.Name = "lblListe";

            this.lblListe.Size = new System.Drawing.Size(44, 17);

            this.lblListe.TabIndex = 0;

            this.lblListe.Text = "Autos";

            //

            // lstAutos

            //

            this.lstAutos.FormattingEnabled = true;

            this.lstAutos.ItemHeight = 16;

            this.lstAutos.Location = new System.Drawing.Point(27, 42);

            this.lstAutos.Name = "lstAutos";

            this.lstAutos.Size = new System.Drawing.Size(168, 244);

            this.lstAutos.TabIndex = 1;

            this.lstAutos.SelectedIndexChanged += new System.EventHandler(this.lstAutos_SelectedIndexChanged);

            //

            // tbxEingabe

            //

            this.tbxEingabe.Location = new System.Drawing.Point(222, 39);

            this.tbxEingabe.Name = "tbxEingabe";

            this.tbxEingabe.Size = new System.Drawing.Size(131, 22);

            this.tbxEingabe.TabIndex = 2;

            //

            // btnAdd

            //

            this.btnAdd.Location = new System.Drawing.Point(222, 74);

            this.btnAdd.Name = "btnAdd";

            this.btnAdd.Size = new System.Drawing.Size(112, 38);

            this.btnAdd.TabIndex = 3;

            this.btnAdd.Text = "Add";

            this.btnAdd.UseVisualStyleBackColor = true;

            this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);

            //

            // btnDelete

            //

            this.btnDelete.Location = new System.Drawing.Point(222, 118);

            this.btnDelete.Name = "btnDelete";

            this.btnDelete.Size = new System.Drawing.Size(112, 38);

            this.btnDelete.TabIndex = 3;

            this.btnDelete.Text = "Delete";

            this.btnDelete.UseVisualStyleBackColor = true;

            this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);

            //

            // btnUpdate

            //

            this.btnUpdate.Location = new System.Drawing.Point(222, 162);

            this.btnUpdate.Name = "btnUpdate";

            this.btnUpdate.Size = new System.Drawing.Size(112, 38);

            this.btnUpdate.TabIndex = 3;

            this.btnUpdate.Text = "Update";

            this.btnUpdate.UseVisualStyleBackColor = true;

            this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);

            //

            // Form1

            //

            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.ClientSize = new System.Drawing.Size(387, 337);

            this.Controls.Add(this.btnUpdate);

            this.Controls.Add(this.btnDelete);

            this.Controls.Add(this.btnAdd);

            this.Controls.Add(this.tbxEingabe);

            this.Controls.Add(this.lstAutos);

            this.Controls.Add(this.lblListe);

            this.Name = "Form1";

            this.Text = "Form1";

            this.Load += new System.EventHandler(this.Form1_Load);

            this.ResumeLayout(false);

            this.PerformLayout();

 

        }

 

        #endregion

 

        private System.Windows.Forms.Label lblListe;

        private System.Windows.Forms.ListBox lstAutos;

        private System.Windows.Forms.TextBox tbxEingabe;

        private System.Windows.Forms.Button btnAdd;

        private System.Windows.Forms.Button btnDelete;

        private System.Windows.Forms.Button btnUpdate;

    }

}

 

 

 


Software Entwicklung Stuttgart Nürtingen
Suche Projekte C#, WPF, Windows App,ASP.Net, vb.Net, WinForms, SQL Server, Access, Excel