работа с бд

This commit is contained in:
AnnaLioness 2024-03-27 16:18:02 +04:00
parent 8c6c68ae2c
commit 31a881056d
10 changed files with 649 additions and 0 deletions

View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33122.133
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExamTimetable", "ExamTimetable\ExamTimetable.csproj", "{073BFD8D-BE81-4F92-963C-F36385B2337C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{073BFD8D-BE81-4F92-963C-F36385B2337C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{073BFD8D-BE81-4F92-963C-F36385B2337C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{073BFD8D-BE81-4F92-963C-F36385B2337C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{073BFD8D-BE81-4F92-963C-F36385B2337C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F3EC9A9E-1007-43F2-BC85-C78B6E031DC8}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,39 @@
namespace ExamTimetable
{
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.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Text = "Form1";
}
#endregion
}
}

View File

@ -0,0 +1,10 @@
namespace ExamTimetable
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}

View File

@ -0,0 +1,120 @@
<?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
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<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
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
mimetype set.
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
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
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
: 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
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -0,0 +1,17 @@
namespace ExamTimetable
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
}
}
}

View File

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExamTimetable_Database
{
public abstract class Abstractions
{
//university
public abstract void AddUniversity(University university);
public abstract List<University> GetUniversities();
public abstract University GetUniversityById(int id);
public abstract void UpdateUniversity(University university);
public abstract void DeleteUniversity(int id);
//Faculty
public abstract void AddFaculty(Faculty faculty);
public abstract List<Faculty> GetFaculties();
public abstract Faculty GetFacultyById(int id);
public abstract void UpdateFaculty(Faculty faculty);
public abstract void DeleteFaculty(int id);
//Specialization
public abstract void AddSpecialization(Specialization specialization);
public abstract List<Specialization> GetSpecializations();
public abstract Specialization GetSpecializationById(int id);
public abstract void UpdateSpecialization(Specialization specialization);
public abstract void DeleteSpecialization(int id);
//Comission
public abstract void AddComission(Comission comission);
public abstract List<Comission> GetComissions();
public abstract Comission GetComissionById(int id);
public abstract void UpdateComission(Comission comission);
public abstract void DeleteComission(int id);
//Exam
public abstract void AddExam(Exam exam);
public abstract List<Exam> GetExams();
public abstract Exam GetExamById(int id);
public abstract void UpdateExam(Exam exam);
public abstract void DeleteExam(int id);
}
}

View File

@ -0,0 +1,39 @@

namespace ExamTimetable_Database
{
public class University
{
public int univer_id { get; set; }
public string univer_name { get; set; }
public string univer_adress { get; set; }
public string phone_number { get; set; }
public string email_addr { get; set; }
}
public class Faculty
{
public int faculty_id { get; set; }
public string faculty_name { get; set; }
public int uni_id { get; set; }
}
public class Specialization
{
public int spec_id { get; set; }
public string spec_name { get; set; }
public int fuc_id { get; set; }
}
public class Comission
{
public int comm_id { get; set; }
public string teaching_stuff { get; set; }
public int fu_id { get; set; }
}
public class Exam
{
public int exam_id { get; set; }
public DateTime exam_date{ get; set; }
public string exam_place { get; set; }
public string exam_subject { get; set; }
public int com_id { get; set; }
public DateTime exam_time { get; set; }
}
}

View File

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,333 @@
using Npgsql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
namespace ExamTimetable_Database
{
public class Implementation : Abstractions
{
private NpgsqlConnection GetConnection()
{
return new NpgsqlConnection("Host=127.0.0.1;Port=5555;Username=anna;Database=subd_anna;");
}
//university
public override void AddUniversity(University university)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"INSERT INTO university (univer_id, univer_name, univer_adress, phone_number, email_addr) VALUES (@univer_id, @univer_name ,@univer_adress, @phone_number, @email_addr)", conn);
cmd.Parameters.AddWithValue("@univer_id", university.univer_id);
cmd.Parameters.AddWithValue("@univer_name", university.univer_name);
cmd.Parameters.AddWithValue("@univer_adress", university.univer_adress);
cmd.Parameters.AddWithValue("@phone_number", university.phone_number);
cmd.Parameters.AddWithValue("@email_addr", university.email_addr);
cmd.ExecuteNonQuery();
}
public override List<University> GetUniversities()
{
var universities = new List<University>();
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("SELECT * FROM university", conn);
using var reader = cmd.ExecuteReader();
while (reader.Read())
{
universities.Add(new University
{
univer_id = reader.GetInt32(0),
univer_name = reader.GetString(1),
univer_adress = reader.GetString(2),
phone_number = reader.GetString(3),
email_addr = reader.GetString(4)
});
}
return universities;
}
public override University GetUniversityById(int id)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"SELECT * FROM university WHERE univer_id = {id}", conn);
using var reader = cmd.ExecuteReader();
if (reader.Read())
{
return new University
{
univer_id = reader.GetInt32(0),
univer_name = reader.GetString(1),
univer_adress = reader.GetString(2),
phone_number = reader.GetString(3),
email_addr = reader.GetString(4)
};
}
return null;
}
public override void UpdateUniversity(University university)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"UPDATE university SET univer_name = {university.univer_name}, univer_adress = {university.univer_adress}, phone_number = {university.phone_number}, email_addr = {university.email_addr} WHERE univer_id = {university.univer_id}", conn);
cmd.ExecuteNonQuery();
}
public override void DeleteUniversity(int id)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"DELETE FROM university WHERE univer_id = {id}", conn);
cmd.ExecuteNonQuery();
}
//Faculty
public override void AddFaculty(Faculty faculty)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"INSERT INTO faculty (faculty_id, faculty_name, uni_id) VALUES (@faculty_id, @faculty_name, @uni_id)", conn);
cmd.Parameters.AddWithValue("@faculty_id", faculty.faculty_id);
cmd.Parameters.AddWithValue("@faculty_name", faculty.faculty_name);
cmd.Parameters.AddWithValue("@uni_id", faculty.uni_id);
cmd.ExecuteNonQuery();
}
public override List<Faculty> GetFaculties()
{
var faculties = new List<Faculty>();
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("SELECT * FROM faculty", conn);
using var reader = cmd.ExecuteReader();
while (reader.Read())
{
faculties.Add(new Faculty
{
faculty_id = reader.GetInt32(0),
faculty_name = reader.GetString(1),
uni_id = reader.GetInt32(2)
});
}
return faculties;
}
public override Faculty GetFacultyById(int id)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"SELECT * FROM faculty WHERE faculty_id = {id}", conn);
using var reader = cmd.ExecuteReader();
if (reader.Read())
{
return new Faculty
{
faculty_id = reader.GetInt32(0),
faculty_name = reader.GetString(1),
uni_id = reader.GetInt32(2)
};
}
return null;
}
public override void UpdateFaculty(Faculty faculty)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"UPDATE faculty SET faculty_name = {faculty.faculty_name}, uni_id = {faculty.uni_id} WHERE faculty_id = {faculty.faculty_id}", conn);
cmd.ExecuteNonQuery();
}
public override void DeleteFaculty(int id)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"DELETE FROM faculty WHERE faculty_id = {id}", conn);
cmd.ExecuteNonQuery();
}
//Specialization
public override void AddSpecialization(Specialization specialization)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"INSERT INTO specialization (spec_id, spec_name, fuc_id) VALUES (@spec_id, @spec_name, @fuc_id)", conn);
cmd.Parameters.AddWithValue("@spec_id", specialization.spec_id);
cmd.Parameters.AddWithValue("@spec_name", specialization.spec_name);
cmd.Parameters.AddWithValue("@fuc_id", specialization.fuc_id);
cmd.ExecuteNonQuery();
}
public override List<Specialization> GetSpecializations()
{
var specializations = new List<Specialization>();
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("SELECT * FROM specialization", conn);
using var reader = cmd.ExecuteReader();
while (reader.Read())
{
specializations.Add(new Specialization
{
spec_id = reader.GetInt32(0),
spec_name = reader.GetString(1),
fuc_id = reader.GetInt32(2)
});
}
return specializations;
}
public override Specialization GetSpecializationById(int id)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"SELECT * FROM specialization WHERE spec_id = {id}", conn);
using var reader = cmd.ExecuteReader();
if (reader.Read())
{
return new Specialization
{
spec_id = reader.GetInt32(0),
spec_name = reader.GetString(1),
fuc_id = reader.GetInt32(2)
};
}
return null;
}
public override void UpdateSpecialization(Specialization specialization)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"UPDATE specialization SET spec_name = {specialization.spec_name}, fuc_id = {specialization.fuc_id} WHERE spec_id = {specialization.spec_id}", conn);
cmd.ExecuteNonQuery();
}
public override void DeleteSpecialization(int id)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"DELETE FROM specialization WHERE spec_id = {id}", conn);
cmd.ExecuteNonQuery();
}
//Comission
public override void AddComission(Comission comission)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"INSERT INTO comission (comm_id, teaching_stuff, fu_id) VALUES (@comm_id, @teaching_stuff, @fu_id)", conn);
cmd.Parameters.AddWithValue("@comm_id", comission.comm_id);
cmd.Parameters.AddWithValue("@teaching_stuff", comission.teaching_stuff);
cmd.Parameters.AddWithValue("@fu_id", comission.fu_id);
cmd.ExecuteNonQuery();
}
public override List<Comission> GetComissions()
{
var comissions = new List<Comission>();
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("SELECT * FROM comission", conn);
using var reader = cmd.ExecuteReader();
while (reader.Read())
{
comissions.Add(new Comission
{
comm_id = reader.GetInt32(0),
teaching_stuff = reader.GetString(1),
fu_id = reader.GetInt32(2)
});
}
return comissions;
}
public override Comission GetComissionById(int id)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"SELECT * FROM comission WHERE comm_id = {id}", conn);
using var reader = cmd.ExecuteReader();
if (reader.Read())
{
return new Comission
{
comm_id = reader.GetInt32(0),
teaching_stuff = reader.GetString(1),
fu_id = reader.GetInt32(2)
};
}
return null;
}
public override void UpdateComission(Comission comission)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"UPDATE comission SET teaching_stuff = {comission.teaching_stuff}, fu_id = {comission.fu_id} WHERE comm_id = {comission.comm_id}", conn);
cmd.ExecuteNonQuery();
}
public override void DeleteComission(int id)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"DELETE FROM comission WHERE comm_id = {id}", conn);
cmd.ExecuteNonQuery();
}
//Exam
public override void AddExam(Exam exam)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"INSERT INTO exam (exam_id, exam_date, exam_place, exam_subject, com_id, exam_time) VALUES (@exam_id, @exam_date, @exam_place, @exam_subject, @com_id, @exam_time)", conn);
cmd.Parameters.AddWithValue("@exam_id", exam.exam_id);
cmd.Parameters.AddWithValue("@exam_date", exam.exam_date);
cmd.Parameters.AddWithValue("@exam_place", exam.exam_place);
cmd.Parameters.AddWithValue("@exam_subject", exam.exam_subject);
cmd.Parameters.AddWithValue("@com_id", exam.com_id);
cmd.Parameters.AddWithValue("@exam_time", exam.exam_time);
cmd.ExecuteNonQuery();
}
public override List<Exam> GetExams()
{
var exams = new List<Exam>();
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("SELECT * FROM exam", conn);
using var reader = cmd.ExecuteReader();
while (reader.Read())
{
exams.Add(new Exam
{
exam_id = reader.GetInt32(0),
exam_date = reader.GetDateTime(1),
exam_place = reader.GetString(2),
exam_subject = reader.GetString(3),
com_id = reader.GetInt32(4),
exam_time = reader.GetDateTime(5)
});
}
return exams;
}
public override Exam GetExamById(int id)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"SELECT * FROM exam WHERE exam_id = {id}", conn);
using var reader = cmd.ExecuteReader();
if (reader.Read())
{
return new Exam
{
exam_id = reader.GetInt32(0),
exam_date = reader.GetDateTime(1),
exam_place = reader.GetString(2),
exam_subject = reader.GetString(3),
com_id = reader.GetInt32(4),
exam_time = reader.GetDateTime(5)
};
}
return null;
}
public override void UpdateExam(Exam exam)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"UPDATE exam SET exam_date = {exam.exam_date}, exam_place = {exam.exam_place}, exam_subject = {exam.exam_subject}, com_id = {exam.com_id}, exam_time= {exam.exam_time} WHERE exam_id = {exam.exam_id}", conn);
cmd.ExecuteNonQuery();
}
public override void DeleteExam(int id)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand($"DELETE FROM exam WHERE exam_id = {id}", conn);
cmd.ExecuteNonQuery();
}
}
}