-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBAdaptor.cs
216 lines (195 loc) · 8.71 KB
/
DBAdaptor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.OleDb;
using System.Data.SqlClient;
namespace WindowsFormsApplication4
{
class DBAdaptor
{
private OleDbConnection connection = null;
private bool ready = false;
public DBAdaptor()
{
if (connection == null) reopenConnection();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
@"Data Source= C:\Users\Tiasa Mondol\Dropbox\Project\AwesomePOS-master\AwesomePOS.accdb;Persist Security Info=False;";
try
{
connection.Open();
ready = true;
}
catch (Exception ex)
{
Console.WriteLine("Failed to connect to data source!!! " + ex);
}
}
private void reopenConnection()
{
if (connection != null) closeConnection();
connection = new System.Data.OleDb.OleDbConnection();
}
public void closeConnection()
{
connection.Close();
connection.Dispose();
ready = false;
}
public DataTypes.Good getItemById(int itemId)
{
DataTypes.Good goodey = new DataTypes.Good();
string sqlStr = @"SELECT * FROM Items WHERE ID = " + itemId;
OleDbCommand cmd = new OleDbCommand(sqlStr, connection);
OleDbDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
goodey.id = Convert.ToInt32(reader["ID"].ToString());
goodey.imageURL = reader["ImageURL"].ToString();
goodey.name = reader["ProductName"].ToString();
goodey.price = Convert.ToDouble(reader["Price"].ToString());
}
else return null;
return goodey;
}
public List<int> getItemsForOrderId(int orderId)
{
List<int> list = new List<int>();
OleDbCommand cmd = new OleDbCommand("SELECT ProductId FROM OrderItems WHERE OrderId = " + orderId, connection);
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
list.Add(Convert.ToInt32(reader["ProductId"].ToString()));
}
return list;
}
/*public List<DataTypes.Good> getAllItems()
{
List<DataTypes.Good> list = new List<DataTypes.Good>();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM Items", connection);
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
DataTypes.Good goodey = new DataTypes.Good();
goodey.id = Convert.ToInt32(reader["ID"].ToString());
goodey.imageURL = reader["ImageURL"].ToString();
goodey.name = reader["ProductName"].ToString();
goodey.price = Convert.ToDouble(reader["Price"].ToString());
list.Add(goodey);
}
return list;
} */
public List<DataTypes.Sale> getSales(int skip = 0, int count = 0)
{
List<DataTypes.Sale> list = new List<DataTypes.Sale>();
string sqlString = @"SELECT * FROM Sales ";
if (skip != 0 || count != 0)
sqlString += @"WHERE ID BETWEEN " + (skip+1) + " AND " + (skip+count) + " AND ACTIVE = YES";
else sqlString += @" WHERE ACTIVE = YES";
OleDbCommand cmd = new OleDbCommand(sqlString, connection);
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
DataTypes.Sale sale = new DataTypes.Sale();
sale.id = Convert.ToInt32(reader["ID"].ToString());
sale.name = reader["SaleName"].ToString();
sale.value = Convert.ToInt32(reader["SaleValue"].ToString());
list.Add(sale);
}
return list;
}
public List<DataTypes.Good> getBeverage(int skip = 0, int count = 0)
{
List<DataTypes.Good> list = new List<DataTypes.Good>();
string sqlString;
if (skip != 0) sqlString = @"SELECT TOP " + count + " * FROM Items WHERE ID NOT IN (SELECT TOP " + skip + " ID FROM Items WHERE Type=" + "\'" + "Beverage" + "\'" + ") AND Type=" + "\'" + "Beverage" + "\'";
else sqlString = @"SELECT TOP " + count + " * FROM Items WHERE Type=" + "\'" + "Beverage" + "\'";
OleDbCommand cmd = new OleDbCommand(sqlString, connection);
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
DataTypes.Good goodey = new DataTypes.Good();
goodey.id = Convert.ToInt32(reader["ID"].ToString());
goodey.imageURL = reader["ImageURL"].ToString();
goodey.name = reader["ProductName"].ToString();
goodey.price = Convert.ToDouble(reader["Price"].ToString());
list.Add(goodey);
}
return list;
}
public List<DataTypes.Good> getGoods(int skip = 0, int count = 0)
{
List<DataTypes.Good> list = new List<DataTypes.Good>();
string sqlString ;
if (skip != 0) sqlString = @"SELECT TOP " + count + " * FROM Items WHERE ID NOT IN (SELECT TOP " + skip + " ID FROM Items WHERE Type=" + "\'" + "Food" + "\'" + ") AND Type=" + "\'" + "Food" + "\'";
else sqlString = @"SELECT TOP " + count + " * FROM Items WHERE Type=" + "\'" + "Food" + "\'";
OleDbCommand cmd = new OleDbCommand(sqlString, connection);
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
DataTypes.Good goodey = new DataTypes.Good();
goodey.id = Convert.ToInt32(reader["ID"].ToString());
goodey.imageURL = reader["ImageURL"].ToString();
goodey.name = reader["ProductName"].ToString();
goodey.price = Convert.ToDouble(reader["Price"].ToString());
list.Add(goodey);
}
return list;
}
public DataTypes.Sale getSaleById(int saleId)
{
DataTypes.Sale sale = new DataTypes.Sale();
string sqlStr = @"SELECT * FROM Sales WHERE ID = " + saleId + " AND Active = YES";
OleDbCommand cmd = new OleDbCommand(sqlStr, connection);
OleDbDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
sale.id = Convert.ToInt32(reader["ID"].ToString());
sale.value = Convert.ToInt32(reader["SaleValue"].ToString());
sale.name = reader["SaleName"].ToString();
}
else return null;
return sale;
}
public bool testConnection()
{
return ready;
}
internal void saveOrder(List<DataTypes.Good> currentOrder, int saleValue, int orderno)
{
/*throw new NotImplementedException();*/
double total = 0;
//connection.Close();
//connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;" +
//@"Data Source= C:\Users\Tiasa Mondol\Dropbox\Project\AwesomePOSorder.accdb;Persist Security Info=False;");
OleDbCommand cmd = connection.CreateCommand();
//connection.Open();
foreach (DataTypes.Good thingy in currentOrder)
//foreach (DataTypes.Good thingy in currentOrder)
{
total += thingy.price;
cmd.CommandText = @"INSERT INTO OrderItems (OrderId, ProductId) VALUES ('" + orderno + "','" + thingy.id + "')";
cmd.ExecuteNonQuery();
}
cmd.CommandText = @"INSERT INTO Orders (Total, Tax, SaleValue) VALUES ('" + total + "','" + (total*saleValue/100) + "','"+saleValue+"')";
cmd.ExecuteNonQuery();
/* con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=student.mdb; Persist Security Info=False;");
cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "update student set lname = '" + textBox2.Text + "' , fname ='" + textBox3.Text + "' where id = '" + textBox1.Text + "';";
con.Open();
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Data Succesfully updated");
}
catch (Exception Ex)
{
MessageBox.Show("Cannot update "+Ex.Message);
}
con.Close();
*/
}
}
}