Пример #1
0
        private void btnAcceder_Click(object sender, EventArgs e)
        {
            if (txtNom.Text == "" || txtPass.Text == "")
            {
                txtDatosIncorrectos.Visible = false;
                txtIngresarDatos.Visible    = true;
            }


            else
            {
                //conectar a bd

                try
                {
                    // guardar variables
                    string nom  = txtNom.Text;
                    string pass = txtPass.Text;
                    // se abre la conexion
                    conexion.Open();
                    // select para ver si el usuario existe en la bd y esta bien la contraseña
                    string consulta = "SELECT * FROM USUARIOS WHERE nombreUsuario = '" + nom + "' and contraseña = HASHBYTES('SHA1','" + pass + "')";

                    SqlCommand    comando = new SqlCommand(consulta, conexion);
                    SqlDataReader lector  = comando.ExecuteReader();
                    // si hay un elemento en el select
                    if (lector.Read())
                    {
                        string nombre = lector.GetString(0);
                        string contra = lector.GetString(1);
                        if (nombre != "" && contra != "")
                        {
                            // codigo de ebert
                            conexion.Close();

                            txtIngresarDatos.Visible = false;
                            frmInicio ventana = new frmInicio();
                            ventana.Show();
                            this.Hide();
                        }
                    }
                    else
                    {
                        txtIngresarDatos.Visible    = false;
                        txtDatosIncorrectos.Visible = true;
                        conexion.Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("" + ex);
                    conexion.Close();
                }
            }
        }