-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductRepo.cs
More file actions
149 lines (98 loc) · 3.83 KB
/
ProductRepo.cs
File metadata and controls
149 lines (98 loc) · 3.83 KB
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
using System;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
namespace Crud_OperationsNC4
{
public class ProductRepo
{
public static string connString;
public List<Product> GetProducts()
{
MySqlConnection conn = new MySqlConnection(connString);
using (conn)
{
conn.Open();
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT ProductID, Name, Price, StockLevel FROM products;";
MySqlDataReader reader = cmd.ExecuteReader();
var products = new List<Product>();
while (reader.Read())
{
var row = new Product();
row.ProductID = reader.GetInt32("ProductID");
row.Name = reader.GetString("Name");
row.Price = reader.GetDecimal("Price");
row.StockLevel = reader.GetInt32("StockLevel");
products.Add(row);
}
return products;
}
}
public void CreateProduct(Product p)
{
MySqlConnection conn = new MySqlConnection(connString);
using (conn)
{
conn.Open();
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO products(Name, Price, CategoryID, OnSale) " +
"VALUES(@name, @price, @catID, @sale);";
cmd.Parameters.AddWithValue("name", p.Name);
cmd.Parameters.AddWithValue("price", p.Price);
cmd.Parameters.AddWithValue("catID", p.CategoryID);
cmd.Parameters.AddWithValue("sale", p.OnSale);
cmd.ExecuteNonQuery();
}
}
public void UpdateProduct(Product p)
{
MySqlConnection conn = new MySqlConnection(connString);
using (conn)
{
conn.Open();
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "Update products SET StockLevel = @stock WHERE ProductID=@prodID;";
cmd.Parameters.AddWithValue("stock", p.StockLevel);
cmd.Parameters.AddWithValue("prodID", p.ProductID);
cmd.ExecuteNonQuery();
}
}
public void DeleteProduct(int prodID, string name)
{
MySqlConnection conn = new MySqlConnection(connString);
using (conn)
{
conn.Open();
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "DELETE FROM products WHERE productID = @prodID AND Name = @name;";
cmd.Parameters.AddWithValue("prodID", prodID);
cmd.Parameters.AddWithValue("name", name);
cmd.ExecuteNonQuery();
}
}
public void DeleteProduct(int prodID)
{
MySqlConnection conn = new MySqlConnection(connString);
using (conn)
{
conn.Open();
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "DELETE FROM products WHERE ProductID = @prodID;";
cmd.Parameters.AddWithValue("prodID", prodID);
cmd.ExecuteNonQuery();
}
}
public void DeleteProduct(string name)
{
MySqlConnection conn = new MySqlConnection(connString);
using (conn)
{
conn.Open();
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "DELETE FROM products WHERE Name = @name;";
cmd.Parameters.AddWithValue("name", name);
cmd.ExecuteNonQuery();
}
}
}
}