views:

383

answers:

5

I have a small C# function that takes any generic SQLDataReader and converts it to a csv file. However I now need to ignore 1 column from a datareader that gets passed into it.

How can I remove 1 column from the SQLDataReader?

This column is needed in the query mainly for ordering but is used in another area on the same pageload, but I do not wish for it to be displayed in the csv file that I will be generating.

EDIT: I think that I was somewhat missleading, The column that I wish to delete is used in other cases, just not this one, So just removing from the query will work but would cause me to have to run the query twice, instead of only once, and it is a larger query.

A: 

You don't need to include a column used in the order by clause of a sql query in the result set. Can you just leave it out of the select statement?

Steve Willcock
A: 

You don't remove a column from a datareader. If the data is not needed, then don't read the column. If you need the column data for sorting, then read it. When writing your CSV file simple omit the data. Writing the CSV file is not related to reading with the SqlDataReader.

ka3751
A: 

Add a parameter including an list of columns to ignore.

I sometimes have parameters like ignore_columns, include_columns, etc. If an ignore_columns parameter is provided and a column is in the list, it is ignored. If an include_columns parameter is provides and a column is not in the list it is ignored.

Cade Roux
A: 

You cannot! You will need to modify the code which generates the CSV to process only the needed column(s) and ignore the other column(s).

Cerebrus
+3  A: 

Pass an array of integers you wish to hide. The integers correspond to the index of the columns you want to turn off. You could create an overloaded method so you don't have to change the code in your program where you are calling it and printing each column.

(I'm assuming a lot here but the overload is the key bit)

public void MakeMyCSV(string MyFileName, SQLDataReader DataToOutput) 
{
    MakeMyCSV(MyFileName, DataToOutput, null);
}

public void MakeMyCSV(string MyFileName, SQLDataReader DataToOutput, int[] ExcludedColumns) //if this is your current method signature...
{
    //iterate through each record
    //    iterate through each column
    //        if ExcludedColumns is not null then see if this column is in it
    //            if it is not in it, output it
}
Neil Trodden