Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
50 views2 pages

data Access Layer/ Capa de Acceso A Datos: Connectiontosql

The document defines classes for connecting to a SQL database and authenticating users. A ConnectionToSql class establishes database connections. A UserDao class contains a Login method that validates credentials by querying the Users table. A UserModel calls the Login method. A form handles login - it calls LoginUser, displays a welcome message on success or an error on failure, and stores user details in a UserLoginCache class.

Uploaded by

Gerson Sanchez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views2 pages

data Access Layer/ Capa de Acceso A Datos: Connectiontosql

The document defines classes for connecting to a SQL database and authenticating users. A ConnectionToSql class establishes database connections. A UserDao class contains a Login method that validates credentials by querying the Users table. A UserModel calls the Login method. A form handles login - it calls LoginUser, displays a welcome message on success or an error on failure, and stores user details in a UserLoginCache class.

Uploaded by

Gerson Sanchez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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; }
}

You might also like