-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseConnection.cs
More file actions
44 lines (33 loc) · 1.18 KB
/
DatabaseConnection.cs
File metadata and controls
44 lines (33 loc) · 1.18 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
using System.Data.SqlClient;
namespace _14_ClassesToBeTested;
public class DatabaseConnection : IDatabaseConnection
{
private const string ConnectionString =
"Data Source=MainServer;Initial Catalog=People;User ID=Admin;Password=12345;";
public Person GetById(int id)
{
Person person = null;
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
string query = "SELECT * FROM Persons WHERE ID = @ID";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@ID", id);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
int personID = (int)reader["Id"];
string firstName = reader["Name"].ToString();
string lastName = reader["LastName"].ToString();
person = new Person(personID, firstName, lastName);
}
reader.Close();
}
return person;
}
public void Write(int id, Person person)
{
throw new NotImplementedException();
}
}