17
I am developing an application in C# and I would like to know how to put together two results of two darlings SQL in one. I have the following code:
public List<MalaDireta> ObterMalaDireta()
{
List<MalaDireta> resultado = new List<MalaDireta>();
string sql_funcionario = "select id, nome, email from funcionario";
string sql_fornecedor = "select id, nome, email from fornecedor";
var conexao = new SqlConnection("string de conexão");
var comando = new SqlCommand(sql_funcionario, conexao);
try
{
conexao.Open();
// como eu monto meu comando com as duas queries?
var dataReader = comando.ExecuteReader();
while (dataReader.Read())
{
MalaDireta m = new MalaDireta();
m.Nome = dataReader["nome"];
m.Email = dataReader["email"];
resultado.Add(m);
}
}
catch
{
}
finally
{
conexao.Close();
}
return resultado;
}
Good observation. I just add that the choice between UNION and UNION ALL depends on the result you want (the question does not make this clear).
– bfavaretto
I agree with @utluiz, good observation. In this case, however, it is very unlikely that employees are also vendors and even if they are, they would probably have different id’s and emails.
– Ecil