-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataAccess.cs
199 lines (165 loc) · 8.16 KB
/
DataAccess.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Configuration;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Threading.Tasks;
using Phonebook.models;
using AutoMapper;
namespace Phonebook
{
public class DataAccess : IDataAccess
{
private readonly string connectionString = ConfigurationManager.ConnectionStrings["Phonebook.Properties.Settings.Setting"].ConnectionString;
public int SearchPerson(PersonDTO personDTO)
{
using(var connectionSearch = new SqlConnection(connectionString))
{
connectionSearch.Open();
string Search = "select PersonId from Person where NameSurname Like '%'+ @NameSurname +'%'";
SqlCommand SearchCommand = new SqlCommand(Search, connectionSearch);
SearchCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@NameSurname", SqlDbType.NVarChar, 250) { Value = personDTO.NameSurname });
var findPersonId = SearchCommand.ExecuteScalar();
if (findPersonId == null)
{
System.Windows.Forms.MessageBox.Show("An error occurred while retrieving the registered customer's ID.");
return 0;
}
else
{
if (!int.TryParse(findPersonId.ToString(), out int _PersonId))
{
System.Windows.Forms.MessageBox.Show("An error occurred while retrieving the registered customer's ID.");
}
return _PersonId;
}
connectionSearch.Close();
}
}
public void DeletePerson(PersonDTO personDTO)
{
using(var connectionDelete = new SqlConnection(connectionString))
{
connectionDelete.Open();
string Delete = "Delete from Person where PersonId = @PersonId";
SqlCommand DeleteCommand = new SqlCommand(Delete, connectionDelete);
DeleteCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@PersonId", SqlDbType.Int) { Value = personDTO.PersonId});
DeleteCommand.ExecuteNonQuery();
connectionDelete.Close();
}
}
public void RegisterDatabase(PersonDTO personDTO)
{
using (var connectionRegister = new SqlConnection(connectionString))
{
connectionRegister.Open();
string registerPerson = "Insert into Person(NameSurname, Number) values(@NameSurname, @Number)";
SqlCommand sqlCommand = new SqlCommand(registerPerson, connectionRegister);
sqlCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@NameSurname", SqlDbType.NChar, 250) { Value = personDTO.NameSurname });
sqlCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Number", SqlDbType.VarChar, 16) { Value = personDTO.Number });
sqlCommand.ExecuteNonQuery();
connectionRegister.Close();
}
}
public PersonDTO MapperSearchForUpdatePerson(Person person)
{
var config = new MapperConfiguration(cfg => cfg.CreateMap<Person, PersonDTO>());
var mapper = new Mapper(config);
var personDTO = mapper.Map<PersonDTO>(person);
return personDTO;
}
public PersonDTO SearchByNameNumber(string _nameSurname, string _number)
{
using (var connectionSearch = new SqlConnection(connectionString))
{
connectionSearch.Open();
Person person = new Person();
string SearchQuery;
if(_number == null)
{
SearchQuery = "SELECT TOP 1 PersonId, NameSurname, Number FROM Person WHERE NameSurname LIKE '%'+ @NameSurname +'%'";
}
else
{
SearchQuery = "SELECT TOP 1 PersonId, NameSurname, Number FROM Person WHERE Number = @Number";
}
var cmd = new SqlCommand(SearchQuery, connectionSearch);
if(_number == null)
{
cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@NameSurname", SqlDbType.NVarChar, 250) { Value = _nameSurname });
}
else
{
cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Number", SqlDbType.VarChar, 16) { Value = _number });
}
var reader = cmd.ExecuteReader();
if(!reader.HasRows)
{
System.Windows.Forms.MessageBox.Show("No record found named " + _nameSurname + ". Please try again");
}
else
{
while(reader.Read())
{
if (!int.TryParse(reader["PersonId"].ToString().ToString(), out int _PersonId))
{
System.Windows.Forms.MessageBox.Show("An error occurred while retrieving the registered customer's ID.");
}
person.PersonId = _PersonId;
person.NameSurname = reader["NameSurname"].ToString();
person.Number = reader["Number"].ToString();
}
}
reader.Close();
connectionSearch.Close();
return MapperSearchForUpdatePerson(person);
}
}
public void UpdatePerson(PersonDTO personDTO)
{
using(var ConnectionUpdate = new SqlConnection(connectionString))
{
ConnectionUpdate.Open();
string UpdateQuery = "Update Person SET NameSurname = @NameSurname , Number = @Number where PersonId = @PersonId ";
var cmd = new SqlCommand(UpdateQuery, ConnectionUpdate);
cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@PersonId", SqlDbType.Int) { Value = personDTO.PersonId });
cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@NameSurname", SqlDbType.NChar, 250) { Value = personDTO.NameSurname });
cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Number", SqlDbType.VarChar, 16) { Value = personDTO.Number });
cmd.ExecuteNonQuery();
ConnectionUpdate.Close();
}
}
public List<PersonDTO> MapperListingPhonebook(List<Person> person)
{
var config = new MapperConfiguration(cfg => cfg.CreateMap<Person, PersonDTO>());
var mapper = new Mapper(config);
List<PersonDTO> personDto = mapper.Map<List<Person>, List<PersonDTO>>(person);
return personDto;
}
public List<PersonDTO> ListingPhonebook()
{
using(var connectionList= new SqlConnection(connectionString))
{
connectionList.Open();
string ListingPhonebookQuery = "SELECT PersonId, NameSurname, Number FROM Person";
List<Person> personList = new List<Person>();
var cmd = new SqlCommand(ListingPhonebookQuery, connectionList);
var reader = cmd.ExecuteReader();
while(reader.Read())
{
if (!int.TryParse(reader["PersonId"].ToString().ToString(), out int _PersonId))
{
System.Windows.Forms.MessageBox.Show("An error occurred while retrieving the registered customer's ID.");
}
personList.Add(new Person { PersonId = _PersonId, NameSurname = reader["NameSurname"].ToString(), Number = reader["Number"].ToString() });
}
return MapperListingPhonebook(personList);
reader.Close();
connectionList.Close();
}
}
}
}