0% found this document useful (0 votes)
22 views6 pages

Autor

This document contains code for a Windows Forms application that manages author data in a SQL database. It includes methods for loading author data from the database into the form, adding a new author, modifying an existing author, and handling events like form loads and button clicks. The form displays author data in a datagrid and textboxes, allows editing the fields, and communicates with stored procedures to insert, update and retrieve author records from the database.

Uploaded by

Vugusto Torres
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views6 pages

Autor

This document contains code for a Windows Forms application that manages author data in a SQL database. It includes methods for loading author data from the database into the form, adding a new author, modifying an existing author, and handling events like form loads and button clicks. The form displays author data in a datagrid and textboxes, allows editing the fields, and communicates with stored procedures to insert, update and retrieve author records from the database.

Uploaded by

Vugusto Torres
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

D:\SisBiblioteca\SisBiblioteca\SisBiblioteca\Autor.

cs 1
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9 using System.Windows.Forms;
10
11 using System.IO;
12 using System.Data.SqlClient;
13 using System.Net;
14
15
16 namespace SisBiblioteca
17 {
18 public partial class Autor : Form
19 {
20
21 public Autor()
22 {
23 InitializeComponent();
24 }
25 EnlaceBD con = new EnlaceBD();
26 #region LLENAR AL FORMULARIO
27 private void Datos_a_Form(string ConexionStr)
28 {
29 label1.DataBindings.Clear();
30 textBox2.DataBindings.Clear();
31 textBox1.DataBindings.Clear();
32 SqlConnection conn = new SqlConnection(con.Conexion
(con.ConexionStr));
33 try
34 {
35 conn.Open();
36 DataTable ds1 = new DataTable();
37 SqlCommand cm = new SqlCommand("BDAutor_Sel", conn);
//command
38 SqlDataAdapter da = new SqlDataAdapter(cm);
//data adapter
39 cm.CommandType = CommandType.StoredProcedure; //tipo de
conexion
40 cm.ExecuteNonQuery();
41
42 DataGridTableStyle miestilo = new DataGridTableStyle();
43
44 DataGridTextBoxColumn grdColStyle1 = new
DataGridTextBoxColumn();
D:\SisBiblioteca\SisBiblioteca\SisBiblioteca\Autor.cs 2
45 grdColStyle1.HeaderText = "CODIGO";
46 grdColStyle1.MappingName = "ID_AU";
47 grdColStyle1.Width = 0;
48 grdColStyle1.Alignment = HorizontalAlignment.Center;
49
50 DataGridTextBoxColumn grdColStyle2 = new
DataGridTextBoxColumn();
51 grdColStyle2.HeaderText = " NOMBRES ";
52 grdColStyle2.MappingName = "AU_NOMBRES";
53 grdColStyle2.Width = 150;
54
55 DataGridTextBoxColumn grdColStyle3 = new
DataGridTextBoxColumn();
56 grdColStyle3.HeaderText = "APELLIDOS";
57 grdColStyle3.MappingName = "AU_APELLIDOS";
58 grdColStyle3.Width = 250;
59 grdColStyle3.Alignment = HorizontalAlignment.Center;
60
61
62 miestilo.GridColumnStyles.AddRange(new DataGridColumnStyle
[] { grdColStyle1, grdColStyle2, grdColStyle3 });
63 dataGrid1.TableStyles.Add(miestilo);
64 da.Fill(ds1);
65
66 this.dataGrid1.CaptionText = "DATOS DE AUTORES";
67 this.dataGrid1.DataSource = ds1;
68
69 label1.DataBindings.Add("Text", ds1, "ID_AU");
70 textBox2.DataBindings.Add("Text", ds1, "AU_NOMBRES");
71 textBox1.DataBindings.Add("Text", ds1, "AU_APELLIDOS");
72 }
73 catch (System.Exception error)
74 {
75 MessageBox.Show("Exite error en el proceso " +
error.Message);
76 }
77 finally
78 {
79 conn.Close();
80 }
81 }
82 #endregion
83
84 private void Autor_Load(object sender, EventArgs e)
85 {
86 Datos_a_Form(con.Conexion(con.ConexionStr));
87 }
88
89 private void menuItem1_Click(object sender, EventArgs e)
D:\SisBiblioteca\SisBiblioteca\SisBiblioteca\Autor.cs 3
90 {
91 dataGrid1.Enabled = false;
92 panel1.Visible = true;
93 textNombre.Text = "";
94 textApellido.Text = "";
95 }
96
97 private void button1_Click(object sender, EventArgs e)
98 {
99 SqlConnection conn = new SqlConnection(con.Conexion
(con.ConexionStr));
100 try
101 {
102 conn.Open();
103 SqlCommand cm = new SqlCommand("BDAutor_Add", conn);
//command
104 cm.CommandType = CommandType.StoredProcedure; //tipo de
conexion
105 cm.Parameters.Add(new SqlParameter("@v1",
SqlDbType.NVarChar, 50, "v1"));
106 cm.Parameters.Add(new SqlParameter("@v2",
SqlDbType.NVarChar, 50, "v2"));
107 cm.Parameters.Add(new SqlParameter("@v3",
SqlDbType.NVarChar, 50, "v3"));
108 cm.Parameters.Add(new SqlParameter("@v4",
SqlDbType.Float , 50, "v4"));
109 cm.Parameters.Add(new SqlParameter("@v5",
SqlDbType.DateTime , 50, "v5"));
110 cm.Parameters.Add(new SqlParameter("@v6",
SqlDbType.NVarChar , 50, "v6"));
111
112
113 cm.Parameters[0].Value = Convert.ToString
(textNombre.Text.ToUpper());
114 cm.Parameters[1].Value = Convert.ToString
(textApellido.Text.ToUpper());
115 cm.Parameters[2].Value = "";
116 cm.Parameters[3].Value = 0;
117 cm.Parameters[4].Value = DateTime.Now.ToShortDateString();
118 cm.Parameters[5].Value = Convert.ToString(Dns.GetHostName
() + "-" + System.DateTime.Now.ToUniversalTime());
119
120 cm.ExecuteNonQuery();
121 //MessageBox.Show("Insercion exitosa");
122 dataGrid1.Enabled = true;
123 Datos_a_Form(con.Conexion(con.ConexionStr));
124 panel1.Visible = false;
125 }
126 catch (System.Exception error)
D:\SisBiblioteca\SisBiblioteca\SisBiblioteca\Autor.cs 4
127 {
128 MessageBox.Show("Exite error en el proceso de inserción en
la tabla Departamento " + error.Message);
129 }
130 finally
131 {
132 conn.Close();
133 }
134 }
135
136 private void button2_Click(object sender, EventArgs e)
137 {
138 dataGrid1.Enabled = true;
139 Datos_a_Form(con.Conexion(con.ConexionStr));
140 panel1.Visible = false;
141 }
142
143 private void button4_Click(object sender, EventArgs e)
144 {
145 //modificacion
146
147 SqlConnection conn = new SqlConnection(con.Conexion
(con.ConexionStr));
148 try
149 {
150 conn.Open();
151 SqlCommand cm = new SqlCommand("BDAutor_Mod", conn);
//command
152 cm.CommandType = CommandType.StoredProcedure; //tipo de
conexion
153 cm.Parameters.Add(new SqlParameter("@v1", SqlDbType.Int,
50, "v1"));
154 cm.Parameters.Add(new SqlParameter("@v2",
SqlDbType.NVarChar, 50, "v2"));
155 cm.Parameters.Add(new SqlParameter("@v3",
SqlDbType.NVarChar, 50, "v3"));
156 cm.Parameters.Add(new SqlParameter("@v4",
SqlDbType.NVarChar, 50, "v4"));
157 cm.Parameters.Add(new SqlParameter("@v5", SqlDbType.Float,
50, "v5"));
158 cm.Parameters.Add(new SqlParameter("@v6",
SqlDbType.DateTime, 50, "v6"));
159 cm.Parameters.Add(new SqlParameter("@v7",
SqlDbType.NVarChar, 50, "v7"));
160
161 cm.Parameters[0].Value = Convert.ToInt32(label1.Text);//
cod
162 cm.Parameters[1].Value = Convert.ToString
(textBox2.Text.ToUpper());
D:\SisBiblioteca\SisBiblioteca\SisBiblioteca\Autor.cs 5
163 cm.Parameters[2].Value = Convert.ToString
(textBox1.Text.ToUpper());
164 cm.Parameters[3].Value = "";
165 cm.Parameters[4].Value = 0;
166 cm.Parameters[5].Value = DateTime.Now.ToShortDateString();
167 cm.Parameters[6].Value = Convert.ToString(Dns.GetHostName
() + "-" + System.DateTime.Now.ToUniversalTime());
168 cm.ExecuteNonQuery();
169
170
171 dataGrid1.Enabled = true;
172 Datos_a_Form(con.Conexion(con.ConexionStr));
173 panel2.Visible = false;
174
175
176 }
177 catch (System.Exception error)
178 {
179 MessageBox.Show("exite error en el proceso " +
error.Message);
180 }
181 finally
182 {
183 conn.Close();
184 }
185 }
186
187 private void menuItem2_Click(object sender, EventArgs e)
188 {
189 dataGrid1.Enabled = false;
190 panel2.Visible = true;
191 }
192
193 private void menuItem3_Click(object sender, EventArgs e)
194 {
195 SqlConnection conn = new SqlConnection(con.Conexion
(con.ConexionStr));
196 try
197 {
198 conn.Open();
199 DialogResult result;
200 result = MessageBox.Show("¿Desea eliminar el registro
seleccionado?", "Confirmar mensaje",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
201 if (result == DialogResult.Yes)
202 {
203 SqlCommand aCommand = new SqlCommand("BDC_Delete",
conn);
204 aCommand.CommandType = CommandType.StoredProcedure;
D:\SisBiblioteca\SisBiblioteca\SisBiblioteca\Autor.cs 6
205 aCommand.Parameters.Add(new SqlParameter
("@OrdenTabla", SqlDbType.Int, 4, "OrdenTabla"));
206 aCommand.Parameters.Add(new SqlParameter("@v1",
SqlDbType.Int, 4, "v1"));
207 aCommand.Parameters[0].Value = 1;
208 aCommand.Parameters[1].Value = Int32.Parse
(label1.Text);
209 aCommand.ExecuteNonQuery();
210 Datos_a_Form(con.Conexion(con.ConexionStr));
211 }
212
213 }
214 catch (System.Exception error)
215 {
216 MessageBox.Show("exite error en el proceso " +
error.Message);
217 }
218 finally
219 {
220 conn.Close();
221 }
222 }
223
224 private void menuItem5_Click(object sender, EventArgs e)
225 {
226 this.Close();
227 }
228
229 private void button3_Click(object sender, EventArgs e)
230 {
231 dataGrid1.Enabled = true;
232 Datos_a_Form(con.Conexion(con.ConexionStr));
233 panel2.Visible = false;
234 }
235 }
236 }
237

You might also like