добил работу с бд

This commit is contained in:
Timourka 2024-03-26 21:17:45 +04:00
parent bb20f0e17b
commit c2be31edef
6 changed files with 243 additions and 31 deletions

View File

@ -28,46 +28,90 @@
/// </summary>
private void InitializeComponent()
{
button1 = new Button();
dataGridView1 = new DataGridView();
((System.ComponentModel.ISupportInitialize)dataGridView1).BeginInit();
menuStrip1 = new MenuStrip();
tablesToolStripMenuItem = new ToolStripMenuItem();
carModelToolStripMenuItem = new ToolStripMenuItem();
carToolStripMenuItem = new ToolStripMenuItem();
branchToolStripMenuItem = new ToolStripMenuItem();
clientToolStripMenuItem = new ToolStripMenuItem();
rentalToolStripMenuItem = new ToolStripMenuItem();
menuStrip1.SuspendLayout();
SuspendLayout();
//
// button1
// menuStrip1
//
button1.Location = new Point(12, 12);
button1.Name = "button1";
button1.Size = new Size(75, 23);
button1.TabIndex = 0;
button1.Text = "button1";
button1.UseVisualStyleBackColor = true;
button1.Click += button1_Click;
menuStrip1.Items.AddRange(new ToolStripItem[] { tablesToolStripMenuItem });
menuStrip1.Location = new Point(0, 0);
menuStrip1.Name = "menuStrip1";
menuStrip1.Size = new Size(800, 24);
menuStrip1.TabIndex = 0;
menuStrip1.Text = "menuStrip1";
//
// dataGridView1
// tablesToolStripMenuItem
//
dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridView1.Location = new Point(12, 41);
dataGridView1.Name = "dataGridView1";
dataGridView1.RowTemplate.Height = 25;
dataGridView1.Size = new Size(776, 397);
dataGridView1.TabIndex = 1;
tablesToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { carModelToolStripMenuItem, carToolStripMenuItem, branchToolStripMenuItem, clientToolStripMenuItem, rentalToolStripMenuItem });
tablesToolStripMenuItem.Name = "tablesToolStripMenuItem";
tablesToolStripMenuItem.Size = new Size(50, 20);
tablesToolStripMenuItem.Text = "tables";
//
// carModelToolStripMenuItem
//
carModelToolStripMenuItem.Name = "carModelToolStripMenuItem";
carModelToolStripMenuItem.Size = new Size(180, 22);
carModelToolStripMenuItem.Text = "car model";
carModelToolStripMenuItem.Click += carModelToolStripMenuItem_Click;
//
// carToolStripMenuItem
//
carToolStripMenuItem.Name = "carToolStripMenuItem";
carToolStripMenuItem.Size = new Size(180, 22);
carToolStripMenuItem.Text = "car";
carToolStripMenuItem.Click += carToolStripMenuItem_Click;
//
// branchToolStripMenuItem
//
branchToolStripMenuItem.Name = "branchToolStripMenuItem";
branchToolStripMenuItem.Size = new Size(180, 22);
branchToolStripMenuItem.Text = "branch";
branchToolStripMenuItem.Click += branchToolStripMenuItem_Click;
//
// clientToolStripMenuItem
//
clientToolStripMenuItem.Name = "clientToolStripMenuItem";
clientToolStripMenuItem.Size = new Size(180, 22);
clientToolStripMenuItem.Text = "client";
clientToolStripMenuItem.Click += clientToolStripMenuItem_Click;
//
// rentalToolStripMenuItem
//
rentalToolStripMenuItem.Name = "rentalToolStripMenuItem";
rentalToolStripMenuItem.Size = new Size(180, 22);
rentalToolStripMenuItem.Text = "rental";
rentalToolStripMenuItem.Click += rentalToolStripMenuItem_Click;
//
// FormMain
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(dataGridView1);
Controls.Add(button1);
Controls.Add(menuStrip1);
MainMenuStrip = menuStrip1;
Name = "FormMain";
Text = "FormMain";
((System.ComponentModel.ISupportInitialize)dataGridView1).EndInit();
menuStrip1.ResumeLayout(false);
menuStrip1.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
#endregion
private Button button1;
private DataGridView dataGridView1;
private MenuStrip menuStrip1;
private ToolStripMenuItem tablesToolStripMenuItem;
private ToolStripMenuItem carModelToolStripMenuItem;
private ToolStripMenuItem carToolStripMenuItem;
private ToolStripMenuItem branchToolStripMenuItem;
private ToolStripMenuItem clientToolStripMenuItem;
private ToolStripMenuItem rentalToolStripMenuItem;
}
}

View File

@ -4,12 +4,37 @@ namespace Forms
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void carModelToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void carToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void branchToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void clientToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void rentalToolStripMenuItem_Click(object sender, EventArgs e)
{
}
/*
private Abstractions bd = new Implementation();
private void button1_Click(object sender, EventArgs e)

View File

@ -117,4 +117,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>

View File

@ -39,5 +39,19 @@
public abstract List<Branch> GetBranches();
public abstract void UpdateBranch(Branch branch);
public abstract void DeleteBranch(int id);
// Status
public abstract void AddStatus(Status status);
public abstract List<Status> GetStatuses();
public abstract Status GetStatusById(int id);
public abstract void UpdateStatus(Status status);
public abstract void DeleteStatus(int id);
// BodyType
public abstract void AddBodyType(BodyType bodyType);
public abstract List<BodyType> GetBodyTypes();
public abstract BodyType GetBodyTypeById(int id);
public abstract void UpdateBodyType(BodyType bodyType);
public abstract void DeleteBodyType(int id);
}
}

View File

@ -347,5 +347,125 @@ namespace database
using var cmd = new NpgsqlCommand($"DELETE FROM branch WHERE id = {id}", conn);
cmd.ExecuteNonQuery();
}
// Status
public override void AddStatus(Status status)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"INSERT INTO status (id, title) VALUES ({status.Id}, '{status.Title}')", conn);
cmd.ExecuteNonQuery();
}
public override List<Status> GetStatuses()
{
var statuses = new List<Status>();
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("SELECT * FROM status", conn);
using var reader = cmd.ExecuteReader();
while (reader.Read())
{
statuses.Add(new Status
{
Id = reader.GetInt32(0),
Title = reader.GetString(1)
});
}
return statuses;
}
public override Status GetStatusById(int id)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"SELECT * FROM status WHERE id = {id}", conn);
using var reader = cmd.ExecuteReader();
if (reader.Read())
{
return new Status
{
Id = reader.GetInt32(0),
Title = reader.GetString(1)
};
}
return null;
}
public override void UpdateStatus(Status status)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"UPDATE status SET title = '{status.Title}' WHERE id = {status.Id}", conn);
cmd.ExecuteNonQuery();
}
public override void DeleteStatus(int id)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"DELETE FROM status WHERE id = {id}", conn);
cmd.ExecuteNonQuery();
}
// BodyType
public override void AddBodyType(BodyType bodyType)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"INSERT INTO body_type (id, title) VALUES ({bodyType.Id}, '{bodyType.Title}')", conn);
cmd.ExecuteNonQuery();
}
public override List<BodyType> GetBodyTypes()
{
var bodyTypes = new List<BodyType>();
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("SELECT * FROM body_type", conn);
using var reader = cmd.ExecuteReader();
while (reader.Read())
{
bodyTypes.Add(new BodyType
{
Id = reader.GetInt32(0),
Title = reader.GetString(1)
});
}
return bodyTypes;
}
public override BodyType GetBodyTypeById(int id)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"SELECT * FROM body_type WHERE id = {id}", conn);
using var reader = cmd.ExecuteReader();
if (reader.Read())
{
return new BodyType
{
Id = reader.GetInt32(0),
Title = reader.GetString(1)
};
}
return null;
}
public override void UpdateBodyType(BodyType bodyType)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"UPDATE body_type SET title = '{bodyType.Title}' WHERE id = {bodyType.Id}", conn);
cmd.ExecuteNonQuery();
}
public override void DeleteBodyType(int id)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"DELETE FROM body_type WHERE id = {id}", conn);
cmd.ExecuteNonQuery();
}
}
}

View File

@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace database
namespace database
{
// Определение моделей данных
@ -56,4 +50,16 @@ namespace database
public string Phone { get; set; }
public string WorkingHours { get; set; }
}
public class Status
{
public int Id { get; set; }
public string Title { get; set; }
}
public class BodyType
{
public int Id { get; set; }
public string Title { get; set; }
}
}