Sie sind auf Seite 1von 4

UNIVERSIDAD SALESIANA DE BOLIVIA

INGENIERIA DE SISTEMAS


DATA ADAPTER


Docente: Cyntia Ortega
Materia: Base de datos 2
Estudiante: Gustavo Choque M.
Fecha de entregra: 28/05/14





Cochabamba Bolivia
1.- Implementacion del dataAdapter


using System.Data.OleDb;

namespace modificar
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//creamos una variable que trabaje con el dataAdapter
private DataSet ds;
OleDbConnection con;
private void Form1_Load(object sender, EventArgs e)
{
//inicializamos una instancia de dataSet
ds = new DataSet();
con = new OleDbConnection();
//definir la cadena de la conexion
con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Users\HOME\Documents\Pollos America.accdb";
//abrir la conexion
con.Open();
}
//para verificar si hay tablas
private void limpiarDatos()
{
if (ds.Tables.Count > 0)
{
ds.Tables["Clientes"].Rows.Clear();
}
}

private void button2_Click(object sender, EventArgs e)
{
limpiarDatos();
//levantamos instancia del dataAdapter y en el momento de de levantar la instancia se
incluye la consulta a la bd
OleDbDataAdapter da = new OleDbDataAdapter("select * from Cliente", con);
//llamamos al dataFill para que se abra conexion
try
{
da.Fill(ds, "Clientes");
dgClientes.DataSource = ds.Tables["Clientes"];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//funcion para modificar un cliente
void modificar()
{
//volvemos a levantar la instancia del dataAdapter incluyendo la consulta
OleDbDataAdapter da = new OleDbDataAdapter("update Cliente set Nombre='" +
textBox2.Text + "', Apellido ='" + textBox3.Text + "' where NIT=" + textBox1.Text, con);
try
{
//abre la conexion
da.Fill(ds, "Clientes");
dgClientes.DataSource = ds.Tables["Clientes"];
MessageBox.Show("registro modificado");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void button1_Click(object sender, EventArgs e)
{
modificar();
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
}

private void dgClientes_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int filaSeleccionada = dgClientes.CurrentRow.Index;
string NIT = Convert.ToString(dgClientes.Rows[filaSeleccionada].Cells[0].Value);
string sql = "Select * from Cliente where NIT=" + NIT;
OleDbCommand com = new OleDbCommand(sql, con);
OleDbDataReader reader;
reader = com.ExecuteReader();
if (reader.Read())
{
textBox1.Text = reader[0].ToString();
textBox2.Text = reader[1].ToString();
textBox3.Text = reader[2].ToString();
}
}
}
}

Das könnte Ihnen auch gefallen