Prefetch

  • Thread starter Thread starter Peter Osawa
  • Start date Start date
P

Peter Osawa

Hi,
I'm receveing an IDataReader object that I bind to a combo list.

I'd like to be able to customize that object before binding it to combo,
but I don't know how to do it...

Ideal would be something aka:

foreach('MyField' in IDataReader.Rows){

If ('MyField'== "Male")
IDataReader.Row.Remove('MyField');
};

TIA
 
Peter Osawa said:
Hi,
I'm receveing an IDataReader object that I bind to a combo list.

I'd like to be able to customize that object before binding it to combo,
but I don't know how to do it...

Ideal would be something aka:

foreach('MyField' in IDataReader.Rows){

If ('MyField'== "Male")
IDataReader.Row.Remove('MyField');
};

Use a DataTable instead of a DataReader. Then you can do whatever you want
to it.

David
 
The problem es that I receive the IDataReader and I'm not allowed to
change that...
 
Peter Osawa said:
The problem es that I receive the IDataReader and I'm not allowed to
change that...

The following will convert the Reader to a DataTable. I don't know how to do
this using any methods on any of the Sql objects, because I haven't spent much
time with the Reader specifically. So I wrote this quickly to help you in your
situation.

Hope it helps!

Mythran


' --------------------------------------------------------------------------
' Description:
' Sample MAIN() console application.
'
Sub Main()
Dim conn As SqlConnection
Dim read As SqlDataReader
Dim adap As SqlDataAdapter
Dim dt As DataTable
Dim cmd As SqlCommand

conn = New
SqlConnection("Server=MyServer;Database=MyDatabase;Trusted_Connection=True")
conn.Open()

cmd = New SqlCommand("SELECT * FROM tblDepartment", conn)
read = cmd.ExecuteReader()

' Convert the reader to a data table.
dt = ConvertReaderToDataTable(read)

' Close the reader and the database connection.
read.Close()
conn.Close()

' Write the first row of the data table.
For Each col As DataColumn In dt.Columns
Console.WriteLine("Name: " & col.ColumnName & _
" - Value: " & dt.Rows(0)(col).ToString())
Next
End Sub

' --------------------------------------------------------------------------
' Description:
' Convert the reader to a data table.
'
Public Function ConvertReaderToDataTable(ByVal Reader As SqlDataReader) As
DataTable
Dim dt As DataTable
Dim dr As DataRow

' Create the data table.
dt = New DataTable()

' Add the columns from the reader to the schema of the data table.
For i As Integer = 0 To Reader.FieldCount - 1
dt.Columns.Add(Reader.GetName(i), Reader.GetFieldType(i))
Next

' Add each row in the reader to the data table.
While Reader.Read()
' Create the new row.
dr = dt.NewRow()

For i As Integer = 0 To Reader.FieldCount - 1
dr(i) = Reader(i)
Next

' Add the row to the table.
dt.Rows.Add(dr)
End While

' Return the DataTable object.
Return dt
End Function
 
Sorry about my previous post in VB.Net, it's not hard to convert to C#, but
still, sorry ... didn't notice this was the C# group until after I posted
heh...let me know if you have problems converting to C#...if you do I'll go ahead
and convert it to the equivalent.

....

Mythran
 
Peter Osawa said:
The problem es that I receive the IDataReader and I'm not allowed to
change that...

Well, that's bad design.

But you can create the DataTable yourself and read through the DataReader
and add rows to the DataTable. You're replicating code that already exists
in the DataAdapter.

David
 
I'll try it

Thank you
Sorry about my previous post in VB.Net, it's not hard to convert to C#, but
still, sorry ... didn't notice this was the C# group until after I posted
heh...let me know if you have problems converting to C#...if you do I'll go ahead
and convert it to the equivalent.

...

Mythran
 
Back
Top