Can anyone tell me if there is an easier or more correct way to call two selects from different tables?

Asked

Viewed 35 times

-2

using BRQ_Jobs.Modelo;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BRQ_Jobs.DAL
{
    public class PesquisaDALComandos
    {
        string conecta = @"Integrated Security = SSPI; Persist Security Info=False;Initial Catalog = BRQ_JOBS; Data Source = NBBV027097";
        SqlConnection conexao = null;
        SqlCommand comando;

        CadastroRecrutador recrutador = new CadastroRecrutador();
        CadastroVagas vaga = new CadastroVagas();

        //Método para listar os dados do banco
        public DataTable Listar()
        {           
            try
            {
                using (conexao = new SqlConnection(conecta))                    
                {                   
                    if (!recrutador.Equals(vaga))
                    {
                        comando = new SqlCommand("select distinct COD_REC,NOME,SITUACAO from RECRUTADOR", conexao);                                     
                    }
                    else if (!vaga.Equals(recrutador))
                    {
                        comando = new SqlCommand("select distinct COD_VAGA,NOME,DESCRICAO,SALARIO,SITUACAO from VAGA", conexao);
                    }
                    SqlDataAdapter da = new SqlDataAdapter();
                    DataTable dt = new DataTable();
                    da.SelectCommand = comando;
                    da.Fill(dt);
                    return dt;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        //Método para pesquisar ao digitar
        public DataTable Pesquisar(PesquisaModel pesquisa)
        {
            try
            {
                using (conexao = new SqlConnection(conecta))
                {
                    SqlDataAdapter da = new SqlDataAdapter();
                    DataTable dt = new DataTable();

                    if (!recrutador.Equals(vaga))
                    {
                        comando = new SqlCommand("select distinct COD_REC,NOME,SITUACAO from RECRUTADOR where NOME like '%' + @nome + '%' order by NOME", conexao);                   
                    }
                    else if (!vaga.Equals(recrutador))
                    {
                        comando = new SqlCommand("select distinct COD_VAGA,NOME,DESCRICAO,SALARIO,SITUACAO from VAGA where NOME like '%' + @nome + '%' order by NOME", conexao);
                    }

                    comando.Parameters.AddWithValue("@nome", pesquisa.Nome);
                    da.SelectCommand = comando;
                    da.Fill(dt);
                    return dt;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                conexao.Close();
            }
        }   
    }
}
  • First you’re doing wrong, using SqlDataAdapter to return a DataTable is not necessary, beyond very low performance. Only your code has no way to help much, need to improve your question

1 answer

0

It is possible. This requires the UNION. Taking your case as an example:

SELECT COD, Nome
FROM (
  SELECT COD_REC AS COD, Nome
  FROM RECRUTADOR
  UNION ALL
  SELECT COD_VAGA, Nome
  FROM VAGA
) AS t1
WHERE t1.COD = 1

Follow link with example in Sql Fiddle: http://sqlfiddle.com/#! 18/99ffc/9/0

  • is the other fields? can only make Union with the same amount of fields, this will not work

Browser other questions tagged

You are not signed in. Login or sign up in order to post.