From 37474430c698d30c9e825c02de63fb742d828045 Mon Sep 17 00:00:00 2001 From: Felix Hartmann Date: Wed, 27 Apr 2022 11:44:49 +0200 Subject: [PATCH] add inital idea, not working! entityframeworkgit add ./git add ./ ahaaaaaha --- Database.cs | 27 ++++++++++++ Form1.Designer.cs | 107 +++++++++++++++++++++++++++++++++++++++++++++- Form1.cs | 61 +++++++++++++++++++++++++- Form1.resx | 62 +-------------------------- Person.cs | 16 +++++++ README.md | 2 +- it-projekt.csproj | 5 +++ 7 files changed, 216 insertions(+), 64 deletions(-) create mode 100644 Database.cs create mode 100644 Person.cs diff --git a/Database.cs b/Database.cs new file mode 100644 index 0000000..f901b71 --- /dev/null +++ b/Database.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Data.Common; +using System.Data.Entity; + +namespace it_projekt +{ + class Database : DbContext + { + public DbSet persons { get; set; } + + public Database() + : base() + { + + + } + public Database(DbConnection existingConnection, bool contextOwnsConnection) + : base(existingConnection, contextOwnsConnection) + { + + } + } +} diff --git a/Form1.Designer.cs b/Form1.Designer.cs index d0a4b03..0f6f0eb 100644 --- a/Form1.Designer.cs +++ b/Form1.Designer.cs @@ -29,13 +29,118 @@ namespace it_projekt /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); + this.loginBtn = new System.Windows.Forms.Button(); + this.searchBtn = new System.Windows.Forms.Button(); + this.label1 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.userTxt = new System.Windows.Forms.TextBox(); + this.passTxt = new System.Windows.Forms.TextBox(); + this.sqlTxt = new System.Windows.Forms.TextBox(); + this.dataGrid = new System.Windows.Forms.DataGridView(); + ((System.ComponentModel.ISupportInitialize)(this.dataGrid)).BeginInit(); + this.SuspendLayout(); + // + // loginBtn + // + this.loginBtn.Location = new System.Drawing.Point(265, 184); + this.loginBtn.Name = "loginBtn"; + this.loginBtn.Size = new System.Drawing.Size(75, 23); + this.loginBtn.TabIndex = 0; + this.loginBtn.Text = "Login"; + this.loginBtn.UseVisualStyleBackColor = true; + this.loginBtn.Click += new System.EventHandler(this.loginBtn_Click); + // + // searchBtn + // + this.searchBtn.Location = new System.Drawing.Point(708, 184); + this.searchBtn.Name = "searchBtn"; + this.searchBtn.Size = new System.Drawing.Size(75, 23); + this.searchBtn.TabIndex = 1; + this.searchBtn.Text = "Suchen!"; + this.searchBtn.UseVisualStyleBackColor = true; + this.searchBtn.Click += new System.EventHandler(this.searchBtn_Click); + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(31, 58); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(83, 15); + this.label1.TabIndex = 2; + this.label1.Text = "Benutzername"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(31, 84); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(54, 15); + this.label2.TabIndex = 3; + this.label2.Text = "Passwort"; + // + // userTxt + // + this.userTxt.Location = new System.Drawing.Point(120, 55); + this.userTxt.Name = "userTxt"; + this.userTxt.Size = new System.Drawing.Size(100, 23); + this.userTxt.TabIndex = 4; + // + // passTxt + // + this.passTxt.Location = new System.Drawing.Point(120, 80); + this.passTxt.Name = "passTxt"; + this.passTxt.Size = new System.Drawing.Size(100, 23); + this.passTxt.TabIndex = 5; + // + // sqlTxt + // + this.sqlTxt.Location = new System.Drawing.Point(358, 12); + this.sqlTxt.Multiline = true; + this.sqlTxt.Name = "sqlTxt"; + this.sqlTxt.Size = new System.Drawing.Size(335, 195); + this.sqlTxt.TabIndex = 6; + // + // dataGrid + // + this.dataGrid.AllowUserToOrderColumns = true; + this.dataGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGrid.Location = new System.Drawing.Point(12, 213); + this.dataGrid.Name = "dataGrid"; + this.dataGrid.RowTemplate.Height = 25; + this.dataGrid.Size = new System.Drawing.Size(771, 225); + this.dataGrid.TabIndex = 7; + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.dataGrid); + this.Controls.Add(this.sqlTxt); + this.Controls.Add(this.passTxt); + this.Controls.Add(this.userTxt); + this.Controls.Add(this.label2); + this.Controls.Add(this.label1); + this.Controls.Add(this.searchBtn); + this.Controls.Add(this.loginBtn); + this.Name = "Form1"; this.Text = "Form1"; + ((System.ComponentModel.ISupportInitialize)(this.dataGrid)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + } #endregion + + private System.Windows.Forms.Button loginBtn; + private System.Windows.Forms.Button searchBtn; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.TextBox userTxt; + private System.Windows.Forms.TextBox passTxt; + private System.Windows.Forms.TextBox sqlTxt; + private System.Windows.Forms.DataGridView dataGrid; } } diff --git a/Form1.cs b/Form1.cs index 3fc1fcc..1662c79 100644 --- a/Form1.cs +++ b/Form1.cs @@ -1,4 +1,5 @@ -using System; +using MySql.Data.MySqlClient; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -8,6 +9,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; + namespace it_projekt { public partial class Form1 : Form @@ -17,5 +19,62 @@ namespace it_projekt InitializeComponent(); } + + private void searchBtn_Click(object sender, EventArgs e) + { + + } + + private void loginBtn_Click(object sender, EventArgs e) + { + LoadUsersFromTable("",""); + } + + + + public static void LoadUsersFromTable(string user, string password) + { + string connectionString = "server=localhost;port=3306;database=stammdaten;User Id=" + user + ";Password=" + password + ";"; + + using (MySqlConnection connection = new MySqlConnection(connectionString)) + { + // Create database if not exists + using (Database contextDB = new Database(connection, false)) + { + contextDB.Database.CreateIfNotExists(); + } + + connection.Open(); + MySqlTransaction transaction = connection.BeginTransaction(); + + try + { + // DbConnection that is already opened + using (Database context = new Database(connection, false)) + { + + // Interception/SQL logging + context.Database.Log = (string message) => { Console.WriteLine(message); }; + + // Passing an existing transaction to the context + context.Database.UseTransaction(transaction); + + // DbSet.AddRange + List persons = new List(); + persons = context.persons.ToList(); + + } + + transaction.Commit(); + } + catch + { + transaction.Rollback(); + throw; + } + } + } + + } } diff --git a/Form1.resx b/Form1.resx index 1af7de1..f298a7b 100644 --- a/Form1.resx +++ b/Form1.resx @@ -1,64 +1,4 @@ - - - + diff --git a/Person.cs b/Person.cs new file mode 100644 index 0000000..34bc9da --- /dev/null +++ b/Person.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace it_projekt +{ + class Person + { + public int Id { get; set; } + public string Lastname { get; set; } + public string Firstname { get; set; } + public DateTime CreationDate { get; set; } + } +} diff --git a/README.md b/README.md index fa4d424..62a2da2 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # it-projekt -SUPER PROJEKT, ICH SAGS EUCH \ No newline at end of file +SUPER PROJEKT, ICH SAGS EUCH, schwöre! \ No newline at end of file diff --git a/it-projekt.csproj b/it-projekt.csproj index b1b8bef..b5e4aec 100644 --- a/it-projekt.csproj +++ b/it-projekt.csproj @@ -7,4 +7,9 @@ true + + + + + \ No newline at end of file