/// <summary> /// Hace un Insert en la BD, guardando el obj instrumento pasado por parametro en la misma. Tiene que llamar a InsertarSaxofon o InsertarPiano ya que estás son tablas derivadas de Instrumento. /// </summary> /// <param name="instrumento"></param> /// <returns></returns> public bool InsertarInstrumento(Instrumento instrumento) { bool rta = true; try { if (this.MarcaId(instrumento.Marca) < 1) { this.InsertarMarca(instrumento.Marca); } StringBuilder sql = new StringBuilder(); sql.Append("INSERT INTO Instrumento (Modelo, Calidad, Usado, idMarca, Precio, Stock) VALUES("); StringBuilder stringBuilder = sql.AppendLine("'" + instrumento.Modelo + "'," + instrumento.Calidad + "," + "," + this.MarcaId(instrumento.Marca) + "," + instrumento.Precio.ToString(System.Globalization.CultureInfo.InvariantCulture) + "," + instrumento.Stock + ")"); this.comando = new SqlCommand(); this.comando.CommandType = CommandType.Text; this.comando.CommandText = sql.ToString(); this.comando.Connection = this.conexion; this.conexion.Open(); int filasAfectadas = this.comando.ExecuteNonQuery(); if (filasAfectadas == 0) { rta = false; } this.conexion.Close(); if (instrumento is Saxofon) { this.InsertarSaxofon(instrumento); } else { this.InsertarPiano(instrumento); } } catch (Exception) { rta = false; } finally { if (this.conexion.State == ConnectionState.Open) { this.conexion.Close(); } } return(rta); }
/// <summary> /// Busca y devuelve el ID del instrumento pasado por parametro en la BD, si no se encuentra devuelve -1. /// </summary> /// <param name="instrumento"></param> /// <returns></returns> private int GetInstrumentoId(Instrumento instrumento) { try { this.comando = new SqlCommand(); this.comando.CommandType = CommandType.Text; this.comando.CommandText = "SELECT a.id AS idInstrumento, b.id AS idMarca, * FROM Instrumento a JOIN Marca b ON a.idMarca = b.id"; this.comando.Connection = this.conexion; this.conexion.Open(); this.lector = comando.ExecuteReader(); while (this.lector.Read()) { bool equal = true; if (instrumento.Modelo != this.lector["Modelo"].ToString()) { equal = false; } if (instrumento.Marca != new Marca(this.lector["Nombre"].ToString(), (EPais)this.lector["Pais"])) { equal = false; } if (instrumento.Precio != (double)this.lector["Precio"]) { equal = false; } if (equal) { return((int)this.lector["id"]); } } return(-1); } catch (Exception) { return(-1); } finally { if (this.conexion.State == ConnectionState.Open) { this.conexion.Close(); } } }
/// <summary> /// Hace una entrada en la tabla Piano de la BD /// </summary> /// <param name="piano"></param>m> /// <returns></returns> private bool InsertarPiano(Instrumento piano) { bool rta = true; try { StringBuilder sql = new StringBuilder(); sql.Append("INSERT INTO Piano (idInstrumento, Tipo) VALUES("); sql.AppendLine("'" + this.GetInstrumentoId(piano) + (piano as Piano).TipoPiano + ")"); this.comando = new SqlCommand(); this.comando.CommandType = CommandType.Text; this.comando.CommandText = sql.ToString(); this.comando.Connection = this.conexion; this.conexion.Open(); int filasAfectadas = this.comando.ExecuteNonQuery(); if (filasAfectadas == 0) { rta = false; } } catch (Exception) { rta = false; } finally { if (this.conexion.State == ConnectionState.Open) { this.conexion.Close(); } } return(rta); }