Populating DataView from DataReader

Following Method will populate DataView from a DataReader


private DataView PopulateDataView(IDataReader dataReader, string tableName)

    {

        DataTable dataReaderTable = new DataTable(tableName);

        try

        {

            for (int count = 0; count < dataReader.FieldCount; count++)

            {

                DataColumn tempCol = new DataColumn(dataReader.GetName(count), dataReader.GetFieldType(count));

                dataReaderTable.Columns.Add(tempCol);

            }

            while (dataReader.Read())

            {

                DataRow dr = dataReaderTable.NewRow();

                for (int i = 0; i < dataReader.FieldCount; i++)

                {

                    dr[i] = dataReader.GetValue(dataReader.GetOrdinal(dataReader.GetName(i)));

                }

                dataReaderTable.Rows.Add(dr);

            }

            return dataReaderTable.DefaultView;

        }

        catch

        {

            return null;

        }

    }

Ebook Download
View all
FileInfo in C#
Read by 9.7k people
Download Now!
Learn
View all