private void checkRutas(object sender, RoutedEventArgs e) { string qry_insertRuta = "INSERT INTO rutas (ruta, nombre) VALUES ('"; try { db.openConn(); db.tr = db.getConn().BeginTransaction(); foreach (Rutas r in routes) { if (String.IsNullOrEmpty(r.ID)) { string newRoute = qry_insertRuta + r.ruta + "', '" + r.nombre + "')"; using (db.setComm(newRoute)) { db.getComm().ExecuteNonQuery(); } } else { string updateRoute = "UPDATE rutas SET "; updateRoute += "ruta = " + "'" + r.ruta + "', "; updateRoute += "nombre = " + "'" + r.nombre + "' "; updateRoute += "WHERE ID = " + r.ID; using (db.setComm(updateRoute)) { db.getComm().ExecuteNonQuery(); } } } db.tr.Commit(); MessageBox.Show("Rutas Actualizadas."); } catch (SQLiteException ex) { MessageBox.Show("Error: {0}", ex.ToString()); if (db.tr != null) { try { db.tr.Rollback(); } catch (SQLiteException ex2) { Console.WriteLine("Transaction rollback failed."); Console.WriteLine("Error: {0}", ex2.ToString()); } finally { db.tr.Dispose(); } } } finally { if (db.getComm() != null) { db.getComm().Dispose(); } if (db.tr != null) { db.tr.Dispose(); } if (db.getConn() != null) { try { db.getConn().Close(); } catch (SQLiteException ex) { Console.WriteLine("Closing connection failed."); Console.WriteLine("Error: {0}", ex.ToString()); } finally { db.getConn().Dispose(); } } } }
private void crearAuto_Click(object sender, RoutedEventArgs e) { string nombreModeloAuto = nombreAuto.Text; MessageBox.Show(nombreModeloAuto); string qry_insAuto = "INSERT INTO autos (modelo, isActive) VALUES ('" + nombreModeloAuto + "', 1)"; db.openConn(); // if car was not inserted or if there is no systems registered, then if (affectedRows == 0 || getNumSelectedCB() == 0) { MessageBoxResult result = MessageBox.Show( "Estás seguro que quieres crear este auto sin relacion con algún sistema?", "Crear auto", MessageBoxButton.YesNo ); switch (result) { case MessageBoxResult.Yes: using (db.setComm(qry_insAuto)) { affectedRows = db.getComm().ExecuteNonQuery(); } if (affectedRows == 0) { db.sendMBandCloseConn("No se pudo crear Auto. Inténtalo de nuevo"); return; } db.sendMBandCloseConn("Auto agregado exitosamente a la base de datos." + " No se relacionó con ningún sistema."); break; case MessageBoxResult.No: MessageBox.Show("Auto no creado.", "Crear Auto"); db.closeConn(); break; } } else { using (db.setComm(qry_insAuto)) { affectedRows = db.getComm().ExecuteNonQuery(); } if (affectedRows == 0) { db.sendMBandCloseConn("No se pudo crear AUTO. Inténtalo de nuevo"); return; } // We get the ID of the last inserted car successfully, // we then have to relate with a system string sql = "SELECT last_insert_rowid()"; SQLiteCommand cmd = new SQLiteCommand(sql, db.getConn()); int lastInsertAutoID = Convert.ToInt32(cmd.ExecuteScalar()); // function to insert relations of a car autosRelations(lastInsertAutoID); db.sendMBandCloseConn("Auto agregado exitosamente a la base de datos." + " Se relacionó con varios sistemas."); } }
// DELETE CARS FOREVER private void deleteOnceNForAll(string ID_item) { string deleteECF = "DELETE FROM edit_campos_funktion "; deleteECF += "WHERE auto_ID = " + ID_item; string deleteRAS = "DELETE FROM rel_autos_sist "; deleteRAS += "WHERE autos_ID = " + ID_item; string deleteAuto = "DELETE FROM autos "; deleteAuto += "WHERE ID = " + ID_item; try { db.openConn(); db.tr = db.getConn().BeginTransaction(); using (db.setComm(deleteECF)) { db.getComm().ExecuteNonQuery(); } using (db.setComm(deleteRAS)) { db.getComm().ExecuteNonQuery(); } using (db.setComm(deleteAuto)) { db.getComm().ExecuteNonQuery(); } db.tr.Commit(); } catch (SQLiteException ex) { MessageBox.Show("Error: {0}", ex.ToString()); if (db.tr != null) { try { db.tr.Rollback(); } catch (SQLiteException ex2) { Console.WriteLine("Transaction rollback failed."); Console.WriteLine("Error: {0}", ex2.ToString()); } finally { db.tr.Dispose(); } } } finally { if (db.getComm() != null) { db.getComm().Dispose(); } if (db.tr != null) { db.tr.Dispose(); } if (db.getConn() != null) { try { db.getConn().Close(); } catch (SQLiteException ex) { Console.WriteLine("Closing connection failed."); Console.WriteLine("Error: {0}", ex.ToString()); } finally { db.getConn().Dispose(); } } } MessageBox.Show("Información actualizada correctamente"); }