Sunteți pe pagina 1din 7

Introducción a ADO.

NET

Javier Serrano

Programación .NET II

Instituto IACC

14-07-2019
Desarrollo

Ejecución.

Cargar archivo XML, con listado de actores.


Lista en Combobox.

Selección de actor en combobox y evento SelectIndexChanged hace la llamada con el id del

actor a la base de datos para consultar sus pelicuasl y ser cargados en un ListBox.
Al seleccionar alguna película del ListBox, se cargará la información de la selección, también

utilizando su evento SelectIndexChanged

Codigo Fuente

using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;

namespace ArchivosXML
{
public partial class Form1 : Form
{

public string stringConexion =


"DataSource=localhost;Database=sakila;Uid=root;Pwd=root;";
private MySqlConnection conexion;
public Form1()
{
InitializeComponent();
cbActores.Enabled = false;
conexion = new MySqlConnection(stringConexion);
}

private void BtnCargarXml_Click(object sender, EventArgs e)


{

OpenFileDialog op = new OpenFileDialog();


op.Filter = "Archivo XML |*.xml";
if (op.ShowDialog() == DialogResult.OK)
{
string rutaXml = op.FileName;
var documento = new XmlDocument();
XmlNodeList listaDocumento;

documento.Load(rutaXml);
listaDocumento = documento.SelectNodes("/actors/actor");
cbActores.DisplayMember = "Text";
cbActores.ValueMember = "Value";
cbActores.Text = "--Seleccionar Actor--";

cbActores.Enabled = true;
foreach (XmlNode nodo in listaDocumento)
{

var id = nodo.ChildNodes.Item(0).InnerText;
var nombre = nodo.ChildNodes.Item(1).InnerText;
var apellido = nodo.ChildNodes.Item(2).InnerText;
cbActores.Items.Add(new KeyValuePair<string, string>($"{id}",
$"{nombre} {apellido}"));

}
}

private void CbActores_SelectedIndexChanged(object sender, EventArgs e)


{
if (cbActores.SelectedItem != null)
{
KeyValuePair<string, string> seleccion = (KeyValuePair<string,
string>)cbActores.SelectedItem;

string query = $"select f.title from film f inner join film_actor fa on


f.film_id = fa.film_id where fa.actor_id = {seleccion.Key}";

ConsultaPeliculas(query);
}
}

public void ConsultaPeliculas(string query)


{
lbPeliculas.Items.Clear();

var comando = new MySqlCommand(query, conexion);


conexion.Open();

MySqlDataReader resultado = comando.ExecuteReader();


lbPeliculas.Enabled = true;
while (resultado.Read())
{
lbPeliculas.Items.Add(resultado[0]);
}

conexion.Close();
}

public void ConsultaPeliculaDetalle(string query)


{
lbIdiomas.Items.Clear();
lbCategorias.Items.Clear();
var comando = new MySqlCommand(query, conexion);
conexion.Open();

MySqlDataReader resultado = comando.ExecuteReader();


//lbPeliculas.Enabled = true;
while (resultado.Read())
{
txtDescripcion.Text = resultado["description"].ToString();
txtLanzamiento.Text = resultado["release_year"].ToString();
txtDuracion.Text = resultado["length"].ToString();
txtPuntuacion.Text = resultado["rating"].ToString();
txtCaracteristicas.Text = resultado["special_features"].ToString();
lbCategorias.Items.Add(resultado["categoria"]);
lbIdiomas.Items.Add(resultado["idioma"]);
}

conexion.Close();
}

private void LbPeliculas_SelectedIndexChanged(object sender, EventArgs e)


{

string peliculaSeleccion = lbPeliculas.SelectedItem.ToString();


string query = $"select c.name as categoria,
description,release_year,length,rating,special_features, l.name as idioma "
+ "from film f inner join language l on f.language_id =
l.language_id "
+ "inner join film_category fc on f.film_id = fc.film_id "
+ "inner join category c on fc.category_id = c.category_id "
+ $"where title = '{peliculaSeleccion}'";
ConsultaPeliculaDetalle(query);
}
}
}
Bibliografía

 Contenidos Semana 5.

S-ar putea să vă placă și