internal CompraDB getCompraByPNR(string pnr) { CompraDB compras = null; try { // // Open the SqlConnection. // con.Open(); // // The following code uses an SqlCommand based on the SqlConnection. // SqlCommand cmd = new SqlCommand(String.Format("SELECT TOP 1 [PNR],[Precio],[Fecha_Compra],[Viaje],[Forma_Pago],[Cliente] FROM [GD2C2015].[JANADIAN_DATE].[Compra] where PNR={0} ", pnr), con); DataTable dt = new DataTable(); dt.TableName = "Tabla"; dt.Load(cmd.ExecuteReader()); if (dt.Rows.Count == 0) { con.Close(); return null; } foreach (DataRow Fila in dt.Rows) { compras = new CompraDB(Convert.ToInt32(Fila["PNR"]), Convert.ToDouble(Fila["Precio"]), Convert.ToDateTime(Fila["Fecha_Compra"]), Convert.ToInt32(Fila["Viaje"]), Convert.ToString(Fila["Forma_Pago"]), Convert.ToInt32(Fila["Cliente"])); } con.Close(); } catch (Exception exAlta) { con.Close(); throw (new Exception(exAlta.ToString())); } return compras; }
internal int insertarCompra(CompraDB compra) { int idCompra = 0; try { con.Open(); SqlCommand insertCompra = new SqlCommand(String.Format("INSERT INTO [GD2C2015].[JANADIAN_DATE].[Compra] (Precio,Fecha_Compra,Viaje,Forma_Pago,Cliente) VALUES ({0:0.00},'{1}',{2},'{3}',{4})", compra.getPrecio.ToString().Replace(",", "."), compra.getFecha, compra.getViaje, compra.getFormaPago, compra.getCliente), con); insertCompra.ExecuteNonQuery(); SqlCommand cmd = new SqlCommand(String.Format("SELECT SCOPE_IDENTITY() as Cont "), con); DataTable dt = new DataTable(); dt.TableName = "Tabla"; dt.Load(cmd.ExecuteReader()); if (dt.Rows.Count == 0) { con.Close(); new Exception(); } foreach (DataRow Fila in dt.Rows) { idCompra = Convert.ToInt32(Fila["Cont"]); break; } con.Close(); } catch (Exception exAlta) { con.Close(); throw (new Exception(exAlta.ToString())); } return idCompra; }