поменяла класс реализацию для специализации на использование параметров в запросах. и формы для специализации донастроила вроде... только айдишники не перезаписываются (удалила 4 айдишник - следующим добавилась специализация с айдишником 5)

This commit is contained in:
Елена Бакальская 2024-05-07 21:17:20 +04:00
parent 735d10a4d6
commit c612091f47
4 changed files with 163 additions and 40 deletions

View File

@ -28,12 +28,57 @@
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Text = "FormSpecialisation";
buttonSave = new Button();
textBoxNameSpecialisation = new TextBox();
buttonCancel = new Button();
SuspendLayout();
//
// buttonSave
//
buttonSave.Location = new Point(37, 86);
buttonSave.Name = "buttonSave";
buttonSave.Size = new Size(133, 44);
buttonSave.TabIndex = 0;
buttonSave.Text = "Сохранить";
buttonSave.UseVisualStyleBackColor = true;
buttonSave.Click += buttonSave_Click;
//
// textBoxNameSpecialisation
//
textBoxNameSpecialisation.Location = new Point(37, 33);
textBoxNameSpecialisation.Name = "textBoxNameSpecialisation";
textBoxNameSpecialisation.Size = new Size(274, 27);
textBoxNameSpecialisation.TabIndex = 1;
//
// buttonCancel
//
buttonCancel.Location = new Point(178, 86);
buttonCancel.Name = "buttonCancel";
buttonCancel.Size = new Size(133, 44);
buttonCancel.TabIndex = 2;
buttonCancel.Text = "Отмена";
buttonCancel.UseVisualStyleBackColor = true;
buttonCancel.Click += buttonCancel_Click;
//
// FormSpecialisation
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(345, 170);
Controls.Add(buttonCancel);
Controls.Add(textBoxNameSpecialisation);
Controls.Add(buttonSave);
Name = "FormSpecialisation";
Text = "Специализация";
Load += FormSpecialisation_Load;
ResumeLayout(false);
PerformLayout();
}
#endregion
private Button buttonSave;
private TextBox textBoxNameSpecialisation;
private Button buttonCancel;
}
}

View File

@ -1,4 +1,6 @@
using System;
using BeautySalonDBModels;
using BeautySalonDBModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
@ -12,9 +14,77 @@ namespace BeautySalon
{
public partial class FormSpecialisation : Form
{
public FormSpecialisation()
private readonly AbstractWorkWithStorage<Specialisation> storage;
private int? _id;
public int Id
{
set { _id = value; }
}
public FormSpecialisation(AbstractWorkWithStorage<Specialisation> storage)
{
this.storage = storage;
InitializeComponent();
}
private void buttonSave_Click(object sender, EventArgs e)
{
try
{
if (_id != null)
{
var newSpecialisation = new Specialisation
{
SpecialisationId = _id ?? 0,
Name = textBoxNameSpecialisation.Text,
};
storage.Update(newSpecialisation);
}
else
{
var specialisation = new Specialisation
{
Name = textBoxNameSpecialisation.Text,
};
storage.Add(specialisation);
}
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel; Close();
}
private void FormSpecialisation_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
var specialisationObject = storage.GetObject(_id.Value);
if (specialisationObject != null)
{
textBoxNameSpecialisation.Text = specialisationObject.Name;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}
}

View File

@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->

View File

@ -14,18 +14,20 @@ namespace BeautySalonDBModels.Implements
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("INSERT INTO specialisations (specialisation_name) " +
"VALUES (@Name)", conn);
using var cmd = new NpgsqlCommand("INSERT INTO specialisations (specialisation_id, specialisation_name) VALUES (nextval('seq_specialisation'), @Name)", conn);
cmd.Parameters.AddWithValue("@Name", specialisation.Name);
cmd.ExecuteNonQuery();
}
public override Specialisation? GetObject(int id)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("SELECT * FROM specialisations WHERE specialisation_id = {id}", conn);
using var cmd = new NpgsqlCommand("SELECT * FROM specialisations WHERE specialisation_id = @id", conn);
cmd.Parameters.AddWithValue("@id", id);
using var reader = cmd.ExecuteReader();
if (reader.Read())
{
return new Specialisation
@ -58,10 +60,14 @@ namespace BeautySalonDBModels.Implements
public override Specialisation? Remove(int id)
{
var specialisation = GetObject(id);
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("delete from specialisations where specialisation_id = {id}", conn);
cmd.ExecuteNonQuery();
if (specialisation != null)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("delete from specialisations where specialisation_id = @SpecialisationId", conn);
cmd.Parameters.AddWithValue("@SpecialisationId", id);
cmd.ExecuteNonQuery();
}
return specialisation;
}
@ -69,9 +75,11 @@ namespace BeautySalonDBModels.Implements
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("update specialisation set specialisation_name = {specialisation.Name}, " +
"where specialisation_id = {specialisation.SpecialisationId}", conn);
using var cmd = new NpgsqlCommand("UPDATE specialisations SET specialisation_name = @Name WHERE specialisation_id = @SpecialisationId", conn);
cmd.Parameters.AddWithValue("@Name", specialisation.Name);
cmd.Parameters.AddWithValue("@SpecialisationId", specialisation.SpecialisationId);
cmd.ExecuteNonQuery();
}
}
}