-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConectDB.cs
More file actions
212 lines (194 loc) · 7.86 KB
/
ConectDB.cs
File metadata and controls
212 lines (194 loc) · 7.86 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
using System.Data.SqlClient;
using System.Data;
using Microsoft.Data.SqlClient;
//using Microsoft.Data.SqlClient.SqlException;
namespace Manager
{
class ConectDB
{
public int InsertDataCategory(string ConnectionString)
{
string query = "INSERT INTO Categories(CategoryName)" +
"VALUES(@CategoryName)";
int rowsAffected = 0;
using (SqlConnection cn = new SqlConnection(ConnectionString))
while (true)
{
Console.WriteLine("Insert category Name");
string CategoryName = Console.ReadLine();
using (SqlCommand cmd = new SqlCommand(query, cn))
{
cmd.Parameters.Add("@CategoryName", SqlDbType.VarChar, 20).Value= CategoryName;
cn.Open();
rowsAffected += cmd.ExecuteNonQuery();
cn.Close();
}
Console.WriteLine("Do you want to add another category? Type y/n");
string? result = Console.ReadLine();
if (result.Equals("n"))
{
if (rowsAffected == 1)
{
Console.WriteLine("1 row affected");
}
else
{
Console.WriteLine(rowsAffected + " rows affected");
}
break;
}
}
return rowsAffected;
}
public int InsertDataProduct(string ConnectionString)
{
string query = "INSERT INTO Products(CategoryID, ProductName,ProductDescription,price,ImgPath)" +
"VALUES(@CategoryID, @ProductName, @ProductDescription, @price, @ImgPath)";
int rowsAffected = 0;
using (SqlConnection cn = new SqlConnection(ConnectionString))
while (true)
{
Console.WriteLine("Insert category Name");
string CategoryName = Console.ReadLine();
Console.WriteLine("Insert Product Name");
string ProductName = Console.ReadLine();
Console.WriteLine("Insert Product Description");
string ProductDescription = Console.ReadLine();
Console.WriteLine("Insert price");
float price = float.Parse(Console.ReadLine());
Console.WriteLine("Insert ImgPath");
string ImgPath = Console.ReadLine();
int CategoryID = getCategoryId(ConnectionString, CategoryName);
if(CategoryID == -1)
{
Console.WriteLine("Error getting Category ID.");
return -1;
}
else if (CategoryID == -2)
{
InsertDataCategory(ConnectionString);
}
using (SqlCommand cmd = new SqlCommand(query, cn))
{
cmd.Parameters.Add("@CategoryID", SqlDbType.Int).Value=CategoryID;
cmd.Parameters.Add("@ProductName", SqlDbType.VarChar, 20).Value=ProductName;
cmd.Parameters.Add("@ProductDescription", SqlDbType.VarChar, 20).Value=ProductDescription;
cmd.Parameters.Add("@price", SqlDbType.Float).Value=price;
cmd.Parameters.Add("@ImgPath", SqlDbType.VarChar, 50).Value=ImgPath;
cn.Open();
rowsAffected += cmd.ExecuteNonQuery();
cn.Close();
}
Console.WriteLine("Do you want to add another product? Type y/n");
string? result = Console.ReadLine();
if (result.Equals("n"))
{
if (rowsAffected == 1)
{
Console.WriteLine("1 row affected");
}
else
{
Console.WriteLine(rowsAffected + " rows affected");
}
break;
}
}
return rowsAffected;
}
private int getCategoryId(string ConnectionString, string categoryName)
{
string query = "SELECT ID FROM Categories WHERE CategoryName = @CategoryName";
int rowsAffected = 0;
using (SqlConnection cn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(query, cn))
{
cmd.Parameters.Add("@CategoryName", SqlDbType.VarChar, 20).Value= categoryName;
try
{
cn.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
int categoryId = reader.GetInt32(0);
return categoryId;
}
else
{
Console.WriteLine("Category not found.");
return -2;
}
}
catch (SqlException ex)
{
Console.WriteLine("Error: " + ex.Message);
return -1;
}
finally
{
cn.Close();
}
}
}
}
public void printCategories(string ConnectionString)
{
string query = "select * from Categories";
using (SqlConnection cn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(query, cn))
{
try
{
cn.Open();
SqlDataReader r = cmd.ExecuteReader();
while (r.Read()) {
Console.WriteLine("\t{0}\t{1}",
r[0], r[1]);
}
r.Close();
}
catch(SqlException ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
public void printProducts(string ConnectionString)
{
string query = "select * from Products";
using (SqlConnection cn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(query, cn))
{
try
{
cn.Open();
SqlDataReader r = cmd.ExecuteReader();
while (r.Read())
{
Console.WriteLine("\t{0}\t{1}\t{2}\t{3}\t{4}\t{5}",
r[0], r[1], r[2], r[3], r[4], r[5]);
}
r.Close();
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
}
}