0% found this document useful (0 votes)
55 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)
55 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

//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())
{
[Link]();
using (var command = new SqlCommand())
{
[Link] = connection;
[Link] = "select *from Users where LoginName=@user and Password=@pass";
[Link]("@user", user);
[Link]("@pass", pass);
[Link] = [Link];
SqlDataReader reader = [Link]();
if ([Link])
{
while ([Link]())
{
[Link] = reader.GetInt32(0);
[Link] = [Link](3);
[Link] = [Link](4);
[Link] = [Link](5);
[Link] = [Link](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 [Link](user,pass);
}
}
//PRESENTATION LAYER/ CAPA DE PRESENTACION
private void btnlogin_Click(object sender, EventArgs e)
{
if ([Link] != "Username" && [Link]>2)
{
if ([Link] != "Password")
{
UserModel user = new UserModel();
var validLogin = [Link]([Link], [Link]);
if (validLogin == true)
{
FormPrincipal mainMenu = new FormPrincipal();
[Link]("Welcome "+[Link]+", "+[Link]);
[Link]();
[Link] += Logout;
[Link]();
}
else {
msgError("Incorrect username or password entered. \n Please try again.");
[Link] ="Password";
[Link] = false;
[Link]();
}
}
else msgError("Please enter password.");
}
else msgError("Please enter username.");
}
private void msgError(string msg)
{
[Link] = " " + msg;
[Link] = true;
}
private void Logout(object sender, FormClosedEventArgs e) {
[Link] = "Password";
[Link] = false;
[Link] = "Username";
[Link] = false;
[Link]();
}

//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