All pastes #190425 Raw Edit

Miscellany

public text v1 · immutable
·published 2006-10-03 23:37 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using System.Data.Common;
using agenda.Properties;

namespace agenda {
    public partial class Agenda : Form {

        DataSet oDS;
        DbDataAdapter oDA;
        DbProviderFactory dbpf = DbProviderFactories.GetFactory("System.Data.OleDb");
        DbConnection oConn;

        DataRowView currentRow;

        private status stat = status.select;

        private enum status {
            update,
            insert,
            delete,
            select
        }

        public Agenda() {
            InitializeComponent();
        }

        private void Agenda_Load(object sender, EventArgs e) {

            oConn = dbpf.CreateConnection();
            oConn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=bd.mdb";

            oDA = dbpf.CreateDataAdapter();

            // creamos oDA.SelectCommand ect ...
            createQuery();

            oDS = new DataSet();
            oDA.Fill(oDS, "Direcciones");

            DataTable tbl = oDS.Tables["Direcciones"];

            listBox1.DataSource = tbl;
            listBox1.DisplayMember = "Apellidos";
            listBox1.ValueMember = "Id";

            foreach(DataColumn col in tbl.Columns) {
                if(col.ColumnName != "Id") comboBoxShearch.Items.Add(col.ColumnName);
            }

            comboBoxShearch.SelectedIndex = 0;

        }

        private void FillControls() {
            textNombre.Text = (string) currentRow.Row["Nombre"];
            textApellidos.Text = (string) currentRow.Row["Apellidos"];
            textDire.Text = (string) currentRow.Row["Dirección"];
            textCiudad.Text = (string) currentRow.Row["Ciudad"];
            textTlf.Text = (string) currentRow.Row["Telefono"];
            textBoxNotas.Text = (string) currentRow.Row["Notas"];
        }

        private void save() {
            
            DataSet tmp = oDS.Copy();
            try {
                switch(stat) {
                    case status.update:
                        LoadRowData(currentRow.Row);
                        break;
                    case status.insert:
                        DataRow row = oDS.Tables["Direcciones"].NewRow();
                        LoadRowData(row);
                        oDS.Tables["Direcciones"].Rows.Add(row);
                        break;
                    case status.delete:
                        currentRow.Row.Delete();
                        break;
                }
                // guardamos en la base de datos
                int num = oDA.Update(oDS.Tables["Direcciones"]);
                Console.WriteLine("{0} filas afectadas",num);
            }catch(Exception ex){
                MessageBox.Show("Error en la interface entre el teclado y el form !"+Environment.NewLine+ex.Message);
                oDS.RejectChanges();
                listBox1.SelectedItem = currentRow;
                FillControls();
            }
        }

        private void Clear() {
            textNombre.Text = "";
            textApellidos.Text = "";
            textDire.Text = "";
            textCiudad.Text = "";
            textTlf.Text = "";
            textBoxNotas.Text = "";
        }

        private void LoadRowData(DataRow row) {
            row["Nombre"] = textNombre.Text;
            row["Apellidos"] = textApellidos.Text;
            row["Dirección"] = textDire.Text;
            row["Ciudad"] = textCiudad.Text;
            row["Telefono"] = textTlf.Text;
            row["Notas"] = textBoxNotas.Text;
        }

        private void createQuery() {

            #region BD select
            oDA.SelectCommand = oConn.CreateCommand();
            oDA.SelectCommand.CommandText = "SELECT Id, Nombre, Apellidos, Dirección, Ciudad, Telefono, Notas FROM Direcciones";
            #endregion

            #region BD update

            oDA.UpdateCommand = oConn.CreateCommand();
            oDA.UpdateCommand.CommandText = "UPDATE Direcciones SET Nombre = ?, Apellidos = ?, Dirección = ?, Ciudad = ?, Telefono = ?, Notas = ? WHERE (Id = ?)";

            DbParameter pNombre = oDA.UpdateCommand.CreateParameter();
            pNombre.SourceColumn = "Nombre";
            oDA.UpdateCommand.Parameters.Add(pNombre);

            DbParameter pApellidos = oDA.UpdateCommand.CreateParameter();
            pApellidos.SourceColumn = "Apellidos";
            oDA.UpdateCommand.Parameters.Add(pApellidos);

            DbParameter pDirección = oDA.UpdateCommand.CreateParameter();
            pDirección.SourceColumn = "Dirección";
            oDA.UpdateCommand.Parameters.Add(pDirección);

            DbParameter pCiudad = oDA.UpdateCommand.CreateParameter();
            pCiudad.SourceColumn = "Ciudad";
            oDA.UpdateCommand.Parameters.Add(pCiudad);

            DbParameter pTelefono = oDA.UpdateCommand.CreateParameter();
            pTelefono.SourceColumn = "Telefono";
            oDA.UpdateCommand.Parameters.Add(pTelefono);

            DbParameter pNotas = oDA.UpdateCommand.CreateParameter();
            pNotas.SourceColumn = "Notas";
            oDA.UpdateCommand.Parameters.Add(pNotas);


            DbParameter pId = oDA.UpdateCommand.CreateParameter();
            pId.SourceColumn = "Id";
            oDA.UpdateCommand.Parameters.Add(pId);
            #endregion

            #region BD insert
            oDA.InsertCommand = oConn.CreateCommand();
            oDA.InsertCommand.CommandText = "INSERT INTO Direcciones (Nombre, Apellidos, Dirección, Ciudad, Telefono, Notas) VALUES (?, ?, ?, ?, ?, ?)";

            DbParameter pInsertNombre = oDA.InsertCommand.CreateParameter();
            pInsertNombre.SourceColumn = "Nombre";
            oDA.InsertCommand.Parameters.Add(pInsertNombre);

            DbParameter pInsertApellidos = oDA.InsertCommand.CreateParameter();
            pInsertApellidos.SourceColumn = "Apellidos";
            oDA.InsertCommand.Parameters.Add(pInsertApellidos);

            DbParameter pInsertDirección = oDA.InsertCommand.CreateParameter();
            pInsertDirección.SourceColumn = "Dirección";
            oDA.InsertCommand.Parameters.Add(pInsertDirección);


            DbParameter pInsertCiudad = oDA.InsertCommand.CreateParameter();
            pInsertCiudad.SourceColumn = "Ciudad";
            oDA.InsertCommand.Parameters.Add(pInsertCiudad);

            DbParameter pInsertTelefono = oDA.InsertCommand.CreateParameter();
            pInsertTelefono.SourceColumn = "Telefono";
            oDA.InsertCommand.Parameters.Add(pInsertTelefono);

            DbParameter pInsertNotas = oDA.InsertCommand.CreateParameter();
            pInsertNotas.SourceColumn = "Notas";
            oDA.InsertCommand.Parameters.Add(pInsertNotas);
            #endregion

            #region DB delete
            oDA.DeleteCommand = oConn.CreateCommand();
            oDA.DeleteCommand.CommandText = "DELETE FROM Direcciones WHERE (Id = ?)";

            DbParameter pDeleteId = oDA.DeleteCommand.CreateParameter();
            pDeleteId.SourceColumn = "Id";
            oDA.DeleteCommand.Parameters.Add(pDeleteId);
            #endregion

        }

        private void buttonDelete_Click(object sender, EventArgs e) {
            stat = status.delete;
            save();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
            DataRowView tmpRow = (DataRowView)listBox1.SelectedItem;
            if(tmpRow != null && tmpRow != currentRow) {
                currentRow = tmpRow;
                Console.WriteLine("Item seleccionado {0}", currentRow.Row["Id"]);
                FillControls();
                stat = status.update;
            }
        }

        private void buttonNew_Click(object sender, EventArgs e) {
            Clear();
            stat = status.insert;
        }

        private void buttonSave_Click(object sender, EventArgs e) {
            save();
        }

        private void buttonShearch_Click(object sender, EventArgs e) {
            if(comboBoxShearch.SelectedItem != null) {

                oDA.SelectCommand.CommandText = 
                    "SELECT Id, Nombre, Apellidos, Dirección, Ciudad, Telefono, Notas FROM Direcciones WHERE ("+
                    (string)comboBoxShearch.SelectedItem+" LIKE '%"+
                    textBoxShearch.Text+"%')";

                oDS.Tables["Direcciones"].Clear();
                
                oDA.Fill(oDS,"Direcciones");
                if(listBox1.Items.Count > 0) {
                    listBox1.SelectedIndex = 0;
                    FillControls();
                }
            }
        }

    }
}