//Agregar en la base de datos las frutas del formulario (_manzana, _banana y _durazno). //Invocar al metodo AgregarFrutas():bool private void btnPunto7_Click(object sender, EventArgs e) { if (SegundoParcial.AgregarFrutas(this)) { MessageBox.Show("Se agregaron las frutas a la Base de Datos"); } else { MessageBox.Show("NO se agregaron las frutas a la Base de Datos"); } }
private static bool AgregarFrutas(SegundoParcial thisClase) { System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone(); customCulture.NumberFormat.NumberDecimalSeparator = "."; System.Threading.Thread.CurrentThread.CurrentCulture = customCulture; bool flag = false; StringBuilder sb = new StringBuilder(); SqlConnection bdConnection = new SqlConnection(Properties.Settings.Default.Conexion_bd); SqlCommand command = new SqlCommand(); command.Connection = bdConnection; command.CommandType = CommandType.Text; command.CommandText = "INSERT INTO sp_lab_II.dbo.frutas (nombre,peso,precio) VALUES" + "('" + thisClase._manzana.Nombre + "'," + thisClase._manzana.Peso.ToString() + "," + (thisClase.c_manzanas.PrecioTotal / thisClase.c_manzanas.Elementos.Count).ToString() + ")," + "('" + thisClase._banana.Nombre + "'," + thisClase._banana.Peso.ToString() + "," + (thisClase.c_bananas.PrecioTotal / thisClase.c_bananas.Elementos.Count).ToString() + ")," + "('" + thisClase._durazno.Nombre + "'," + thisClase._durazno.Peso.ToString() + "," + (thisClase.c_duraznos.PrecioTotal / thisClase.c_duraznos.Elementos.Count).ToString() + ")"; try { bdConnection.Open(); if (command.ExecuteNonQuery() > 0) { flag = true; } } catch (Exception e) { MessageBox.Show(e.Message); } finally { bdConnection.Close(); } return(flag); }
//Obtener de la base de datos (sp_lab_II) el listado de frutas: //frutas { id(autoincremental - numérico) - nombre(cadena) - peso(numérico) - precio(numérico) }. //Invocar al método ObtenerListadoFrutas. private void btnPunto6_Click(object sender, EventArgs e) { MessageBox.Show(SegundoParcial.ObtenerListadoFrutas()); }