-
Notifications
You must be signed in to change notification settings - Fork 4
/
sql_queries.cs
439 lines (411 loc) · 14.2 KB
/
sql_queries.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
namespace Auction_Management_system
{
class sql_queries
{
private SqlConnection sqlcon;
public sql_queries(string connectionstrings)
{
sqlcon = new SqlConnection(connectionstrings);
}
///////////////////////////////////////////////////////////////Inserting data into the database//////////////////////////////////////////////////
//inserting the user information into the database
public void insert_user_information(string profilename, string username, string password)
{
try
{
sqlcon.Open();
string query = "Sign_Up";
SqlCommand sqlcmd = new SqlCommand(query, sqlcon);
sqlcmd.CommandType = CommandType.StoredProcedure;
SqlParameter sqlParameter = new SqlParameter("@username", username);
sqlcmd.Parameters.Add(sqlParameter);
SqlParameter parameter = new SqlParameter("@profilename", profilename);
sqlcmd.Parameters.Add(parameter);
SqlParameter parameter2 = new SqlParameter("@password", password);
sqlcmd.Parameters.Add(parameter2);
sqlcmd.ExecuteNonQuery();
}
catch (SqlException exception)
{
MessageBox.Show(exception.Message);
}
finally
{
sqlcon.Close();
}
}
//inserting the product information into the database
public void insert_product_info(string Title, double price, string category, string description, string image_location, string startdate, string enddate,string seller)
{
byte[] image = null;
FileStream fs = new FileStream(image_location, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
image = br.ReadBytes((int)fs.Length);
try
{
sqlcon.Open();
string query = "Insert_product";
SqlCommand sqlcmd;
sqlcmd = new SqlCommand(query, sqlcon);
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.Parameters.Add(new SqlParameter("@Title", Title));
sqlcmd.Parameters.Add(new SqlParameter("@price", price));
sqlcmd.Parameters.Add(new SqlParameter("@category", category));
sqlcmd.Parameters.Add(new SqlParameter("@description", description));
sqlcmd.Parameters.Add(new SqlParameter("@image", image));
sqlcmd.Parameters.Add(new SqlParameter("@start", startdate));
sqlcmd.Parameters.Add(new SqlParameter("@end", enddate));
sqlcmd.Parameters.Add(new SqlParameter("@seller", seller));
sqlcmd.ExecuteNonQuery();
}
catch (SqlException exception)
{
MessageBox.Show(exception.Message);
}
finally
{
sqlcon.Close();
}
}
///////////////////////////////////////////////////////////User Authentication/////////////////////////////////////////////////////////////
// checking for the existence of a specific username in the database
public bool check_username(string name)
{
DataTable dt = new DataTable();
try
{
sqlcon.Open();
string query = "check_username";
SqlCommand cmd = new SqlCommand(query, sqlcon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@name", name));
SqlDataAdapter sda = new SqlDataAdapter(query, sqlcon);
sda.SelectCommand = cmd;
sda.Fill(dt);
}
catch (SqlException exception)
{
MessageBox.Show(exception.Message);
}
finally
{
sqlcon.Close();
}
return dt.Rows.Count > 0;
}
// checking for the existence of a specific username and password in the database
public bool check_username_and_password(string name, string password)
{
DataTable dt = new DataTable();
try
{
sqlcon.Open();
string query = "check_username_and_password";
SqlCommand cmd = new SqlCommand(query, sqlcon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@name", name));
cmd.Parameters.Add(new SqlParameter("@password", password));
SqlDataAdapter sda = new SqlDataAdapter(query, sqlcon);
sda.SelectCommand = cmd;
sda.Fill(dt);
}
catch (SqlException exception)
{
MessageBox.Show(exception.Message);
}
finally
{
sqlcon.Close();
}
return dt.Rows.Count > 0;
}
public void Delete_user(string name)
{
try
{
sqlcon.Open();
string query = "delete from UserInformation where username = '" + name + "'";
SqlCommand cmd = new SqlCommand(query, sqlcon);
cmd.ExecuteNonQuery();
}
catch (SqlException exception)
{
MessageBox.Show(exception.Message);
}
finally
{
sqlcon.Close();
}
}
/////////////////////////////////////////////////////////////////Retrieving data from the database//////////////////////////////////////////////////////
public DataTable profilename(string username)
{
DataTable dt = new DataTable();
string query = "select * from UserInformation where username = '" + username + "'";
try
{
sqlcon.Open();
SqlDataAdapter sda = new SqlDataAdapter(query, sqlcon);
sda.Fill(dt);
}
catch(SqlException exception)
{
MessageBox.Show(exception.Message);
}
finally
{
sqlcon.Close();
}
return dt;
}
/////////////////////////////////////////////////////////////////Retrieving data from the database//////////////////////////////////////////////////////
//retrieve all of products to show it in the home page
public DataTable Retrieve_products()
{
DataTable dt = new DataTable();
try
{
sqlcon.Open();
string query = "select * from product";
SqlDataAdapter sda = new SqlDataAdapter(query, sqlcon);
sda.Fill(dt);
}
catch (SqlException exception)
{
MessageBox.Show(exception.Message);
}
finally
{
sqlcon.Close();
}
return dt;
}
//searching for specific product
public DataTable search_product(string title, string category)
{
DataTable dt = new DataTable();
string query;
if (category == "")
{
query = "select * from product where Title like'%" + title + "%'";
}
else
{
query = "select * from product where category='" + category + "' and Title like'%" + title + "%'";
}
try
{
sqlcon.Open();
SqlDataAdapter sda = new SqlDataAdapter(query, sqlcon);
sda.Fill(dt);
}
catch (SqlException exception)
{
MessageBox.Show(exception.Message);
}
finally
{
sqlcon.Close();
}
return dt;
}
//searching for a product in a specific category
public DataTable Filter_by_category(string category)
{
DataTable dt = new DataTable();
try
{
sqlcon.Open();
string querys = "select * from product where category ='" + category + "'";
SqlDataAdapter sda = new SqlDataAdapter(querys, sqlcon);
sda.Fill(dt);
}
catch (SqlException exception)
{
MessageBox.Show(exception.Message);
}
finally
{
sqlcon.Close();
}
return dt;
}
//------------------------------ check catagory ---------------------------//
public bool check_catagory(string catagory)
{
DataTable dt = new DataTable();
try
{
sqlcon.Open();
string query = "check_category";
SqlCommand cmd = new SqlCommand(query, sqlcon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@category", catagory));
SqlDataAdapter sda = new SqlDataAdapter(query, sqlcon);
sda.SelectCommand = cmd;
sda.Fill(dt);
}
catch (SqlException exception)
{
MessageBox.Show(exception.Message);
}
finally
{
sqlcon.Close();
}
return dt.Rows.Count > 0;
}
////// delete catagory//////////////
public void Delete_catagory(string catagory)
{
try
{
sqlcon.Open();
string query = "delete from Category where category_name = '" + catagory + "'";
SqlCommand cmd = new SqlCommand(query, sqlcon);
cmd.ExecuteNonQuery();
}
catch (SqlException exception)
{
MessageBox.Show(exception.Message);
}
finally
{
sqlcon.Close();
}
}
// update
public void update_catagories(string catagory)
{
try
{
sqlcon.Open();
string query = "update product set category = 'others' where category= '" + catagory + "'";
SqlCommand cmd = new SqlCommand(query, sqlcon);
cmd.ExecuteNonQuery();
}
catch (SqlException exception)
{
MessageBox.Show(exception.Message);
}
finally
{
sqlcon.Close();
}
}
// add catagory
public void Add_catagory(string catagory)
{
try
{
sqlcon.Open();
string query = "insert into Category values ( '" + catagory + "')";
SqlCommand cmd = new SqlCommand(query, sqlcon);
cmd.ExecuteNonQuery();
}
catch (SqlException exception)
{
MessageBox.Show(exception.Message);
}
finally
{
sqlcon.Close();
}
}
// returns all the products found in a specific category
public DataTable Get_categories()
{
DataTable dt = new DataTable();
try
{
sqlcon.Open();
string query = "select * from Category";
SqlDataAdapter sda = new SqlDataAdapter(query, sqlcon);
sda.Fill(dt);
}
catch (SqlException exception)
{
MessageBox.Show(exception.Message);
}
finally
{
sqlcon.Close();
}
return dt;
}
///////////////////////////////////////////////////////Update queries////////////////////////////////////////////////
public void update_top_price(double value, int Id, string username)
{
string query = "update product set price = " + value + ", Winner ='" + username + "'" + "where id =" + Id;
try
{
sqlcon.Open();
SqlCommand sqlcmd = new SqlCommand(query, sqlcon);
sqlcmd.ExecuteNonQuery();
}
catch (SqlException exception)
{
MessageBox.Show(exception.Message);
}
finally
{
sqlcon.Close();
}
}
public void update_user(string new_name,string old_name,string type)
{
string query = "update UserInformation set " + type + "= '" + new_name + "'"+"where "+type+"= '"+old_name+"'";
try
{
sqlcon.Open();
SqlCommand sqlcmd = new SqlCommand(query, sqlcon);
sqlcmd.ExecuteNonQuery();
}
catch (SqlException exception)
{
MessageBox.Show(exception.Message);
}
finally
{
sqlcon.Close();
}
}
/////////////////////////////////////////Deleting queries////////////////////////////////////////////////////////////
public void delete_product(int id)
{
string query = "delete from product where id = " + id;
try
{
sqlcon.Open();
SqlCommand cmd = new SqlCommand(query, sqlcon);
cmd.ExecuteNonQuery();
}
catch(SqlException exception)
{
MessageBox.Show(exception.Message);
}
finally
{
sqlcon.Close();
}
}
}
}