Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SQLite;
  4. using System.Windows.Forms;
  5.  
  6. namespace DB_Tree
  7. {
  8. public partial class TreeVisualization : Form
  9. {
  10. List<string> towns;
  11.  
  12. public TreeVisualization()
  13. {
  14. towns = new List<string>();
  15.  
  16. InitializeComponent();
  17. InitializeTVResources();
  18. }
  19.  
  20. public void InitializeTVResources()
  21. {
  22. var sBuilder = new SQLiteConnectionStringBuilder();
  23. sBuilder.DataSource = @"C:\Users\thesk\OneDrive\Desktop\ПГНИУ\Практика\УП БД\Лаба 1\Workers.sqlite";
  24. sBuilder.ForeignKeys = true;
  25.  
  26. string sConnStr = sBuilder.ConnectionString;
  27.  
  28. using (SQLiteConnection sConn = new SQLiteConnection(sConnStr))
  29. {
  30. sConn.Open();
  31. SQLiteCommand sCommand = new SQLiteCommand();
  32. sCommand.Connection = sConn;
  33. sCommand.CommandText = @"
  34. SELECT town.id as t_id,
  35. town.name as t_name,
  36. company.id as c_id,
  37. company.name as c_name,
  38. worker.name as w_name
  39. FROM town
  40. LEFT OUTER JOIN company
  41. ON town.id = company.town_id
  42. LEFT OUTER JOIN worker
  43. ON company.id = worker.company_id
  44. ORDER BY t_name,t_id,c_name ,c_id,w_name";
  45.  
  46. using (SQLiteDataReader reader = sCommand.ExecuteReader())
  47. {
  48. TreeNode CurrentTown = null;
  49. TreeNode CurrentCompany = null;
  50. long CurrentTownId = -1;
  51. long CurrentCompanyId = -1;
  52.  
  53. while (reader.Read())
  54. {
  55. string TownName = (string)reader["t_name"];
  56. long TownId = (long)reader["t_id"];
  57.  
  58. if (TownId != CurrentTownId)
  59. {
  60. towns.Add(TownName);
  61. CurrentTown = DBVisualization.Nodes.Add(TownName);
  62. CurrentTownId = TownId;
  63. }
  64.  
  65. if (reader["c_id"] is DBNull)
  66. continue;
  67.  
  68. string CompanyName = (string)reader["c_name"];
  69. long CompanyId = (long)reader["c_id"];
  70.  
  71. if (CompanyId != CurrentCompanyId)
  72. {
  73. CurrentCompany = CurrentTown.Nodes.Add(CompanyName);
  74. CurrentCompanyId = CompanyId;
  75. }
  76.  
  77. if (reader["w_name"] is DBNull)
  78. continue;
  79.  
  80. string WorkerName = (string)reader["w_name"];
  81. CurrentCompany.Nodes.Add(WorkerName);
  82.  
  83. }
  84. }
  85. }
  86. }
  87.  
  88. private void DBVisualization_AfterCollapse(object sender, TreeViewEventArgs e)
  89. {
  90. if (e.Node.Level != 0)
  91. return;
  92. e.Node.Text = towns[e.Node.Index];
  93. }
  94.  
  95. private void DBVisualization_AfterExpand(object sender, TreeViewEventArgs e)
  96. {
  97. if (e.Node.Level != 0)
  98. return;
  99.  
  100. int A = 0, B = 0;
  101. foreach (TreeNode node in e.Node.Nodes)
  102. if (node.Nodes.Count != 0)
  103. A++;
  104. else
  105. B++;
  106.  
  107. e.Node.Text += $" {A}/{B}";
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement