Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- программа
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace LR3
- {
- static class Program
- {
- /// <summary>
- /// Главная точка входа для приложения.
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new MainForm());
- }
- }
- }
- основное окно
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using LR3.Controller;
- namespace LR3
- {
- public partial class MainForm : Form
- {
- Query controller;
- public MainForm()
- {
- InitializeComponent();
- controller = new Query(ConnectionString.ConnStr);
- DataGrid.DataSource = controller.UpdateOperation();
- }
- private void UpdateButton_Click(object sender, EventArgs e)
- {
- DataGrid.DataSource = controller.UpdateOperation();
- }
- private void AddButton_Click(object sender, EventArgs e)
- {
- Form dialog_window = new AddNewForm();
- dialog_window.ShowDialog();
- DataGrid.DataSource = controller.UpdateOperation();
- }
- private void DeleteButton_Click(object sender, EventArgs e)
- {
- controller.Delete(int.Parse(DataGrid.Rows[DataGrid.CurrentRow.Index].Cells["ID"].Value.ToString()));
- DataGrid.DataSource = controller.UpdateOperation();
- }
- private void GenerateReportButton_Click(object sender, EventArgs e)
- {
- Form newForm = new GenerateReportForm();
- newForm.Show();
- }
- }
- }
- форма добавления операции
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using LR3.Controller;
- namespace LR3
- {
- public partial class AddNewForm : Form
- {
- bool CategoryTextBoxClicked = false;
- bool TypeTextBoxClicked = false;
- bool SumTextBoxClicked = false;
- bool OpDateTimeTextBoxClicked = false;
- bool ComissionTextBoxClicked = false;
- Query controller;
- public AddNewForm()
- {
- InitializeComponent();
- controller = new Query(ConnectionString.ConnStr);
- }
- private void AddButton_Click(object sender, EventArgs e)
- {
- controller.Add(CategoryTextBox.Text,
- TypeTextBox.Text,
- decimal.Parse(SumTextBox.Text),
- DateTime.Parse(OpDateTimeTextBox.Text),
- decimal.Parse(ComissionTextBox.Text));
- this.Close();
- }
- private void CategoryTextBox_Click(object sender, EventArgs e)
- {
- if (!CategoryTextBoxClicked)
- {
- CategoryTextBoxClicked = true;
- CategoryTextBox.Text = string.Empty;
- }
- }
- private void TypeTextBox_Click(object sender, EventArgs e)
- {
- if (!TypeTextBoxClicked)
- {
- TypeTextBoxClicked = true;
- TypeTextBox.Text = string.Empty;
- }
- }
- private void SumTextBox_Click(object sender, EventArgs e)
- {
- if (!SumTextBoxClicked)
- {
- SumTextBoxClicked = true;
- SumTextBox.Text = string.Empty;
- }
- }
- private void OpDateTimeTextBox_Click(object sender, EventArgs e)
- {
- if (!OpDateTimeTextBoxClicked)
- {
- OpDateTimeTextBoxClicked = true;
- OpDateTimeTextBox.Text = string.Empty;
- }
- }
- private void ComissionTextBox_Click(object sender, EventArgs e)
- {
- if (!ComissionTextBoxClicked)
- {
- ComissionTextBoxClicked = true;
- ComissionTextBox.Text = string.Empty;
- }
- }
- }
- }
- форма отчета
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using LR3.Controller;
- namespace LR3
- {
- public partial class ResultForm : Form
- {
- Query controller;
- public ResultForm()
- {
- InitializeComponent();
- controller = new Query(ConnectionString.ConnStr);
- }
- public ResultForm(DateTime dateFrom, DateTime dateTo, bool isCategory, string categoryOrType)
- {
- InitializeComponent();
- controller = new Query(ConnectionString.ConnStr);
- DateFromTextBox.Text = dateFrom.ToString();
- DateToTextBox.Text = dateTo.ToString();
- string label = string.Empty;
- DataTable data;
- if(isCategory)
- {
- label += "Category: ";
- data = controller.DoCategoryStuff(dateFrom, dateTo, categoryOrType);
- } else {
- label += "Type: ";
- data = controller.DoTypeStuff(dateFrom, dateTo, categoryOrType);
- }
- label += categoryOrType;
- CategoryOrTypeLabel.Text = label;
- DataGrid.DataSource = data;
- OpCountTextBox.Text = data.Rows.Count.ToString();
- decimal totalSum = 0;
- foreach(DataRow row in data.Rows)
- {
- totalSum += row.Field<decimal>("Sum");
- }
- AvOpSumTextBox.Text = (totalSum / data.Rows.Count).ToString();
- }
- }
- }
- запросы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Data.OleDb;
- using System.Data;
- namespace LR3.Controller
- {
- class Query
- {
- OleDbConnection connection;
- OleDbCommand command;
- OleDbDataAdapter dataAdaptor;
- DataTable bufferTable;
- public Query(string ConnStr)
- {
- connection = new OleDbConnection(ConnStr);
- bufferTable = new DataTable();
- }
- public DataTable UpdateOperation()
- {
- connection.Open();
- dataAdaptor = new OleDbDataAdapter("SELECT * FROM Operation", connection);
- bufferTable.Clear();
- dataAdaptor.Fill(bufferTable);
- connection.Close();
- return bufferTable;
- }
- public void Add(string Category, string Type, decimal Sum, DateTime OpDateTime, decimal Comission)
- {
- connection.Open();
- command = new OleDbCommand($"INSERT INTO Operation([ID], [Category], [Type], [Sum], [OpDateTime], [Comission]) VALUES(@ID, @Category, @Type, @Sum, @OpDateTime, @Comission)", connection);
- command.Parameters.Add("@ID", OleDbType.Integer).Value = GetNextID();
- command.Parameters.Add("@Category", OleDbType.VarChar).Value = Category;
- command.Parameters.Add("@Type", OleDbType.VarChar).Value = Type;
- command.Parameters.Add("@Sum", OleDbType.Currency).Value = Sum.ToString("0.0000");
- command.Parameters.Add("@OpDateTime", OleDbType.Date).Value = OpDateTime;
- command.Parameters.Add("@Comission", OleDbType.Currency).Value = Comission.ToString("0.0000");
- command.ExecuteNonQuery();
- connection.Close();
- }
- private int GetNextID()
- {
- dataAdaptor = new OleDbDataAdapter("SELECT MAX([ID]) AS [ID] FROM Operation", connection);
- bufferTable.Clear();
- dataAdaptor.Fill(bufferTable);
- var row = bufferTable.Rows[0];
- int res = row.IsNull("ID") ? 0 : bufferTable.Rows[0].Field<int>("ID") + 1;
- return res;
- }
- public void Delete(int ID)
- {
- connection.Open();
- command = new OleDbCommand($"DELETE FROM Operation WHERE ID = {ID}", connection);
- command.ExecuteNonQuery();
- connection.Close();
- }
- public DataTable DoCategoryStuff(DateTime dateFrom, DateTime dateTo, string category)
- {
- connection.Open();
- command = new OleDbCommand("SELECT [Type], [Sum], [OpDateTime], [Comission] FROM Operation WHERE [Category] = @category AND [OpDateTime] BETWEEN @dateFrom AND @dateTo ORDER BY [Comission]", connection);
- command.Parameters.Add("@category", OleDbType.VarChar).Value = category;
- command.Parameters.Add("@dateFrom", OleDbType.Date).Value = dateFrom;
- command.Parameters.Add("@dateTo", OleDbType.Date).Value = dateTo;
- dataAdaptor = new OleDbDataAdapter(command);
- string testString = command.CommandText;
- bufferTable.Clear();
- dataAdaptor.Fill(bufferTable);
- connection.Close();
- return bufferTable;
- }
- public DataTable DoTypeStuff(DateTime dateFrom, DateTime dateTo, string type)
- {
- connection.Open();
- command = new OleDbCommand("SELECT [Category], [Sum], [OpDateTime], [Comission] FROM Operation WHERE [Type] = @type AND [OpDateTime] BETWEEN @dateFrom AND @dateTo ORDER BY [Comission]", connection);
- command.Parameters.Add("@type", OleDbType.VarChar).Value = type;
- command.Parameters.Add("@dateFrom", OleDbType.Date).Value = dateFrom;
- command.Parameters.Add("@dateTo", OleDbType.Date).Value = dateTo;
- dataAdaptor = new OleDbDataAdapter(command);
- bufferTable.Clear();
- dataAdaptor.Fill(bufferTable);
- connection.Close();
- return bufferTable;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement