public void DeleteUsuario(UsuarioBean u) { try { using (IDbConnection connection = CreateDBConnection()) { using (IDbTransaction transaction = connection.BeginTransaction()) { try { using (IDbCommand command = connection.CreateCommand()) { command.Transaction = transaction; //le agregamos la transaccion command.CommandText = "DELETE FROM dbo.RolUsuario WHERE UsuarioId=@id"; IDbDataParameter idParam = command.CreateParameter(); idParam.ParameterName = "id"; idParam.Value = u.Id; command.Parameters.Add(idParam); command.ExecuteNonQuery(); //esto ya no requiere de parametro command.CommandText = "DELETE FROM dbo.Usuario WHERE UsuarioId = @id"; command.ExecuteNonQuery(); } transaction.Commit(); //realiza el commit de la trasaccion } catch (Exception) { transaction.Rollback(); //si falla algo realizamos el rollback } } } } catch (Exception ex) { throw ex; } }
public void SaveUsuario(UsuarioBean u) { try { using (IDbConnection connection = CreateDBConnection()) { using (IDbCommand command = connection.CreateCommand()) { if (u.Id == 0) { //update command.CommandText = @"INSERT INTO dbo.Usuario(NombreUsuario,Contrasena) VALUES(@usuario, @pwd)"; } else { //insert command.CommandText = @"UPDATE dbo.Usuario SET NombreUsuario=@usuario, Contrasena=@pwd WHERE UsuarioId=@id"; IDbDataParameter idP = command.CreateParameter(); idP.ParameterName = "id"; idP.Value = u.Id; command.Parameters.Add(idP); } //agregamos los parametros IDbDataParameter usuarioP = command.CreateParameter(); usuarioP.ParameterName = "usuario"; usuarioP.Value = u.UsuarioName; command.Parameters.Add(usuarioP); IDbDataParameter pwdP = command.CreateParameter(); pwdP.ParameterName = "pwd"; pwdP.Value = u.Pwd; command.Parameters.Add(pwdP); //ejecutamos int i = command.ExecuteNonQuery(); } } } catch (Exception ex) { throw ex; } }
private void btnEliminar_Click(object sender, EventArgs e) { UsuarioBean u = new UsuarioBean(); string id = string.IsNullOrEmpty(txtID.Text) ? "0" : txtID.Text; u.Id = Convert.ToInt32(id); u.UsuarioName = txtUsuario.Text; u.Pwd = txtPwd.Text; //guardar usuario new UsuarioDAO().DeleteUsuario(u); txtID.Text = string.Empty; txtUsuario.Text = string.Empty; txtPwd.Text = string.Empty; RefreshUsers(); }