-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeacherRepository.cs
61 lines (53 loc) · 1.76 KB
/
TeacherRepository.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
using DBLayer;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
namespace Evaluation_Manager
{
public static class TeacherRepository
{
public static Teacher GetTeacher(string username)
{
string sql = $"SELECT * FROM Teachers WHERE Username = '{username}'";
return FetchTeacher(sql);
}
public static Teacher GetTeacher(int id)
{
string sql = $"SELECT * FROM Teachers WHERE Id = '{id}'";
return FetchTeacher(sql);
}
private static Teacher FetchTeacher(string sql)
{
DB.OpenConnection();
var reader = DB.GetDataReader(sql);
Teacher teacher = null;
if (reader.HasRows == true)
{
reader.Read();
teacher = CreateObject(reader);
reader.Close();
}
DB.CloseConnection();
return teacher;
}
private static Teacher CreateObject(SqlDataReader reader)
{
int id = int.Parse(reader["Id"].ToString());
string firstName = reader["FirstName"].ToString();
string lastName = reader["LastName"].ToString();
string username = reader["Username"].ToString();
string password = reader["Password"].ToString();
var teacher = new Teacher();
teacher.Id = id;
teacher.FirstName = firstName;
teacher.LastName = lastName;
teacher.Username = username;
teacher.Password = password;
return teacher;
}
}
}