0% found this document useful (0 votes)
142 views7 pages

8 AGENDA WappC# Prog Inserta, Consulta, Act

This document contains code for creating a phone book application in C# with modules for data capture, consultation, and updating. It includes code to create a database and table in SQL, classes for database connection and data manipulation, and forms for insertion, search, and updating of contact data. The classes handle connecting to the database, executing queries, and the forms provide user interfaces for the different CRUD operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views7 pages

8 AGENDA WappC# Prog Inserta, Consulta, Act

This document contains code for creating a phone book application in C# with modules for data capture, consultation, and updating. It includes code to create a database and table in SQL, classes for database connection and data manipulation, and forms for insertion, search, and updating of contact data. The classes handle connecting to the database, executing queries, and the forms provide user interfaces for the different CRUD operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

CREACION DE UNA AGENDA TELEFONICA EN C# (MODULOS DE:

CAPTURA, CONSULTA Y ACTUALIZACION DE DATOS) Wapp


/*paso crear EN SQL,l query*/
/* query 1*/
create database agendadb
---------------------------------
/* query 2*/
USE [agendadb]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[agenda]') AND
type in (N'U'))
BEGIN
CREATE TABLE [dbo].[agenda](
[codigo] [int] IDENTITY(1,1) NOT NULL,
[nombre] [varchar](50) NOT NULL,
[apellido] [varchar](50) NOT NULL,
[telefono] [varchar](13) NOT NULL
) ON [PRIMARY]
END
-------------------------------------------------
/* quey 3*/
select * from agenda
-------------------------------------------------

_CREANDO LA CLASE cls_conexion______________________________________________________________

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace WindowsFormsApplication2
{
class cls_conexion
{
SqlConnection sqlcon;

public cls_conexion()
{
sqlcon = new SqlConnection("Data Source=INGFREDDYSANTOS\\SQLEXPRESS;Initial
Catalog=agendadb;Integrated Security=True");

public SqlConnection sql_conectar()


{
try
{
sqlcon.Open();
}
catch
{

}
return sqlcon;
}

public void sql_Desconectar()


{
try
{
sqlcon.Close();
}
catch
{ }

}
}

_CREANDO LA CLASE cls_datos


______________________________________________________________

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
class cls_datos : cls_conexion
{
#region DATOS
string query = "";
SqlCommand command;
cls_conexion conexion = new cls_conexion();
DataSet ds = new DataSet();
SqlDataAdapter adapter;
#endregion

public cls_datos()
{

public void sql_insertar(string tabla, string campos, string valores)


{
query = "INSERT INTO " + tabla + "(" + campos + ") VALUES (" + valores + ")";

try
{
command = new SqlCommand(query, conexion.sql_conectar());
command.ExecuteNonQuery();
conexion.sql_Desconectar();
}
catch (SqlException x)
{
MessageBox.Show(x.ToString());
}

public DataTable sql_Consultas(string tabla, string campos, string condicion)


{
query = "SELECT " + campos + " FROM " + tabla + " WHERE " + condicion + "";
try
{
adapter = new SqlDataAdapter(query, conexion.sql_conectar());
adapter.Fill(ds, "TDATOS");
}
catch (SqlException x)
{
MessageBox.Show(x.ToString());
}
return ds.Tables["TDATOS"];
}

public void sql_actualizar(string tabla, string campos, string condicion)


{
query = "UPDATE " + tabla + " SET " + campos + " WHERE " + condicion + "";

try
{
command = new SqlCommand(query, conexion.sql_conectar());
command.ExecuteNonQuery();
conexion.sql_Desconectar();
}
catch (SqlException x)
{
MessageBox.Show(x.ToString());
}
}

}
}

______________________________________________________________________________________________

Form1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ejemplo.Formularios; // para usar formularios de la carpeta
[ FORMULARIOS ]

namespace ejemplo
{
public partial class FrmPrincipal : Form
{
public FrmPrincipal()
{
InitializeComponent();
}
Boton Insertar
private void btnInsert_Click(object sender, EventArgs e)
{
frmInsert insert = new frmInsert();
insert.Show();
}
Boton Consulta
private void btnConsulta_Click(object sender, EventArgs e)
{
frmConsulta consulta = new frmConsulta();
consulta.Show();
}
Boton Actualizar
private void btnActualizar_Click(object sender, EventArgs e)
{
frmActualizar actualizar = new frmActualizar();
actualizar.Show();
}

______________________________________________________________________________________________

Formulario Insertar

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
public partial class FrmInsertar : Form
{
cls_datos datos = new cls_datos();

public FrmInsertar()
{
InitializeComponent();
}

public void limpiar()


{
txtNombre.Text = "";
txtApellido.Text = "";
txtTelefono.Text = "";
txtNombre.Focus();
}

private void FrmInsertar_Load(object sender, EventArgs e)


{
txtNombre.Focus();
}

private void button1_Click(object sender, EventArgs e)


{
limpiar();
}

private void button2_Click(object sender, EventArgs e)


{
datos.sql_insertar("agenda", "nombre,apellido,telefono", "'" + txtNombre.Text +
"','" + txtApellido.Text + "','" + txtTelefono.Text + "'");
limpiar();

}
}
}

______________________________________________________________________________________________

Formulario consulta

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ejemplo.Clases;

namespace ejemplo.Formularios
{
public partial class frmConsulta : Form
{
cls_datos datos = new cls_datos();

public frmConsulta()
{
InitializeComponent();
}

private void frmConsulta_Load(object sender, EventArgs e)


{
txtBuscar.Focus();
}

Boton Buscar

private void btnBuscar_Click(object sender, EventArgs e)


{
grvConsulta.DataSource = datos.sql_Consultas("agenda","codigo as ID,nombre as
Nombre,apellido as Apellido,telefono as Telefono ","nombre = '"+ txtBuscar.Text +"' or
apellido = '"+ txtBuscar.Text +"'");
}

Link Salir

private void lnkSalir_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)


{
if (MessageBox.Show("Esta Seguro ? ", "AVISO", MessageBoxButtons.YesNo,
MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{
this.Close();
}
}

}
}

______________________________________________________________________________________________

Formulario actualizar

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
public partial class FrmActualiza : Form
{
cls_datos datos = new cls_datos();
public FrmActualiza()
{
InitializeComponent();
}

public void limpiar()


{
txtCodigo.Text = "";
txtNombre.Text = "";
txtApellido.Text = "";
txtTelefono.Text = "";

txtCodigo.Enabled = true;
linkLabel1.Enabled = true;
txtCodigo.Focus();

private void FrmActualiza_Load(object sender, EventArgs e)


{
txtCodigo.Focus();
}

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)


{
DataTable data = datos.sql_Consultas("agenda", "*", "codigo='" + txtCodigo.Text
+"'");

if (data.Rows.Count != 0)
{
txtNombre.Text = data.Rows[0]["nombre"].ToString();
txtApellido.Text = data.Rows[0]["apellido"].ToString();
txtTelefono.Text = data.Rows[0]["telefono"].ToString();

txtCodigo.Enabled = false;
linkLabel1.Enabled = false;
}
}

private void button1_Click(object sender, EventArgs e)


{
limpiar();
}

private void button2_Click(object sender, EventArgs e)


{
datos.sql_actualizar("agenda","nombre='"+txtNombre.Text+"',
apellido='"+txtApellido.Text+"',telefono='"+txtTelefono.Text+"'","codigo='"+txtCodigo.Text+
"'");
limpiar();

}
}

You might also like