· 6 years ago · Aug 29, 2019, 07:42 AM
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using System.Windows;
7using System.Windows.Controls;
8using System.Windows.Data;
9using System.Windows.Documents;
10using System.Windows.Input;
11using System.Windows.Media;
12using System.Windows.Media.Imaging;
13using System.Windows.Navigation;
14using System.Windows.Shapes;
15using System.IO;
16using System.Data;
17using System.Data.SQLite;
18
19
20namespace VarehusOpgaveWPF
21{
22 /// <summary>
23 /// Interaction logic for MainWindow.xaml
24 /// </summary>
25 public partial class MainWindow : Window
26 {
27 public List<Person> personList = new List<Person>();
28 public List<Order> orderList = new List<Order>();
29 public List<Item> itemList = new List<Item>();
30
31 public string personTable = "persontable";
32 public string orderTable = "ordertable";
33 public string itemTable = "itemtable";
34
35 Button newbtn;
36
37 private const string databaseFile = "databaseFile.db";
38 private const string databaseSource = "data source=" + databaseFile;
39
40 public MainWindow()
41 {
42 InitializeComponent();
43
44 if (!File.Exists(databaseFile))
45 {
46 SQLiteConnection.CreateFile(databaseFile);
47 }
48
49 using (var connection = new SQLiteConnection(databaseSource))
50 {
51 using (var command = new SQLiteCommand(connection))
52 {
53 connection.Open();
54 command.CommandType = CommandType.Text;
55
56 //command.CommandText = $"DROP TABLE {personTable}";
57 //command.ExecuteNonQuery();
58 command.CommandText = $"CREATE TABLE IF NOT EXISTS {personTable} (cpr int, name varchar(50), zipcode int)";
59 command.ExecuteNonQuery();
60 command.CommandText = $"INSERT INTO {personTable} (cpr, name, zipcode) VALUES (021292, 'Christoffer', 8800)";
61 command.ExecuteNonQuery();
62 command.CommandText = $"INSERT INTO {personTable} (cpr, name, zipcode) VALUES (011295, 'Nicklas', 8850)";
63 command.ExecuteNonQuery();
64 command.CommandText = $"INSERT INTO {personTable} (cpr, name, zipcode) VALUES (030296, 'Anne Marie', 7300)";
65 command.ExecuteNonQuery();
66
67 //command.CommandText = $"DROP TABLE {orderTable}";
68 //command.ExecuteNonQuery();
69 command.CommandText = $"CREATE TABLE IF NOT EXISTS {orderTable} (cpr int, itemID int)";
70 command.ExecuteNonQuery();
71 command.CommandText = $"INSERT INTO {orderTable} (cpr, itemID) VALUES (021292, 1)";
72 command.ExecuteNonQuery();
73 command.CommandText = $"INSERT INTO {orderTable} (cpr, itemID) VALUES (021292, 2)";
74 command.ExecuteNonQuery();
75 command.CommandText = $"INSERT INTO {orderTable} (cpr, itemID) VALUES (030296, 3)";
76 command.ExecuteNonQuery();
77
78 //command.CommandText = $"DROP TABLE {itemTable}";
79 //command.ExecuteNonQuery();
80 command.CommandText = $"create table if not exists {itemTable} (id, 'name', price)";
81 command.ExecuteNonQuery();
82 command.CommandText = $"insert into {itemTable} (id, name, price) values (1, 'tomato', 20)";
83 command.ExecuteNonQuery();
84 command.CommandText = $"insert into {itemTable} (id, name, price) values (2, 'cucumber', 15)";
85 command.ExecuteNonQuery();
86 command.CommandText = $"insert into {itemTable} (id, name, price) values (3, 'carrot', 10)";
87 command.ExecuteNonQuery();
88 command.CommandText = $"insert into {itemTable} (id, name, price) values (4, 'potato', 5)";
89 command.ExecuteNonQuery();
90
91 command.CommandText = $"SELECT * FROM {personTable}, {orderTable}, {itemTable}";
92 SQLiteDataReader reader = command.ExecuteReader();
93
94 while (reader.Read())
95 {
96 personList.Add(new Person(Convert.ToInt32(reader["cpr"]), Convert.ToString(reader["name"]), Convert.ToInt32(reader["zipcode"])));
97 orderList.Add(new Order(Convert.ToInt32(reader["cpr"]), Convert.ToInt32(reader["itemID"])));
98 itemList.Add(new Item(Convert.ToInt32(reader["id"]), Convert.ToString(reader["name"]), Convert.ToInt32(reader["price"])));
99 }
100 }
101 }
102 for (int i = 0; i < personList.Count; i++)
103 {
104 newbtn = new Button
105 {
106 Content = personList[i].name,
107 Tag = personList[i].cpr
108 };
109 newbtn.Click += new RoutedEventHandler(ShowOrders);
110 personPanel.Children.Add(newbtn);
111 }
112 }
113 public void ShowOrders(object sender, RoutedEventArgs e)
114 {
115 using (var connection = new SQLiteConnection(databaseSource))
116 {
117 using (var command = new SQLiteCommand(connection))
118 {
119 connection.Open();
120 command.CommandType = CommandType.Text;
121 command.CommandText = "";
122 command.ExecuteNonQuery();
123 }
124 }
125 }
126 }
127 public class Person
128 {
129 public int cpr;
130 public string name;
131 public int zipcode;
132
133 public Person(int cpr, string name, int zipcode)
134 {
135 this.cpr = cpr;
136 this.name = name;
137 this.zipcode = zipcode;
138 }
139 }
140 public class Order
141 {
142 public int cpr;
143 public int itemID;
144
145 public Order(int cpr, int itemID)
146 {
147 this.cpr = cpr;
148 this.itemID = itemID;
149 }
150 }
151 public class Item
152 {
153 public int id;
154 public string itemName;
155 public int price;
156
157 public Item(int id, string itemName, int price)
158 {
159 this.id = id;
160 this.itemName = itemName;
161 this.price = price;
162 }
163 }
164}