Está en la página 1de 4

Form1

using MySql.Data.MySqlClient;

private void button1_Click(object sender, EventArgs e)


{
int resultado = -1;
MySqlConnection Conn = BdConnexion1.ObtenerConexion();
MySqlDataReader reader = null;
String nombre = textBox1.Text;
String pass = textBox2.Text;
MySqlCommand Comando = new MySqlCommand("select * from UsuariosApp
where Nombre like '" + nombre + "' and pass like '" + pass + "'", Conn);
Conn.Open();
reader = Comando.ExecuteReader();
while (reader.Read())
{
resultado = 50;
}
if (resultado > 0)
{
this.Hide();
Form2 f = new Form2();
MessageBox.Show("Bienvenida " + nombre);
f.ShowDialog();
}
else
{
MessageBox.Show("Error de datos");
Conn.Close();
}

private void button2_Click(object sender, EventArgs e)


{
this.Close();
}

-----------------------------------------------------------------------------------
--------------------------------------------------------
Form2

using MySql.Data.MySqlClient;

private void button1_Click(object sender, EventArgs e)


{
string sql = "SELECT * FROM libros WHERE Nombre_del_libro LIKE '" +
textBox1.Text + "'";
MySqlConnection Conn = BdConnexion1.ObtenerConexion();
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(sql, Conn);
MySqlCommandBuilder commandBuilder = new
MySqlCommandBuilder(dataAdapter);
DataSet ds = new DataSet();

dataAdapter.Fill(ds);
dataGridView1.ReadOnly = true;
dataGridView1.DataSource = ds.Tables[0];
}

-----------------------------------------------------------------------------------
--------------------------------------------------------
Libros

public class Libros


{
public Int64 Codigo_de_libro { get; set; }
public String Nombre_del_libro { get; set; }
public String Editorial { get; set; }
public String Autor { get; set; }
public String Genero { get; set; }
public String Pais_del_autor { get; set; }
public Int64 Numero_de_paginas { get; set; }
public Int64 Año_de_edicion { get; set; }
public Int64 Precio { get; set; }

public Libros() { }
public Libros (Int64 pCodigo_de_libro, String pNombre_del_libro, String
pEditorial, String pAutor, String pGenero, String pPais_del_autor, Int64
pNumero_de_paginas, Int64 pAño_de_edicion, Int64 pPrecio)
{
this.Codigo_de_libro = pCodigo_de_libro;
this.Nombre_del_libro = pNombre_del_libro;
this.Editorial = pEditorial;
this.Autor = pAutor;
this.Genero = pGenero;
this.Pais_del_autor = pPais_del_autor;
this.Numero_de_paginas = pNumero_de_paginas;
this.Año_de_edicion = pAño_de_edicion;
this.Precio = pPrecio;
}

-----------------------------------------------------------------------------------
--------------------------------------------------------
Consultas

using MySql.Data.MySqlClient;
using System.Data.SqlClient;

public static List<Libros> BuscarLibros(string pNombre_del_libro, string


pEditorial, string pAutor, string pGenero, string pPais_del_autor, Int64
pNumero_de_paginas, Int64 pAño_de_edicion, Int64 pPrecio)
{
List<Libros> Lista = new List<Libros>();
using (MySqlConnection Conn = BdConnexion1.ObtenerConexion())
{
MySqlCommand Comando = new MySqlCommand(string.Format(
"SELECT Codigo_de_libro, Nombre_del_libro, Editorial, Autor,
Genero, Pais_del_autor, Numero_de_paginas, Año_de_edicion, Precio FROM libros WHERE
Nombre_del_libro LIKE '%{0}%'", pNombre_del_libro), Conn);
MySqlDataReader reader = Comando.ExecuteReader();
while (reader.Read())
{
Libros pLibros = new Libros();
pLibros.Codigo_de_libro = reader.GetInt32(0);
pLibros.Nombre_del_libro = reader.GetString(1);
pLibros.Editorial = reader.GetString(2);
pLibros.Autor = reader.GetString(3);
pLibros.Genero = reader.GetString(4);
pLibros.Pais_del_autor = reader.GetString(5);
pLibros.Numero_de_paginas = reader.GetInt32(6);
pLibros.Año_de_edicion = reader.GetInt32(7);
pLibros.Precio = reader.GetInt32(8);

Lista.Add(pLibros);
}

Conn.Close();
return Lista;
}
}

-----------------------------------------------------------------------------------
--------------------------------------------------------
BdConexion

using System.Data.SqlClient;
using MySql.Data.MySqlClient;

public static MySqlConnection ObtenerConexion()


{
string server = "localhost";
string port = "3306";
string user = "root";
string password = "root";
string bd = "biblioteca";

string cadenaConexion = ("Data Source=" + server + "; port=" + port +


"; User Id=" + user + "; Password=" + password + "; Database=" + bd);

MySqlConnection Conn = new MySqlConnection(cadenaConexion);

return Conn;
}

-----------------------------------------------------------------------------------
--------------------------------------------------------
Mysql

CREATE TABLE biblioteca.UsuariosApp(


`id` int auto_increment not null,
`Nombre` Text not null,
`pass`Text Not null,
`Tipo_Usuario` Text not null,
primary key (`id`));

INSERT INTO biblioteca.UsuariosApp (`Nombre`,`pass`,`Tipo_Usuario`) Values


('Pricila','123','Administrador'),
('Lucas','456','Administrador'),
('Dante','789','Administrador'),
('Antonio','321','Usuario');

SELECT * FROM biblioteca.UsuariosApp;

También podría gustarte