/// <summary> /// Delete /// </summary> /// <returns>void</returns> /// <Date></Date> /// <Author>hcmoreno</Author> public static void Delete(Rol pRol) { SqlConnection connection = null; String strCnn; SqlCommand cmd = new SqlCommand(); try { strCnn = CONNECTION_STRING; connection = new SqlConnection(strCnn); String sql = "delete from roles where id_rol=@id_rol"; cmd.CommandText = sql; cmd.Connection = connection; cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue("@id_rol", pRol.Id_rol); connection.Open(); cmd.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } finally { cmd.Dispose(); connection.Close(); } }
/// <summary> /// GetAll /// </summary> /// <Author>hcmoreno</Author> public static List<Rol> GetAll() { SqlConnection connection = null; SqlCommand cmd = new SqlCommand(); SqlDataReader reader = null; try { connection = new SqlConnection(CONNECTION_STRING); String sql = "select id_rol, descripcion from roles"; cmd.CommandText = sql; cmd.Connection = connection; cmd.CommandType = CommandType.Text; connection.Open(); reader = cmd.ExecuteReader(); List<Rol> roles = new List<Rol>(); while (reader.Read()) { Rol oRol = new Rol(); oRol.Id_rol = (Int32)reader["id_rol"]; oRol.Descripcion = (String)reader["descripcion"]; roles.Add(oRol); } return roles; } catch (Exception ex) { throw ex; } finally { cmd.Dispose(); connection.Close(); reader.Close(); } }
/// <summary> /// Insert /// </summary> /// <Author>hcmoreno</Author> public static int Insert(Rol pRol) { SqlConnection connection = null; SqlCommand cmd = new SqlCommand(); try { connection = new SqlConnection(CONNECTION_STRING); String sql = "insert into roles(id_rol,descripcion) OUTPUT Inserted.id_rol " + "values(@id_rol, @descripcion)"; cmd.CommandText = sql; cmd.Connection = connection; cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue("@id_rol", pRol.Id_rol); cmd.Parameters.AddWithValue("@descripcion", pRol.Descripcion); connection.Open(); int id = (int)cmd.ExecuteScalar(); return id; } catch (Exception ex) { throw ex; } finally { cmd.Dispose(); connection.Close(); } }
public static Rol GetById(int id) { SqlDataReader reader = null; SqlConnection connection = null; SqlCommand cmd = new SqlCommand(); try { connection = new SqlConnection(CONNECTION_STRING); String sql = "select id_rol, descripcion from roles where id_rol = @id_rol"; cmd.CommandText = sql; cmd.Connection = connection; cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue("@id_rol", id); connection.Open(); reader = cmd.ExecuteReader(); Rol Rol = null; if (reader.Read()) { Rol = new Rol(); Rol.Id_rol = (Int32)reader["id_rol"]; Rol.Descripcion = (String)reader["descripcion"]; } return Rol; } catch (Exception ex) { throw ex; } finally { cmd.Dispose(); connection.Close(); reader.Close(); } }