How to get only some Columns DataTable

0

Good morning, I have this Web Service that provides me with all the data of a table and all the columns, how do I select only some Columns and not all?

[WebMethod]
    public DataTable consultaIndividualSUCIS(int tipoCodigoEntidad, int estadoAutorizacion)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {

            using (SqlCommand cmd = new SqlCommand("SELECT * FROM Idoneidad_Funcionarios WHERE tipoCodigoEntidad = @tipoCodigoEntidad AND estadoAutorizacion = @estadoAutorizacion"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Parameters.AddWithValue("@tipoCodigoEntidad", tipoCodigoEntidad);  // <-- Este es el parámetro de SQL que estás recibiendo cómo parámetro en tu método
                    cmd.Parameters.AddWithValue("@estadoAutorizacion", estadoAutorizacion);  // <-- Este es el parámetro de SQL que estás recibiendo cómo parámetro en tu método

                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        dt.TableName = "Idoneidad_Funcionarios";

                        sda.Fill(dt);
                        return dt;
                    }
                }
            }
        }
    }

the table is as follows:

+---------+-----------+-----------+------------+
|tipCod   | Nombre    |estadoAut  |fechaInicio |
+---------+-----------+-----------+------------+
|101      |Pepe       |Activo     |2017/05/01  |
+---------+-----------+-----------+------------+

In the results I just want to get and that the search parameter is [tipCod]:

+-----------+-----------+------------+
| Nombre    |estadoAut  |fechaInicio |
+-----------+-----------+------------+
|Pepe       |Activo     |2017/05/01  |
+-----------+-----------+------------+
    
asked by Vulpex 26.05.2017 в 16:31
source

2 answers

1
SELECT Nombre, 
       estadoAut,
       fechaInicio
  FROM Idoneidad_Funcionarios 
 WHERE tipoCodigoEntidad = @tipoCodigoEntidad
   AND estadoAutorizacion = @estadoAutorizacion
    
answered by 26.05.2017 / 18:08
source
1

Good afternoon,

what you should do is modify the statement of the sqlcommand so that it only brings the columns you need.

 using (SqlCommand cmd = new SqlCommand("SELECT Nombre,estadoAut,fechaInicio FROM Idoneidad_Funcionarios WHERE tipoCodigoEntidad = @tipoCodigoEntidad AND estadoAutorizacion = @estadoAutorizacion"))
    
answered by 26.05.2017 в 20:40