Sei sulla pagina 1di 2

//DATA ACCESS LAYER/ CAPA DE ACCESO A DATOS

public abstract class ConnectionToSql


{
private readonly string connectionString;

public ConnectionToSql() {
connectionString = "Server=DESKTOP-4FVONF2\\RJCODES;DataBase= MyCompany; integrated security=
true";
}
protected SqlConnection GetConnection() {
return new SqlConnection(connectionString);
}
}

public class UserDao : ConnectionToSql


{
public bool Login(string user, string pass)
{
using (var connection = GetConnection())
{
connection.Open();
using (var command = new SqlCommand())
{
command.Connection = connection;
command.CommandText = "select *from Users where LoginName=@user and Password=@pass";
command.Parameters.AddWithValue("@user", user);
command.Parameters.AddWithValue("@pass", pass);
command.CommandType = CommandType.Text;
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
UserLoginCache.IdUser = reader.GetInt32(0);
UserLoginCache.FirstName = reader.GetString(3);
UserLoginCache.LastName = reader.GetString(4);
UserLoginCache.Position = reader.GetString(5);
UserLoginCache.Email = reader.GetString(6);
}
return true;
}
else
return false;
}
}

}
}

//DOMAIN LAYER/ CAPA DE DOMINIO O NEGOCIO


public class UserModel
{
UserDao userDao = new UserDao();
public bool LoginUser(string user, string pass) {
return userDao.Login(user,pass);
}
}
//PRESENTATION LAYER/ CAPA DE PRESENTACION
private void btnlogin_Click(object sender, EventArgs e)
{
if (txtuser.Text != "Username" && txtuser.TextLength>2)
{
if (txtpass.Text != "Password")
{
UserModel user = new UserModel();
var validLogin = user.LoginUser(txtuser.Text, txtpass.Text);
if (validLogin == true)
{
FormPrincipal mainMenu = new FormPrincipal();
MessageBox.Show("Welcome "+UserLoginCache.FirstName+", "+UserLoginCache.LastName);
mainMenu.Show();
mainMenu.FormClosed += Logout;
this.Hide();
}
else {
msgError("Incorrect username or password entered. \n Please try again.");
txtpass.Text ="Password";
txtpass.UseSystemPasswordChar = false;
txtuser.Focus();
}
}
else msgError("Please enter password.");
}
else msgError("Please enter username.");
}
private void msgError(string msg)
{
lblErrorMessage.Text = " " + msg;
lblErrorMessage.Visible = true;
}
private void Logout(object sender, FormClosedEventArgs e) {
txtpass.Text = "Password";
txtpass.UseSystemPasswordChar = false;
txtuser.Text = "Username";
lblErrorMessage.Visible = false;
this.Show();
}

//COMMON-SUPPORT LAYER/ CAPA COMUN DE SOPORTE


public static class UserLoginCache
{
public static int IdUser { get; set; }
public static string FirstName { get; set; }
public static string LastName { get; set; }
public static string Position { get; set; }
public static string Email { get; set; }
}

Potrebbero piacerti anche