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

Create Database Tester: Tarea Investigar Y Definir (Metodo,)

The document contains code for creating a database called "tester" with a table called "usuario" to store user login information. It also includes a class called "cls_conexion" that handles database connections and has a method called "Validar" to validate user credentials. Finally, there is code for a login form that uses the "cls_conexion" class to validate a user on the "button1_Click" event and open a new form or display an error message.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views2 pages

Create Database Tester: Tarea Investigar Y Definir (Metodo,)

The document contains code for creating a database called "tester" with a table called "usuario" to store user login information. It also includes a class called "cls_conexion" that handles database connections and has a method called "Validar" to validate user credentials. Finally, there is code for a login form that uses the "cls_conexion" class to validate a user on the "button1_Click" event and open a new form or display an error message.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

TAREA INVESTIGAR Y DEFINIR (METODO, new SqlConnection("Data Source=?

;Initial
Catalog= ?;Integrated Security=?"), SqlCommand, SqlConnection, SqlDataReader, HasRows )

/* Query Para Login


create database tester
------------------------------------------------------------------------
Use tester
create table usuario (codigo int,id_usuario varchar(20),pass varchar(20))
------------------------------------------------------------------------
select * from usuario
--------------------------------------*/
insert into usuario values (001,'administrator','stewart')
insert into usuario values (002,'soporte','jordan')
--------------------------------------
Clase cls_conexion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
class cls_conexion
{
SqlConnection MyCon;
SqlCommand comand;
SqlDataReader reader;
public cls_conexion()
{
// User Id="";Password="";
MyCon = new SqlConnection("Data Source=INGFREDDYSANTOS\\SQLEXPRESS;Initial
Catalog=tester;Integrated Security=SSPI");
}
public SqlConnection Conectar()
{
try
{
MyCon.Open(); // abrir conexion
}
catch (Exception x) // si se produce un error los despliega
{
MessageBox.Show("Error :: " + x.ToString());
}
return MyCon; // return variable de conexion
}
public bool Validar(string user, string password) // valida usuraio
{
bool valido = false; // estatus del usuario
string query = @"SELECT id_usuario,pass from dbo.usuario where
id_usuario='" + user + "' and pass ='" + password + "'"; // query valida
usuario

comand = new SqlCommand(query, Conectar()); // ejecuta el query


reader = comand.ExecuteReader(); // asigna el resultado del select del command al reader

if (reader.HasRows == true) // si el reader tiene valor de el select anterior el estatus


es valido(true)
{
valido = true;
}

return valido; // return estatus del usuario --


}

}
}
Formulario Login

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

Boton Salir

private void button2_Click(object sender, EventArgs e)


{
if (MessageBox.Show("Esta Seguro ?", "Aviso", MessageBoxButtons.YesNo,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{
this.Close();
}

}
Boton Entrar
private void button1_Click(object sender, EventArgs e)
{
cls_conexion conexion = new cls_conexion();
if (conexion.Validar(textBox1.Text, textBox2.Text) == true)
{
this.Hide();
FrmMENU principal = new FrmMENU();
principal.Show();
}
else
{
MessageBox.Show("Usuario Incorrecto", "aviso", MessageBoxButtons.OK,
MessageBoxIcon.Error);
textBox2.Clear();
textBox1.Clear();
textBox1.Focus();
}

}
}
}

You might also like