-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDapperExemplo.cs
More file actions
92 lines (77 loc) · 2.2 KB
/
DapperExemplo.cs
File metadata and controls
92 lines (77 loc) · 2.2 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
using Dapper;
using Lesson_DotNet_Dapper.Entities;
using System.Data.SqlClient;
namespace Lesson_DotNet_Dapper;
public class DapperExemplo
{
private string _connectionString;
public DapperExemplo(string connectionString)
{
_connectionString = connectionString;
}
public IEnumerable<Usuario> ListarUsuarios()
{
var queryCommand = @"
SELECT
UsuarioID,
Nome,
Sobrenome,
Email,
DataCriacao
FROM dbo.tblUsuario";
using var connection = new SqlConnection(_connectionString);
return connection.Query<Usuario>(queryCommand);
}
public Usuario? ObterUsuario(int usuarioID)
{
var queryCommand = @"
SELECT
UsuarioID,
Nome,
Sobrenome,
Email,
DataCriacao
FROM dbo.tblUsuario
WHERE
UsuarioID = @UsuarioID";
using var connection = new SqlConnection(_connectionString);
return connection.QueryFirstOrDefault<Usuario>(
queryCommand,
new
{
UsuarioID = usuarioID
});
}
public void InserirUsuario(string nome, string sobrenome, string email)
{
var insertCommand = @"
INSERT INTO dbo.tblUsuario(Nome, Sobrenome, Email)
VALUES(@Nome, @Sobrenome, @Email)";
using var connection = new SqlConnection(_connectionString);
connection.Execute(
insertCommand,
new
{
Nome = nome,
Sobrenome = sobrenome,
Email = email
});
}
public void AtualizarEmailUsuario(int usuarioID, string email)
{
var updateCommand = @"
UPDATE dbo.tblUsuario
SET
Email = @Email
WHERE
UsuarioID = @UsuarioID";
using var connection = new SqlConnection(_connectionString);
connection.Execute(
updateCommand,
new
{
UsuarioID = usuarioID,
Email = email
});
}
}