C# Dataset select help RRS feed

  • Question

  • Hey everyone. Currently I am extracting a dataset through sql ado in visual studio. The dataset has many fields that I do not need. Is there a way to extract just the columns (not rows) that I want? Thanks.

    Heres a sample of my code: (the string sql contains my sql command)

    Code Snippet

    SqlDataAdapter daCust = new SqlDataAdapter(sql, con);

    DataSet ds = new DataSet();

    daCust.Fill(ds, "SLX");

    return ds;

    Thursday, June 21, 2007 12:04 PM

All replies

  • Easiest why is just to define the columns you want in the select statement of the sql.


    Code Snippet

    select column1, column2, column3
    from atable
    where somecondition = somevalue.

    Thursday, June 21, 2007 1:53 PM
  • You should probably change the SQL query, but to remove the column you need to access the table in the dataset.  If there is only one table you can use the index or you will have to use the name.  Here is how you can access the column to remove it.

    Code Snippet

    DataSet ds = new DataSet();

    ds.Tables[0].Columns.Remove("Name of Column");


    ds.Tables["Name of Table"].Columns.Remove("Name of Column");


    Thursday, June 21, 2007 4:25 PM