-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccount.cs
129 lines (116 loc) · 4.52 KB
/
Account.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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
namespace BusReservationSystem.Classes
{
public class Account
{
//Properties
public int User_id { get; set; }
public string Full_name { get; set; }
public string Password { get; set; }
public string Phone_no { get; set; }
public string Counter { get; set; }
public int Role { get; set; }
//Connection String [for connecting with database]
private readonly string conStr = ConfigurationManager.ConnectionStrings["BTRS_Db"].ConnectionString;
//Login Method
public bool Login(string phoneNum, string password)
{
bool isSuccess = false;
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "SELECT [user_id],[full_name],[counter],[role] FROM [User] WHERE [phone_no] = @phone_no AND [password] = @password;";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@phone_no", phoneNum);
cmd.Parameters.AddWithValue("@password", password);
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
dataAdapter.Fill(dt);
int row = dt.Rows.Count;
if (row > 0)
{
LogInfo.user_id = int.Parse(dt.Rows[0][0].ToString());
LogInfo.user_name = dt.Rows[0][1].ToString();
LogInfo.user_role = int.Parse(dt.Rows[0][3].ToString());
LogInfo.user_counter = dt.Rows[0][2].ToString();
isSuccess = true;
}
else
{
isSuccess = false;
}
}
return isSuccess;
}
//Register Method
public bool Register()
{
//System.Windows.Forms.MessageBox.Show(Properties.PropertyName<Account>(x => x.user_id));
bool isSuccess = false;
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "INSERT INTO [User]([full_name],[phone_no] ,[password],[counter],[role]) VALUES (@fullName, @phone, @pass, @counter, @role)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@fullName", Full_name);
cmd.Parameters.AddWithValue("@phone", Phone_no);
cmd.Parameters.AddWithValue("@pass", Password);
cmd.Parameters.AddWithValue("@counter", Counter);
cmd.Parameters.AddWithValue("@role", Role);
con.Open();
if (cmd.ExecuteNonQuery() > 0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}
}
return isSuccess;
}
//Get Roles
public DataTable GetRoles()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "SELECT * FROM [Role]";
SqlCommand command = new SqlCommand(sql, con);
SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
dataAdapter.Fill(dt);
}
return dt;
}
//Get Counters
public DataTable GetConters()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "SELECT * FROM [counter]";
SqlCommand command = new SqlCommand(sql, con);
SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
dataAdapter.Fill(dt);
}
return dt;
}
public DataTable GetConters(string search)
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "SELECT * FROM [counter] where [counter].[name] like '@searchText%'";
SqlCommand command = new SqlCommand(sql, con);
command.Parameters.AddWithValue("@searchText", search);
SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
dataAdapter.Fill(dt);
}
return dt;
}
}
}