static public Library.Despesa FindById(long idDespesa) { SqlConnection conexao = null; SqlDataAdapter dap = null; DataSet ds = null; Library.Despesa despesa = null; try { conexao = new SqlConnection(global::Connection.Connection.String()); dap = new SqlDataAdapter("SELECT * FROM Despesa WHERE id='" + idDespesa + "'", conexao); ds = new DataSet(); dap.Fill(ds, "Despesa"); if (ds.Tables["Despesa"].Rows.Count == 1) { despesa = new Despesa(); despesa.Id = (long)ds.Tables["Despesa"].Rows[0]["id"]; despesa.Data = (DateTime)ds.Tables["Despesa"].Rows[0]["data"]; despesa.Descricao = ds.Tables["Despesa"].Rows[0]["descricao"].ToString(); despesa.Valor = (decimal)ds.Tables["Despesa"].Rows[0]["valor"]; despesa.VencimentoInicio = (DateTime)ds.Tables["Despesa"].Rows[0]["vencimentoInicio"]; despesa.VencimentoFim = (DateTime)ds.Tables["Despesa"].Rows[0]["vencimentoFim"]; despesa.repetitivo = (bool)ds.Tables["Despesa"].Rows[0]["repetitivo"]; despesa.ocorre = (string)ds.Tables["Despesa"].Rows[0]["ocorre"]; despesa.recor_dia = (int)ds.Tables["Despesa"].Rows[0]["recor_dia"]; despesa.recor_sem = (int)ds.Tables["Despesa"].Rows[0]["recor_sem"]; despesa.recorsem_lst = (string)ds.Tables["Despesa"].Rows[0]["recorsem_lst"]; despesa.recor_men = (int)ds.Tables["Despesa"].Rows[0]["recor_men"]; despesa.recor_ord = (int)ds.Tables["Despesa"].Rows[0]["recor_ord"]; } return(despesa); } catch (Exception ex) { Library.Diagnostics.Logger.Error(ex); } finally { conexao.Close(); dap.Dispose(); ds.Dispose(); } return(null); }
static public Library.Despesa FindDespesaById(long idDespesa) { SqlConnection conexao = null; SqlDataAdapter dap = null; DataSet ds = null; Library.Despesa despesa = null; try { conexao = new SqlConnection(global::Connection.Connection.String()); dap = new SqlDataAdapter("SELECT * FROM Despesa WHERE id='" + idDespesa + "'", conexao); ds = new DataSet(); dap.Fill(ds, "Despesa"); if (ds.Tables["Despesa"].Rows.Count == 1) { despesa = new Despesa(); despesa.Id = (long)ds.Tables["Despesa"].Rows[0]["id"]; despesa.Data = (DateTime)ds.Tables["Despesa"].Rows[0]["data"]; despesa.Descricao = ds.Tables["Despesa"].Rows[0]["descricao"].ToString(); despesa.Valor = (decimal)ds.Tables["Despesa"].Rows[0]["valor"]; } return(despesa); } catch (Exception ex) { Library.Diagnostics.Logger.Error(ex);; } finally { conexao.Close(); dap.Dispose(); ds.Dispose(); } return(null); }
static public List <Library.Despesa> FindAdvanced(params Library.Classes.QItem[] args) { SqlDataReader rdr = null; SqlConnection conexao = null; SqlCommand comando = null; try { conexao = new SqlConnection(global::Connection.Connection.String()); comando = new SqlCommand(); string query = "SELECT * FROM Despesa AS d "; int p = 0; string pre = ""; foreach (Library.Classes.QItem qi in args) { if (p == 0) { pre = "WHERE "; } else { pre = "AND "; } p++; switch (qi.Campo) { case "d.id": query += pre + "d.id = @id"; comando.Parameters.AddWithValue("@id", qi.Objeto); break; case "ORDER BY": query += " ORDER BY " + qi.Objeto; break; } } comando.CommandText = query; comando.Connection = conexao; conexao.Open(); rdr = comando.ExecuteReader(); List <Library.Despesa> despesas = new List <Library.Despesa>(); while (rdr.Read()) { Library.Despesa despesa = new Despesa(); despesa.Id = (long)rdr["id"]; despesa.Data = (DateTime)rdr["data"]; despesa.Descricao = rdr["descricao"].ToString(); despesa.Valor = (decimal)rdr["valor"]; despesas.Add(despesa); } return(despesas); } catch (Exception ex) { Library.Diagnostics.Logger.Error(ex); } finally { conexao.Close(); comando.Dispose(); } return(null); }
static public List <Library.Despesa> FindAdvanced(params Library.Classes.QItem[] args) { SqlDataReader rdr = null; SqlConnection conexao = null; SqlCommand comando = null; try { conexao = new SqlConnection(global::Connection.Connection.String()); comando = new SqlCommand(); string query = "SELECT * FROM Despesa AS d "; int p = 0; string pre = ""; foreach (Library.Classes.QItem qi in args) { if (p == 0) { pre = "WHERE "; } else { pre = " AND "; } p++; switch (qi.Campo) { case "d.id": query += pre + "d.id = @id"; comando.Parameters.AddWithValue("@id", qi.Objeto); break; case "d.data": query += pre + "(CONVERT(varchar, d.data, 103) = '@data')"; comando.Parameters.AddWithValue("@data", qi.Objeto); break; case "ORDER BY": query += " ORDER BY " + qi.Objeto; break; } } comando.CommandText = query; comando.Connection = conexao; conexao.Open(); rdr = comando.ExecuteReader(); List <Library.Despesa> despesas = new List <Library.Despesa>(); while (rdr.Read()) { Library.Despesa despesa = new Despesa(); despesa.Id = (long)rdr["id"]; despesa.Data = (DateTime)rdr["data"]; despesa.Descricao = rdr["descricao"].ToString(); despesa.Valor = (decimal)rdr["valor"]; despesa.VencimentoInicio = (DateTime)rdr["vencimentoInicio"]; despesa.VencimentoFim = (DateTime)rdr["vencimentoFim"]; if ((int)rdr["repetitivo"] == 1) { despesa.repetitivo = true; } else { despesa.repetitivo = false; } despesa.ocorre = (string)rdr["ocorre"]; despesa.recor_dia = (int)rdr["recor_dia"]; despesa.recor_sem = (int)rdr["recor_sem"]; despesa.recorsem_lst = (string)rdr["recorsem_lst"]; despesa.recor_men = (int)rdr["recor_men"]; despesa.recor_ord = (int)rdr["recor_ord"]; despesas.Add(despesa); } return(despesas); } catch (Exception ex) { Library.Diagnostics.Logger.Error(ex); } finally { conexao.Close(); comando.Dispose(); } return(null); }