public void updateContact(lContact contact) { try { conection.Open(); //se realizara el Query(Instruccion SQL) en ves de realizarlo por un Procedimiento Almacenado string query = @"UPDATE Contact SET Nombres=@Nombres,Apellidos=@Apellidos,Telefono=@Telefono,Direccion=@Direccion WHERE Id=@Id"; //esta es la forma normal por parametro y valor agregando el id SqlParameter id = new SqlParameter("@Id", contact.Id); SqlParameter nombre = new SqlParameter("@Nombres", contact.Nombres); SqlParameter apellido = new SqlParameter("@Apellidos", contact.Apellidos); SqlParameter telefono = new SqlParameter("@Telefono", contact.Telefono); SqlParameter direccion = new SqlParameter("@Direccion", contact.Direccion); SqlCommand command = new SqlCommand(query, conection); command.Parameters.Add(id); command.Parameters.Add(nombre); command.Parameters.Add(apellido); command.Parameters.Add(telefono); command.Parameters.Add(direccion); command.ExecuteNonQuery(); } catch (Exception) { throw; } finally { conection.Close(); } }
//metodos de apertura y cierre de la Conexion public void insertContact(lContact contact) { try { conection.Open(); //se realizara el Query(Instruccion SQL) en ves de realizarlo por un Procedimiento Almacenado string query = @"insert into Contact (Nombres,Apellidos,Telefono,Direccion) Values (@Nombres,@Apellidos,@Telefono,@Direccion) "; ////esta es la forma extendida //SqlParameter nombre = new SqlParameter(); //nombre.ParameterName = "@Nombres"; //nombre.Value = contact.Nombres; //nombre.DbType = System.Data.DbType.String; //esta es la forma normal por parametro y valor SqlParameter nombre = new SqlParameter("@Nombres", contact.Nombres); SqlParameter apellido = new SqlParameter("@Apellidos", contact.Apellidos); SqlParameter telefono = new SqlParameter("@Telefono", contact.Telefono); SqlParameter direccion = new SqlParameter("@Direccion", contact.Direccion); SqlCommand command = new SqlCommand(query, conection); command.Parameters.Add(nombre); command.Parameters.Add(apellido); command.Parameters.Add(telefono); command.Parameters.Add(direccion); command.ExecuteNonQuery(); } catch (Exception) { throw; } finally { conection.Close(); } }
public lContact guardarContact(lContact contact) { if (contact.Id == 0) { cnx.insertContact(contact); } else { cnx.updateContact(contact); } return(contact); }
public void loadContact(lContact contact) { lcontact = contact; if (contact != null) { limpiarFContact(); gTxtNombre.Text = contact.Nombres; gTxtApellidos.Text = contact.Apellidos; gTxtTelefono.Text = contact.Telefono; gTxtDireccion.Text = contact.Direccion; } }
private void insertarContacto() { lContact contact = new lContact(); contact.Nombres = gTxtNombre.Text; contact.Apellidos = gTxtApellidos.Text; contact.Telefono = gTxtTelefono.Text; contact.Direccion = gTxtDireccion.Text; contact.Id = lcontact != null ? lcontact.Id : 0; data.guardarContact(contact); }