Create Overload of its Methods, and optimize code for unnecessary code repetition. I made an example, but, it is as simple as possible for understanding, since I would do more optimized still these methods.
public class Filtro
{
private String SQLDefault
{
get
{
return @"select pro_codigo, set_codigo, pro_nome,
pro_valor, pro_localizacao, pro_descricao,
pro_cestabasica, pro_imagem, pro_codigobarras
from produtos where 1=1";
}
}
public IList<Produtos> SelectFilter(Int32 CodigoProduto)
{
ConexaoBancoDados conexaoDB = new ConexaoBancoDados();
MySqlConnection con = null;
MySqlCommand cmd = null;
String SQLComplete = SQLDefault;
IList<Produtos> Produtos = null;
try
{
con = new MySqlConnection(conexaoDB.getStringConexao());
con.Open();
SQLComplete += " AND pro_codigo = @pro_codigo";
cmd = new MySqlCommand(SQLComplete, con);
cmd.Parameters.Add("@pro_codigo", MySqlDbType.Int32).Value = CodigoProduto;
using (MySqlDataReader dr = cmd.ExecuteReader())
{
if (dr.HasRows)
{
Produtos = new List<Produtos>();
while (dr.Read())
{
Produtos prod = new Produtos();
prod.pro_codigo = dr.GetInt32(0);
prod.set_codigo = dr.GetInt32(1);
prod.pro_nome = dr.GetString(2);
prod.pro_valor = dr.GetDouble(3);
prod.pro_localizacao = dr.GetString(4);
prod.pro_descricao = dr.GetString(5);
prod.pro_cestabasica = dr.GetString(6);
prod.pro_imagem = dr.GetString(7);
prod.pro_codigobarras = dr.GetString(8);
Produtos.Add(prod);
}
}
}
cmd.Dispose();
con.Close();
con.Dispose();
}
catch {
if (cmd != null ){
cmd.Dispose();
}
if (con != null){
if (con.State == System.Data.ConnectionState.Open){
con.Close();
}
con.Dispose();
}
}
return Produtos;
}
public IList<Produtos> SelectFilter(String NomeProduto)
{
ConexaoBancoDados conexaoDB = new ConexaoBancoDados();
MySqlConnection con = null;
MySqlCommand cmd = null;
String SQLComplete = SQLDefault;
IList<Produtos> Produtos = null;
try
{
con = new MySqlConnection(conexaoDB.getStringConexao());
con.Open();
SQLComplete += " AND pro_nome like @pro_nome";
cmd = new MySqlCommand(SQLComplete, con);
cmd.Parameters.Add("@pro_nome", MySqlDbType.String).Value = string.Format("%{0}%", NomeProduto);
using (MySqlDataReader dr = cmd.ExecuteReader())
{
if (dr.HasRows)
{
Produtos = new List<Produtos>();
while (dr.Read())
{
Produtos prod = new Produtos();
prod.pro_codigo = dr.GetInt32(0);
prod.set_codigo = dr.GetInt32(1);
prod.pro_nome = dr.GetString(2);
prod.pro_valor = dr.GetDouble(3);
prod.pro_localizacao = dr.GetString(4);
prod.pro_descricao = dr.GetString(5);
prod.pro_cestabasica = dr.GetString(6);
prod.pro_imagem = dr.GetString(7);
prod.pro_codigobarras = dr.GetString(8);
Produtos.Add(prod);
}
}
}
cmd.Dispose();
con.Close();
con.Dispose();
}
catch
{
if (cmd != null)
{
cmd.Dispose();
}
if (con != null)
{
if (con.State == System.Data.ConnectionState.Open)
{
con.Close();
}
con.Dispose();
}
}
return Produtos;
}
public IList<Produtos> SelectFilter(Double ValorProduto)
{
ConexaoBancoDados conexaoDB = new ConexaoBancoDados();
MySqlConnection con = null;
MySqlCommand cmd = null;
String SQLComplete = SQLDefault;
IList<Produtos> Produtos = null;
try
{
con = new MySqlConnection(conexaoDB.getStringConexao());
con.Open();
SQLComplete += " AND pro_valor = @pro_valor";
cmd = new MySqlCommand(SQLComplete, con);
cmd.Parameters.Add("@pro_valor", MySqlDbType.Double).Value = ValorProduto;
using (MySqlDataReader dr = cmd.ExecuteReader())
{
if (dr.HasRows)
{
Produtos = new List<Produtos>();
while (dr.Read())
{
Produtos prod = new Produtos();
prod.pro_codigo = dr.GetInt32(0);
prod.set_codigo = dr.GetInt32(1);
prod.pro_nome = dr.GetString(2);
prod.pro_valor = dr.GetDouble(3);
prod.pro_localizacao = dr.GetString(4);
prod.pro_descricao = dr.GetString(5);
prod.pro_cestabasica = dr.GetString(6);
prod.pro_imagem = dr.GetString(7);
prod.pro_codigobarras = dr.GetString(8);
Produtos.Add(prod);
}
}
}
cmd.Dispose();
con.Close();
con.Dispose();
}
catch
{
if (cmd != null)
{
cmd.Dispose();
}
if (con != null)
{
if (con.State == System.Data.ConnectionState.Open)
{
con.Close();
}
con.Dispose();
}
}
return Produtos;
}
public IList<Produtos> SelectFilter(object value)
{
Type _type = value.GetType();
IList<Produtos> Produtos = null;
switch (_type.FullName.ToLower())
{
case "system.string":
{
Produtos = this.selectFilter(((string)value));
break;
}
case "system.int":
case "system.int32":
{
Produtos = this.selectFilter(((Int32)value));
break;
}
case "system.double":
{
Produtos = this.selectFilter(((double)value));
break;
}
}
return Produtos;
}
}
The method SelectFilter, has 4 overloads, one of the type Int32, the other kind String, the other kind Double and the last of its kind Object, this is magic where the compiler will decide which method to execute according to the informed framework.
The SelectFilter of the Paramento Object it has a pecularity, where it will decide which type is the value informed and redirect to the correct method according to type.
References:
Could you post the code as text even instead of image? You can edit the question to include. The button
{}editor will help you format code as code. So whoever helps you has a starting point without having to type in the entire image code. Thank you.– bfavaretto
Prefer to post the code instead of the screenshot. I don’t know exactly where your question is. Is the difficulty in C# code or SQL? What can’t you do? This one
where 1=1I already think it’s a beautiful gambiarra.– Maniero
this Where 1=1 is an example only
– anderson seibert
I am unable to create the condition if the name is entered it searches by name, if it is entered the code will fetch the code.
– anderson seibert
supported the suggestion to post code as text, facilitates reading, editing and copy/paste to editors for testing and inspection in code
– Marcelo Bezerra bovino
@Andersonseibert I still don’t know if I understand since you haven’t added new information in your comment. I’ll kick something: would be missing a
cmd.CommandText = sql;before you can add some parameter?– Maniero