-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseService.cs
104 lines (95 loc) · 3.67 KB
/
DatabaseService.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
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
using System.Data;
using Microsoft.Data.SqlClient;
namespace CaseStudy;
public class DatabaseService : IDatabaseService
{
private readonly ILogger<IDatabaseService> _Logger;
private readonly string ConnectionString;
public DatabaseService(ILogger<IDatabaseService> logger, string connectionString)
{
_Logger = logger;
ConnectionString = connectionString;
}
public void Insert(SqlTable sqlTable, IEnumerable<Customer> customers)
{
using var connection = new SqlConnection(ConnectionString);
connection.Open();
using var batch = new SqlBatch(connection);
foreach (var customer in customers)
{
var batchCommand = batch.CreateBatchCommand();
batchCommand.CommandText = $"""
INSERT INTO {sqlTable.Name} (Name, Phone, Uploaded)
VALUES (@Name, @Phone, @Uploaded)
""";
batchCommand.CommandType = CommandType.Text;
var Nameparam = batchCommand.CreateParameter();
Nameparam.ParameterName = "@Name";
Nameparam.Value = customer.Name;
batchCommand.Parameters.Add(Nameparam);
var phoneParam = batchCommand.CreateParameter();
phoneParam.ParameterName = "@Phone";
phoneParam.Value = customer.PhoneNumber;
batchCommand.Parameters.Add(phoneParam);
var uploadedParam = batchCommand.CreateParameter();
uploadedParam.ParameterName = "@Uploaded";
uploadedParam.Value = customer.Uploaded;
batchCommand.Parameters.Add(uploadedParam);
batch.BatchCommands.Add(batchCommand);
}
batch.ExecuteNonQuery();
}
public void Update(SqlTable sqlTable, IEnumerable<Customer> customers)
{
using var connection = new SqlConnection(ConnectionString);
connection.Open();
using var batch = new SqlBatch(connection);
foreach (var customer in customers)
{
var batchCommand = batch.CreateBatchCommand();
batchCommand.CommandText = $"""
UPDATE {sqlTable.Name}
SET Uploaded = 1
WHERE Id = @Id
""";
batchCommand.CommandType = CommandType.Text;
var IdParam = batchCommand.CreateParameter();
IdParam.ParameterName = "@Id";
IdParam.Value = customer.Id;
batchCommand.Parameters.Add(IdParam);
batch.BatchCommands.Add(batchCommand);
}
batch.ExecuteNonQuery();
}
public IEnumerable<Customer> Get(SqlTable sqlTable)
{
using var connection = new SqlConnection(ConnectionString);
connection.Open();
var query = $"""
SELECT
*
FROM
{sqlTable.Name}
WHERE Uploaded = 0
""";
using var command = new SqlCommand(query, connection);
using var reader = command.ExecuteReader();
var l = new List<Customer>();
while (reader.Read())
{
int id = (int)reader["id"];
string name = (string)reader["name"];
string phone = (string)reader["phone"];
bool uploaded = (bool)reader["uploaded"];
var c = new Customer()
{
Id = id,
Name = name,
PhoneNumber = phone,
Uploaded = uploaded
};
l.Add(c);
}
return l;
}
}