AP7-AA4-EV1-DESARROLLO DE APLICACIONES WINDOWS CON C# Y VISUAL STUDIO .NET
MARIA YANETH OSPINA MOLINA
Servicio Ncio!" e A$re!i%&e SENA A!'"i(i( ) De(rro""o e Si(*e+( e I!,or+ci! M)o /10
USUARIO A+i! CONTRASE2A A+i! using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Windows.Forms; namespace WindowsFormsApplication ! class usuarios usuarios""conexion ! pri#ate string usuario; pri#ate string contrasena; pu$lic usuarios%& ! usuario ' string string.(mpty; .(mpty; contrasena ' string string.(mpty; .(mpty; t)is.sql t)is .sql ' string ' string.(mpty; .(mpty; * pu$lic string +suario ! get !return t)is.usuario;* t)is.usuario;* set !t)is t)is.usuario .usuario ' #alue ' #alue;* ;* * pu$lic string Contrasena !
get !return t)is.contrasena;* set !t)is.contrasena ' #alue;* * pu$lic $ool ,uscar%& ! $ool resultado ' -alse; t)is.sql ' string.Format%/S(L(CT #en+suario F012 #endedores W3(0( #en+suario '4!5*4 A6D #enContrasena '4!*4/ 7t)is.usuario7 t)is.contrasena&; t)is.comandosql ' new SqlCommand %t)is.sql7t)is.cnn&;
t)is.cnn.1pen%&; SqlData0eader reg ' null; reg ' t)is.comandosql.(xecute0eader%&; i- %reg.0ead%&& ! resultado ' true; mensa8es' /,ien#enido datos correctos/; * else ! mensa8es ' /Datos incorrectos/; * t)is.cnn.Close%&; return resultado; *
* *
using System; using System.Collections.Generic; using System.Component2odel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication ! pu$lic partial class Form9 " Form ! pu$lic Form9%& ! :nitialieComponent%&; int a; int $; int c; * pri#ate #oid Form9
a ' new Form>%&; a.S)ow%&; * pri#ate #oid -acturarToolStrip2enu:tem
using System; using System.Collections.Generic; using System.Component2odel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.Data;
namespace WindowsFormsApplication ! pu$lic partial class Form> " Form ! com$o$ox com$o ' new com$o$ox%&; agregarproducto c ' new agregarproducto%&; pu$lic Form>%& ! :nitialieComponent%&; com$o.Seleccionar%co$"com$o,ox9&; com$o c$ ' new com$o%&; c$.llenaritems%com$o,ox&; *
pri#ate #oid ta$age9
* pri#ate #oid ta$age
! * pri#ate #oid text,ox
* pri#ate #oid com$o,ox
i- %sql.actualiar%text,ox.Text7 com$o,ox9.Text7 text,ox.Text7 text,[email protected]&& ! 2essage,ox.S)ow%/Datos Actualiado/&; t)is.3ide%&;
* else 2essage,ox.S)ow%/Datos 6o Actualiados/&; * pri#ate #oid $utton@
using System; using System.Collections.Generic; using System.Component2odel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient;
namespace WindowsFormsApplication ! pu$lic partial class Form? " Form ! -acturacom$o com$o ' new -acturacom$o%&; agregarproducto c ' new agregarproducto%&; dou$le #al; dou$le cant; decimal resultado; pu$lic Form?%& ! :nitialieComponent%&; com$o.Seleccionar%co$" listaCli&;
com$o.Seleccionarroducto%co$" listaro&; com$o c$ ' new com$o%&; c$.+sers%username&;
* pu$lic SqlConnection conex ' new SqlConnection%/Data Source'.;:nitial Catalog'-acturacion;:ntegrated Security'True/&; pri#ate #oid la$el9
pri#ate #oid AgregarproE.Balue ' username.Text; -ila.Cells@E.Balue-ila.Cells?E.Balue '' Codro.Text;-actura.Text; -ila.CellsE.Balue ' cantidad.Text; dg#.0ows.Add%-ila&; -ec)a.Text ' //; cantidad.Text ' //; listaro.Text ' //; Codro.Text ' //; listaro.0esetText%&;
decimal total ' 5; -oreac) %DataGridBiew0ow 0ow in dg#.0ows& !
total I' Con#ert.ToDecimal%0ow.Cells/colum?/E.Balue&; * #alortotal.Text ' Con#ert.ToString%total&; * pri#ate #oid Form?E; * *
pri#ate #oid $utton9
!
2essage,ox.S)ow%/(sta -actura ya existe/&; * else ! try ! -oreac)%DataGridBiew0ow 0ow in dg#.0ows& ! SqlCommand agregar ' new SqlCommand%/insert into -acturas #alues %-ac6umero7 -acFec)a7 -acCliente7 -acBalortotal7 -acBendedor&/7 conex&; conex.1pen%&; agregar.arameters.Clear%&; agregar.arameters.AddWit)Balue% /-ac6umero/7 Con#ert.ToString%-actura.Text&&;
agregar.arameters.AddWit)Balue% /-acFec)a/7 Con#ert.ToString%0ow.Cells/colum9/E.Balue&&; agregar.arameters.AddWit)Balue% /-acCliente/7 Con#ert.ToString%0ow.Cells/colum>/E.Balue&&; agregar.arameters.AddWit)Balue% /-acBalortotal/7 Con#ert.ToString%#alortotal.Text&&; agregar.arameters.AddWit)Balue% /-acBendedor/7 Con#ert.ToString%0ow.Cells/colum@/E.Balue&&; agregar.(xecute6onKuery%&; * * catc) ! * -inally ! * * *
try ! SqlCommand agregarro ' new SqlCommand%/insert into -acturadetalleT #alues %-ac6umero7 -acroducto7 -acCantidad&/7 conex&;
-oreac) %DataGridBiew0ow 0ow in dg#.0ows& !
agregarro.arameters.Clear%&; agregarro.arameters.AddWit)Balue% /-ac6umero/ 7 Con#ert.ToString%0ow.Cells/Column/E.Balue&&; agregarro.arameters.AddWit)Balue% /-acroducto/7 Con#ert.To:nt>9%0ow.Cells/Column/E.Balue&&; agregarro.arameters.AddWit)Balue% /-acCantidad/ 7 Con#ert.To:nt>9%0ow.Cells/Column/ E.Balue&&;
agregarro.(xecute6onKuery%&;
* 2essage,ox.S)ow%/Datos agregados/&; * catc) %(xception ex& ! 2essage,ox.S)ow%/(rror al guardar/Iex&; * -inally ! conex.Close%&;
* * pri#ate #oid Kuitarproduc
* * *
using System; using System.Collections.Generic; using System.Component2odel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApplication ! pu$lic partial class Form@ " Form !
com$o$ox com$o ' new com$o$ox%&; agregarproducto sql ' new agregarproducto%&; pu$lic Form@%& ! :nitialieComponent%&; com$o c$ ' new com$o%&; c$.llenaritems%com$o,ox9&; * pri#ate #oid Form@
cmd.(xecute6onKuery%&; DataTa$le dt ' new DataTa$le%&; SqlDataAdapter da ' new SqlDataAdapter %cmd&; da.Fill%dt&; dg#.DataSource ' dt; conex.Close%&; * * *
using System; using System.Collections.Generic; using System.Component2odel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication ! pu$lic partial class Clientes " Form ! agregarcliente c ' new agregarcliente%&; pu$lic Clientes%& ! :nitialieComponent%&; * pri#ate #oid Clientes.Text7 text,ox?.Text7 text,[email protected]& '' true& ! 2essage,ox.S)ow%/datos insertados/&; t)is.0e-res)%&; text,ox.Clear%&; text,ox9.Clear%&; text,ox>.Clear%&; text,ox?.Clear%&;
text,[email protected]%&; t)is.3ide%&;
* else ! 2essage,ox.S)ow%/datos no insertados/&; * *
*
*
CODI3O UTILIADO PARA LAS 5UNCIONES CONE6IN CON 8ASE DE DATOS using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace WindowsFormsApplication ! class conexion ! pu$lic string cadenaconexion; protected string sql; protected int resultado; protected SqlConnection cnn; protected SqlCommand comandosql; protected string mensa8es; pu$lic conexion%& ! t)is.cadenaconexion ' %/Data Source'.;:nitial Catalog '-acturacion; integrated security'true/&; t)is.cnn ' new SqlConnection%t)is.cadenaconexion&; * * *
USUARIOS using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Windows.Forms;
namespace WindowsFormsApplication ! class usuarios"conexion ! pri#ate string usuario; pri#ate string contrasena; pu$lic usuarios%& ! usuario ' string.(mpty; contrasena ' string.(mpty; t)is.sql ' string.(mpty; * pu$lic string +suario ! get !return t)is.usuario;* set !t)is.usuario ' #alue;* * pu$lic string Contrasena ! get !return t)is.contrasena;* set !t)is.contrasena ' #alue;* * pu$lic $ool ,uscar%& ! $ool resultado ' -alse; t)is.sql ' string.Format%/S(L(CT #en+suario F012 #endedores W3(0( #en+suario '4!5*4 A6D #enContrasena '4!*4/7t)is.usuario7 t)is.contrasena&; t)is.comandosql ' new SqlCommand%t)is.sql7t)is.cnn&; t)is.cnn.1pen%&; SqlData0eader reg ' null; reg ' t)is.comandosql.(xecute0eader%&; i- %reg.0ead%&& ! resultado ' true; mensa8es' /,ien#enido datos correctos/; * else ! mensa8es ' /Datos incorrectos/; * t)is.cnn.Close%&; return resultado; *
* *
A3RE3AR PRODUCTO using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Sql; using System.Data; using System.Data.SqlClient; using System.Windows.Forms; namespace WindowsFormsApplication ! class agregarproducto ! pri#ate SqlConnection conexion ' new SqlConnection%/Data Source'.;:nitial Catalog'-acturacion;:ntegrated Security'True/&; pri#ate DataSet ds; pu$lic DataTa$le mostrardatos%& ! conexion.1pen%&; SqlCommand cmd ' new SqlCommand%/select H -rom productosT/7 conexion&; SqlDataAdapter ad ' new SqlDataAdapter %cmd&; ds ' new DataSet%&; ad.Fill%ds7 /ta$la/&; conexion.Close%&; return ds.Ta$les/ta$la/E; * pu$lic $ool :nsertar%string proCodigo7 string proDescripcion7 string proBalor7 string proCantidad& ! conexion.1pen%&; SqlCommand cmd ' new SqlCommand%string.Format%/insert into productosT #alues %!5*7 4!*47 4!9*47 4!>*4&/7 new stringE ! proCodigo7 proDescripcion7 proBalor7 proCantidad *&7 conexion&; int -ilasa-ectadas ' cmd.(xecute6onKuery%&; conexion.Close%&; i- %-ilasa-ectadas 5& return true; else return -alse; * pu$lic $ool actualiar%string proCodigo7 string proDescripcion7 string proBalor7 string proCantidad& ! conexion.1pen%&; SqlCommand cmd ' new SqlCommand%string.Format%/update productosT set proDescripcion ' 4!5*47 proBalor ' 4!*47 proCantidad ' 4!9*4 w)ere proCodigo ' !>*/ 7 new stringE ! proDescripcion7 proBalor7 proCantidad7 proCodigo*&7 conexion&; int -ilasa-ectadas ' cmd.(xecute6onKuery%&; conexion.Close%&; i- %-ilasa-ectadas 5& return true; else return -alse;
* pu$lic DataTa$le ,uscar%string proDescripcion& ! conexion.1pen%&; SqlCommand cmd ' new SqlCommand%string.Format%/select H -rom productosT w)ere proDescripcion ' 4!*4/7 proDescripcion&7 conexion&; SqlDataAdapter ad ' new SqlDataAdapter %cmd&; ds ' new DataSet%&; ad.Fill%ds7 /ta$la/&; conexion.Close%&; return ds.Ta$les/ta$la/E; * pu$lic $ool eliminar%string proCodigo& ! conexion.1pen%&; SqlCommand cmd ' new SqlCommand%string.Format%/delete -rom productosT w)ere proCodigo ' !5*/7proCodigo&7 conexion&; int -ilasa-ectadas ' cmd.(xecute6onKuery%&; conexion.Close%&; i- %-ilasa-ectadas 5& return true; else return -alse; *
* *
AG0(GA0 CL:(6T(" using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Sql; using System.Data; using System.Data.SqlClient; using System.Windows.Forms; namespace WindowsFormsApplication ! class agregarcliente ! pri#ate SqlConnection conexion ' new SqlConnection%/Data Source'.;:nitial Catalog'-acturacion;:ntegrated Security'True/&; pri#ate DataSet ds;
pu$lic $ool :nsertar%string cliDocumento7 string cli6om$re7 string cliDireccion7 string cliTele-ono7string cliCorreo& ! conexion.1pen%&; SqlCommand cmd ' new SqlCommand%string.Format%/insert into clientes #alues %!5*7 4!*47 4!9*47 4!>*47 4!?*4&/7 new stringE ! cliDocumento7 cli6om$re7 cliDireccion7 cliTele-ono7 cliCorreo *&7 conexion&; int -ilasa-ectadas ' cmd.(xecute6onKuery%&; conexion.Close%&; i- %-ilasa-ectadas 5& return true; else return -alse;
* * *
A3RE3AR 5ACTURA using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Sql; using System.Data; using System.Data.SqlClient; using System.Windows.Forms; namespace WindowsFormsApplication ! class agregar-actura ! pri#ate SqlConnection conexion ' new SqlConnection%/Data Source'.;:nitial Catalog'-acturacion;:ntegrated Security'True/&; pri#ate DataSet ds; pu$lic DataTa$le mostrardatos%& ! conexion.1pen%&; SqlCommand cmd ' new SqlCommand%/select H -rom -acturas/7 conexion&; SqlDataAdapter ad ' new SqlDataAdapter %cmd&; ds ' new DataSet%&; ad.Fill%ds7 /ta$la/&; conexion.Close%&; return ds.Ta$les/ta$la/E; * pu$lic $ool Consecuti#oFactura%string -ac6umero7 string -acFec)a7 string -acCliente7 string -acBalortotal7 string -acBendedor& ! conexion.1pen%&;
SqlCommand cmd ' new SqlCommand%string.Format%/insert into -acturas #alues %!5*7 4!*47 4!9*47 4!>*474!?*4&/7 new stringE ! -ac6umero7 -acFec)a7 -acCliente7 -acBalortotal7 -acBendedor *&7 conexion&; int -ilasa-ectadas ' cmd.(xecute6onKuery%&; conexion.Close%&; i- %-ilasa-ectadas 5& return true; else return -alse;
* pu$lic $ool FacturaDetalle%string -ac6umero7 string -acroducto7 string FacCantidad& ! conexion.1pen%&; SqlCommand cmd ' new SqlCommand%string.Format%/insert into productosT #alues %!5*7 4!*47 4!9*4&/7 new stringE ! -ac6umero7 -acroducto7 FacCantidad*&7 conexion&; int -ilasa-ectadas ' cmd.(xecute6onKuery%&; conexion.Close%&; i- %-ilasa-ectadas 5& return true; else return -alse;
*
* *
DEMAS CODI3O UTILIADO using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; using System.Windows.Forms; namespace WindowsFormsApplication ! class com$o ! SqlCommand cmd; SqlConnection cnn; SqlData0eader dr; pu$lic com$o%& ! try ! cnn ' new SqlConnection%/Data Source'.;:nitial Catalog'-acturacion;:ntegrated Security'True/&; cnn.1pen%&; *
catc) %(xception ex& ! * * pu$lic #oid llenaritems%Com$o,ox c$& ! try !
SqlData0eader dr; cmd ' new SqlCommand%/select proDescripcion -rom productosT/7 cnn&; dr ' cmd.(xecute0eader%&; w)ile %dr.0ead%&& ! c$.:tems.Add%dr/proDescripcion/ E.ToString%&&; *
* catc) %(xception ex& ! * * pu$lic #oid +sers%Com$o,ox c$& ! try ! SqlData0eader dr; cmd ' new SqlCommand%/select #en+suario -rom #endedores/7 cnn&; dr ' cmd.(xecute0eader%&; w)ile %dr.0ead%&& ! c$.:tems.Add%dr/#en+suario/E.ToString%&&; * * catc) %(xception ex& !
*
*
* *
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T)reading.Tas=s; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApplication
! class com$o$ox ! SqlConnection con ' new SqlConnection%/Data Source'.;:nitial Catalog'-acturacion;:ntegrated Security'True/&; pu$lic #oid Seleccionar%Com$o,ox co$& ! co$.:tems.Clear%&; con.1pen%&; SqlCommand cmd ' new SqlCommand%/select H -rom productosT/7 con&; SqlData0eader dr ' cmd.(xecute0eader%&; w)ile %dr.0ead%&& ! co$.:tems.Add%drE.ToString%&&; * con.Close%&; co$.:tems.:nsert%57 /MMMseleccione un :temMMM/&; co$.Selected:ndex ' 5; * pu$lic stringE captar
I /4/7 con&;
SqlData0eader dr ' cmd.(xecute0eader%&; stringE resultado ' null; w)ile %dr.0ead%&& ! stringE #alores ' ! dr5E.ToString%&7 dr9E.ToString%&7 dr>E.ToString%&7 *; resultado ' #alores; * con.Close%&; return resultado; * * *
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; using System.Windows.Forms;
namespace WindowsFormsApplication ! class -acturacom$o ! SqlConnection con ' new SqlConnection%/Data Source'.;:nitial Catalog'-acturacion;:ntegrated Security'True/&; pu$lic #oid Seleccionar%Com$o,ox co$& ! co$.:tems.Clear%&; con.1pen%&; SqlCommand cmd ' new SqlCommand%/select H -rom clientes/7 con&; SqlData0eader dr ' cmd.(xecute0eader%&; w)ile %dr.0ead%&& ! co$.:tems.Add%drE.ToString%&&; * con.Close%&; co$.:tems.:nsert%57 /MMMSeleccione a el ClienteMMM/&; co$.Selected:ndex ' 5; * pu$lic stringE captarE.ToString%&7 dr?E.ToString%&7 *; resultado ' #alores; * con.Close%&; return resultado; * pu$lic #oid Seleccionarroducto%Com$o,ox co$& ! co$.:tems.Clear%&; con.1pen%&; SqlCommand cmd ' new SqlCommand%/select H -rom productosT/7 con&; SqlData0eader dr ' cmd.(xecute0eader%&; w)ile %dr.0ead%&& ! co$.:tems.Add%drE.ToString%&&; * con.Close%&;
co$.:tems.:nsert%57 /MMMSeleccione un productoMMM/&; co$.Selected:ndex ' 5; * pu$lic stringE captar
stringE resultado ' null; w)ile %dr.0ead%&& ! stringE #alores ' ! dr5E.ToString%&7 dr9E.ToString%&7 dr>E.ToString%&7 *; resultado ' #alores; * con.Close%&; return resultado; * * *
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace WindowsFormsApplication ! static class rogram
!
* *
NNN Osummary / T)e main entry point -or t)e application. / ONsummary STAT)readE static #oid 2ain%& ! Application.(na$leBisualStyles%&; Application.SetCompati$leText0enderingDe-ault% -alse&; Application.0un%new Form%&&; *