-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBancoDeDados.cs
51 lines (45 loc) · 1.63 KB
/
BancoDeDados.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
using System;
using Microsoft.Data.SqlClient;
namespace CultivaTechPoc
{
internal class BancoDeDados
{
private readonly string _connectionString;
public BancoDeDados(string connectionString)
{
_connectionString = connectionString;
}
public Usuarios ValidarCredenciais(string email, string senha)
{
string query = "SELECT id, nome, email, tipo FROM Usuarios WHERE email = @Email AND senha = @Senha";
using (SqlConnection connection = new SqlConnection(_connectionString))
{
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@Email", email);
command.Parameters.AddWithValue("@Senha", senha);
try
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
return new Usuarios
{
Id = reader.GetInt32(0),
Nome = reader.GetString(1),
Email = reader.GetString(2),
Tipo = reader.GetString(3)
};
}
}
}
catch (Exception ex)
{
Console.WriteLine("Erro ao conectar ao banco de dados: " + ex.Message);
}
}
return null;
}
}
}