-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
217 lines (174 loc) · 7.65 KB
/
Copy pathProgram.cs
File metadata and controls
217 lines (174 loc) · 7.65 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
212
213
214
215
216
217
using System;
using System.Data.SqlClient;
using static System.Console;
namespace CSharpAdoNET
{
class Program
{
static void Main(string[] args)
{
WriteLine("=================== CONTROLE DE CLIENTES ===================\n");
WriteLine("Selecione uma opção:");
WriteLine("1 - Listar");
WriteLine("2 - Cadastrar");
WriteLine("3 - Editar");
WriteLine("4 - Excluir");
WriteLine("5 - Visualizar");
int opc = Convert.ToInt32(ReadLine());
Clear();
switch (opc)
{
case 1:
Title = "Listagem de Clientes";
WriteLine("=================== LISTAGEM DE CLIENTES ===================\n");
ListarClientes();
break;
case 2:
Title = "Novo Cliente";
WriteLine("=================== NOVO CLIENTE ===================\n");
Write("Informe um nome: ");
string nome = ReadLine();
Write("Informe um email: ");
string email = ReadLine();
SalvarCliente(nome, email);
break;
case 3:
Title = "Editar Cliente";
WriteLine("=================== EDITAR CLIENTE ===================\n");
ListarClientes();
Write("Selecione um cliente pelo ID: ");
int idOpc = Convert.ToInt32(ReadLine());
(int _id, string _nome, string _email) = SelecionarCliente(idOpc);
Clear();
Title = "Editar Cliente " + _nome;
WriteLine($"=================== EDITAR CLIENTE - {_nome}===================\n");
Write("Informe um nome: ");
nome = ReadLine();
Write("Informe um email: ");
email = ReadLine();
nome = nome.Equals("") ? _nome : nome;
email = email.Equals("") ? _email : email;
SalvarCliente(nome, email, idOpc);
break;
case 4:
Title = "Excluir Cliente";
WriteLine("=================== EXCLUIR CLIENTE ===================\n");
ListarClientes();
Write("Selecione um cliente pelo ID: ");
idOpc = Convert.ToInt32(ReadLine());
(_id, _nome, _email) = SelecionarCliente(idOpc);
Clear();
Title = "Excluir Cliente - " + _nome;
WriteLine($"=================== EXCLUIR CLIENTE - {_nome} ===================\n");
WriteLine("\n************** ATENÇÃO **************\n");
WriteLine("Deseja Excluir (S - sim | N - não): ");
string confirmar = ReadLine().ToUpper();
if (confirmar.Equals("S"))
{
DeletarCliente(idOpc);
}
DeletarCliente(idOpc);
break;
case 5:
Title = "Visualizar Cliente";
WriteLine("=================== VISUALIZAR CLIENTE ===================\n");
ListarClientes();
Write("Selecione um cliente pelo ID: ");
idOpc = Convert.ToInt32(ReadLine());
(_id, _nome, _email) = SelecionarCliente(idOpc);
Clear();
Title = "Detalhes do Cliente - " + _nome;
WriteLine($"=================== DETALHES DO CLIENTE - {_nome} ===================\n");
WriteLine("ID: {0}", _id);
WriteLine("Nome: {0}", _nome);
WriteLine("E-mail: {0}", _email);
break;
default:
Title = "Opção Inválida";
WriteLine("=================== SELECIONE UMA OPÇÃO VÁLIDA ===================\n");
break;
}
ReadLine();
}
static void ListarClientes()
{
string connString = getStringConnection();
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT ID, NOME, EMAIL FROM CLIENTES ORDER BY ID";
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
WriteLine("ID: {0}", dr["id"].ToString());
WriteLine("Nome: {0}", dr["nome"].ToString());
WriteLine("E-mail: {0}", dr["email"].ToString());
WriteLine("-------------------------------------------------\n");
}
}
}
}
static void SalvarCliente(string nome, string email)
{
string connString = getStringConnection();
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO CLIENTES (NOME, EMAIL) VALUES (@nome, @email)";
cmd.Parameters.AddWithValue("@nome", nome);
cmd.Parameters.AddWithValue("@email", email);
cmd.ExecuteNonQuery();
}
}
static void SalvarCliente(string nome, string email, int id)
{
string connString = getStringConnection();
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "UPDATE CLIENTES SET NOME = @nome, EMAIL = @email WHERE ID = @id";
cmd.Parameters.AddWithValue("@nome" , nome);
cmd.Parameters.AddWithValue("@email", email);
cmd.Parameters.AddWithValue("@id" , id);
cmd.ExecuteNonQuery();
}
}
static void DeletarCliente(int id)
{
string connString = getStringConnection();
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "DELETE FROM CLIENTES WHERE ID = @id";
cmd.Parameters.AddWithValue("@id", id);
cmd.ExecuteNonQuery();
}
}
static (int, string, string) SelecionarCliente(int id)
{
string connString = getStringConnection();
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM CLIENTES WHERE ID = @id";
cmd.Parameters.AddWithValue("@id", id);
using (SqlDataReader dr = cmd.ExecuteReader())
{
dr.Read();
return (Convert.ToInt32(dr["id"].ToString()), dr["nome"].ToString(), dr["email"].ToString());
}
}
}
static string getStringConnection()
{
string connString = "Server=LAPTOP-B2ADTHUP\\SQLEXPRESS;Database=CSharpAdoNet;User Id=sa;Password=root";
return connString;
}
}
}