-4

I have SqlDataReader that fill with ExecuteDataReader statement. I want to change some column name in DataReader after fill.

for example after fill datareader i have two column (A,B) i want to change colum 'A' to 'a'(convert to lowercase)befor binde to grid

sqlDataReader reader;
reader.executedatareadet();

like reader.GetName(i) i want to be abe somthing like

reader.SetName(i)

but it seams that we cant change the datareader column name

abianari
  • 343
  • 7
  • 19
  • I'm not sure what you're asking. Can you provide some code? – Mikey Mouse Dec 18 '12 at 14:34
  • Can you specify "i want to change some column name"? It might help to see what you have tried. – Tim Schmelter Dec 18 '12 at 14:34
  • 1
    Please post a specific question. A post that is nothing more than "I want to do something" might be proper at some discussion forums, but is not appropriate for Stack Overflow. – mah Dec 18 '12 at 14:37

3 Answers3

2

The DataReader is only for reading the returned data. You read the values and you put them in your own data structures or variables.

The only way to change the structure of the data reader is on the data origin, i.e. the SQL query which formats the returned data.

Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
  • alex tahnk you for youre reponse .you mean it is not possible to change the column name in datareader after fill and befor bindig to grid..? – abianari Dec 18 '12 at 14:47
  • You should use a [SqlDataAdapter](http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.aspx). You may call the `Fill(DataTable)` or `Fill(DataSet)` methods to populate a `DataTable` object. Then, you bind it to the grid's data source. – Alex Filipovici Dec 18 '12 at 14:48
0

You'll need to store the data in your own variable then. Run your executeReader() and put the result in a var dataHolder. Then loop through the dataHolder with a

   foreach(var x in dataHolder)
   {
        x.Property = x.Property.ToLower(); //Or something like this
   }

Then when you're finished, you can put the dataHolder in a grid or something

Mikey Mouse
  • 2,968
  • 2
  • 26
  • 44
  • thanks 'Mikey' but for large DataSet it is too time consumming to put reasult to another dataholer.i don't want to copy data to another dataholder.it has overhead – abianari Dec 18 '12 at 14:53
  • Ok, I didn't realize you were dealing with a very large dataset. You could try a Linq2Sql approach that would let you pull the data directly into a container with Properties that you can update, I think you can even do the "toLower" stuff directly on the Database side with Linq2Sql – Mikey Mouse Dec 18 '12 at 14:57
0

Introduce a middle model layer between the reading and binding. After you read the data from data reader, load the data into your intermediate model and then make changes to the data there and then bind your model collection to the datasource.

Azhar Khorasany
  • 2,712
  • 16
  • 20