Advertisement
baadgeorge

Untitled

Dec 13th, 2021
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.22 KB | None | 0 0
  1. программа
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7.  
  8. namespace LR3
  9. {
  10. static class Program
  11. {
  12. /// <summary>
  13. /// Главная точка входа для приложения.
  14. /// </summary>
  15. [STAThread]
  16. static void Main()
  17. {
  18. Application.EnableVisualStyles();
  19. Application.SetCompatibleTextRenderingDefault(false);
  20. Application.Run(new MainForm());
  21. }
  22. }
  23. }
  24.  
  25. основное окно
  26. using System;
  27. using System.Collections.Generic;
  28. using System.ComponentModel;
  29. using System.Data;
  30. using System.Drawing;
  31. using System.Linq;
  32. using System.Text;
  33. using System.Threading.Tasks;
  34. using System.Windows.Forms;
  35.  
  36. using LR3.Controller;
  37.  
  38. namespace LR3
  39. {
  40. public partial class MainForm : Form
  41. {
  42. Query controller;
  43.  
  44. public MainForm()
  45. {
  46. InitializeComponent();
  47. controller = new Query(ConnectionString.ConnStr);
  48. DataGrid.DataSource = controller.UpdateOperation();
  49. }
  50.  
  51. private void UpdateButton_Click(object sender, EventArgs e)
  52. {
  53. DataGrid.DataSource = controller.UpdateOperation();
  54. }
  55.  
  56. private void AddButton_Click(object sender, EventArgs e)
  57. {
  58. Form dialog_window = new AddNewForm();
  59. dialog_window.ShowDialog();
  60. DataGrid.DataSource = controller.UpdateOperation();
  61. }
  62.  
  63. private void DeleteButton_Click(object sender, EventArgs e)
  64. {
  65. controller.Delete(int.Parse(DataGrid.Rows[DataGrid.CurrentRow.Index].Cells["ID"].Value.ToString()));
  66. DataGrid.DataSource = controller.UpdateOperation();
  67. }
  68.  
  69. private void GenerateReportButton_Click(object sender, EventArgs e)
  70. {
  71. Form newForm = new GenerateReportForm();
  72. newForm.Show();
  73. }
  74. }
  75. }
  76.  
  77. форма добавления операции
  78. using System;
  79. using System.Collections.Generic;
  80. using System.ComponentModel;
  81. using System.Data;
  82. using System.Drawing;
  83. using System.Linq;
  84. using System.Text;
  85. using System.Threading.Tasks;
  86. using System.Windows.Forms;
  87.  
  88. using LR3.Controller;
  89.  
  90. namespace LR3
  91. {
  92. public partial class AddNewForm : Form
  93. {
  94. bool CategoryTextBoxClicked = false;
  95. bool TypeTextBoxClicked = false;
  96. bool SumTextBoxClicked = false;
  97. bool OpDateTimeTextBoxClicked = false;
  98. bool ComissionTextBoxClicked = false;
  99.  
  100. Query controller;
  101.  
  102. public AddNewForm()
  103. {
  104. InitializeComponent();
  105. controller = new Query(ConnectionString.ConnStr);
  106. }
  107.  
  108. private void AddButton_Click(object sender, EventArgs e)
  109. {
  110. controller.Add(CategoryTextBox.Text,
  111. TypeTextBox.Text,
  112. decimal.Parse(SumTextBox.Text),
  113. DateTime.Parse(OpDateTimeTextBox.Text),
  114. decimal.Parse(ComissionTextBox.Text));
  115. this.Close();
  116. }
  117.  
  118. private void CategoryTextBox_Click(object sender, EventArgs e)
  119. {
  120. if (!CategoryTextBoxClicked)
  121. {
  122. CategoryTextBoxClicked = true;
  123. CategoryTextBox.Text = string.Empty;
  124. }
  125. }
  126.  
  127. private void TypeTextBox_Click(object sender, EventArgs e)
  128. {
  129. if (!TypeTextBoxClicked)
  130. {
  131. TypeTextBoxClicked = true;
  132. TypeTextBox.Text = string.Empty;
  133. }
  134. }
  135.  
  136. private void SumTextBox_Click(object sender, EventArgs e)
  137. {
  138. if (!SumTextBoxClicked)
  139. {
  140. SumTextBoxClicked = true;
  141. SumTextBox.Text = string.Empty;
  142. }
  143. }
  144.  
  145. private void OpDateTimeTextBox_Click(object sender, EventArgs e)
  146. {
  147. if (!OpDateTimeTextBoxClicked)
  148. {
  149. OpDateTimeTextBoxClicked = true;
  150. OpDateTimeTextBox.Text = string.Empty;
  151. }
  152. }
  153.  
  154. private void ComissionTextBox_Click(object sender, EventArgs e)
  155. {
  156. if (!ComissionTextBoxClicked)
  157. {
  158. ComissionTextBoxClicked = true;
  159. ComissionTextBox.Text = string.Empty;
  160. }
  161. }
  162. }
  163. }
  164.  
  165. форма отчета
  166. using System;
  167. using System.Collections.Generic;
  168. using System.ComponentModel;
  169. using System.Data;
  170. using System.Drawing;
  171. using System.Linq;
  172. using System.Text;
  173. using System.Threading.Tasks;
  174. using System.Windows.Forms;
  175.  
  176. using LR3.Controller;
  177.  
  178. namespace LR3
  179. {
  180. public partial class ResultForm : Form
  181. {
  182. Query controller;
  183.  
  184. public ResultForm()
  185. {
  186. InitializeComponent();
  187. controller = new Query(ConnectionString.ConnStr);
  188. }
  189. public ResultForm(DateTime dateFrom, DateTime dateTo, bool isCategory, string categoryOrType)
  190. {
  191. InitializeComponent();
  192. controller = new Query(ConnectionString.ConnStr);
  193.  
  194. DateFromTextBox.Text = dateFrom.ToString();
  195. DateToTextBox.Text = dateTo.ToString();
  196.  
  197. string label = string.Empty;
  198. DataTable data;
  199. if(isCategory)
  200. {
  201. label += "Category: ";
  202. data = controller.DoCategoryStuff(dateFrom, dateTo, categoryOrType);
  203. } else {
  204. label += "Type: ";
  205. data = controller.DoTypeStuff(dateFrom, dateTo, categoryOrType);
  206. }
  207. label += categoryOrType;
  208. CategoryOrTypeLabel.Text = label;
  209.  
  210. DataGrid.DataSource = data;
  211.  
  212. OpCountTextBox.Text = data.Rows.Count.ToString();
  213.  
  214. decimal totalSum = 0;
  215.  
  216. foreach(DataRow row in data.Rows)
  217. {
  218. totalSum += row.Field<decimal>("Sum");
  219. }
  220.  
  221. AvOpSumTextBox.Text = (totalSum / data.Rows.Count).ToString();
  222. }
  223. }
  224. }
  225.  
  226. запросы
  227. using System;
  228. using System.Collections.Generic;
  229. using System.Linq;
  230. using System.Text;
  231. using System.Threading.Tasks;
  232. using System.Data.OleDb;
  233. using System.Data;
  234.  
  235. namespace LR3.Controller
  236. {
  237. class Query
  238. {
  239. OleDbConnection connection;
  240. OleDbCommand command;
  241. OleDbDataAdapter dataAdaptor;
  242. DataTable bufferTable;
  243.  
  244. public Query(string ConnStr)
  245. {
  246. connection = new OleDbConnection(ConnStr);
  247. bufferTable = new DataTable();
  248. }
  249. public DataTable UpdateOperation()
  250. {
  251. connection.Open();
  252. dataAdaptor = new OleDbDataAdapter("SELECT * FROM Operation", connection);
  253.  
  254. bufferTable.Clear();
  255. dataAdaptor.Fill(bufferTable);
  256.  
  257. connection.Close();
  258. return bufferTable;
  259. }
  260.  
  261. public void Add(string Category, string Type, decimal Sum, DateTime OpDateTime, decimal Comission)
  262. {
  263. connection.Open();
  264.  
  265. command = new OleDbCommand($"INSERT INTO Operation([ID], [Category], [Type], [Sum], [OpDateTime], [Comission]) VALUES(@ID, @Category, @Type, @Sum, @OpDateTime, @Comission)", connection);
  266. command.Parameters.Add("@ID", OleDbType.Integer).Value = GetNextID();
  267. command.Parameters.Add("@Category", OleDbType.VarChar).Value = Category;
  268. command.Parameters.Add("@Type", OleDbType.VarChar).Value = Type;
  269. command.Parameters.Add("@Sum", OleDbType.Currency).Value = Sum.ToString("0.0000");
  270. command.Parameters.Add("@OpDateTime", OleDbType.Date).Value = OpDateTime;
  271. command.Parameters.Add("@Comission", OleDbType.Currency).Value = Comission.ToString("0.0000");
  272.  
  273. command.ExecuteNonQuery();
  274.  
  275. connection.Close();
  276. }
  277.  
  278. private int GetNextID()
  279. {
  280. dataAdaptor = new OleDbDataAdapter("SELECT MAX([ID]) AS [ID] FROM Operation", connection);
  281.  
  282. bufferTable.Clear();
  283. dataAdaptor.Fill(bufferTable);
  284.  
  285.  
  286. var row = bufferTable.Rows[0];
  287. int res = row.IsNull("ID") ? 0 : bufferTable.Rows[0].Field<int>("ID") + 1;
  288.  
  289. return res;
  290. }
  291.  
  292. public void Delete(int ID)
  293. {
  294. connection.Open();
  295.  
  296. command = new OleDbCommand($"DELETE FROM Operation WHERE ID = {ID}", connection);
  297. command.ExecuteNonQuery();
  298.  
  299. connection.Close();
  300. }
  301.  
  302. public DataTable DoCategoryStuff(DateTime dateFrom, DateTime dateTo, string category)
  303. {
  304. connection.Open();
  305. command = new OleDbCommand("SELECT [Type], [Sum], [OpDateTime], [Comission] FROM Operation WHERE [Category] = @category AND [OpDateTime] BETWEEN @dateFrom AND @dateTo ORDER BY [Comission]", connection);
  306.  
  307. command.Parameters.Add("@category", OleDbType.VarChar).Value = category;
  308. command.Parameters.Add("@dateFrom", OleDbType.Date).Value = dateFrom;
  309. command.Parameters.Add("@dateTo", OleDbType.Date).Value = dateTo;
  310.  
  311. dataAdaptor = new OleDbDataAdapter(command);
  312.  
  313. string testString = command.CommandText;
  314.  
  315. bufferTable.Clear();
  316. dataAdaptor.Fill(bufferTable);
  317.  
  318. connection.Close();
  319. return bufferTable;
  320. }
  321.  
  322. public DataTable DoTypeStuff(DateTime dateFrom, DateTime dateTo, string type)
  323. {
  324. connection.Open();
  325. command = new OleDbCommand("SELECT [Category], [Sum], [OpDateTime], [Comission] FROM Operation WHERE [Type] = @type AND [OpDateTime] BETWEEN @dateFrom AND @dateTo ORDER BY [Comission]", connection);
  326.  
  327. command.Parameters.Add("@type", OleDbType.VarChar).Value = type;
  328. command.Parameters.Add("@dateFrom", OleDbType.Date).Value = dateFrom;
  329. command.Parameters.Add("@dateTo", OleDbType.Date).Value = dateTo;
  330.  
  331. dataAdaptor = new OleDbDataAdapter(command);
  332.  
  333. bufferTable.Clear();
  334. dataAdaptor.Fill(bufferTable);
  335.  
  336. connection.Close();
  337. return bufferTable;
  338. }
  339. }
  340. }
  341.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement