Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Browsing datareader results
Message
 
To
21/08/2008 14:45:31
General information
Forum:
ASP.NET
Category:
Forms
Environment versions
Environment:
C# 3.0
Miscellaneous
Thread ID:
01339462
Message ID:
01341290
Views:
13
> Your and MSDN sample require to know SQL select statement.
> However this is not avaliable in my case.
> My method receives an opened datareader.
> SQL select statement is not avaliable.
> I think I must implement the following:

> 1. Read column names from datareader using FataReader.GetName()
> 2. Read first row from datareader to determine column types.
> 3. Create object with those properties and names using MS Dynamic Linq library.
> 4. Read whole datareader to fill List object with it contents.
> 5. Assing result list to DataGridView DataSource.

Ok, if you have just a DataReader object (But in your code at first post you had a
string in the name of "query" it was your Sql Select statement - Anyway)

- It's possible to make a List of DataColumn class
- Then loop by FieldCount proprty of DataReader and add DataColumn to the List
(with proper type and the name name as comes by data reader)
- Instantiate a DataTable object and add the DataColumn(s) from List to it
- Loop through data reader and read the row(s) and add row(s) to the DataTable and fill its column(s)
- Assign DataTable to DataSource of DataGridView

You can do something like this:
(just imagine you have received myDR as a DataReader object)
                List<DataColumn> dataClmnList = new List<DataColumn>(); 
                for( int i = 0; i < myDR.FieldCount; i ++ )
                    dataClmnList.Add( new DataColumn( myDR.GetName(i), myDR.GetFieldType(i) ) );

                DataTable myDT = new DataTable();
                for( int i = 0; i < myDR.FieldCount; i ++ )
                    myDT.Columns.Add( dataClmnList[i] );

                int iCntr = 0;
                while (myDR.Read())
                {
                    myDT.Rows.Add();
                    for (int i = 0; i < myDR.FieldCount; i++)
                        myDT.Rows[iCntr][i] = myDR.GetValue(i);
                    iCntr++;
                }

                dgvDataReader.DataSource = myDT;
The complete code could be as following (just for test):
            string mySqlCnStr = @"Data Source=.\SQLEXPRESS;Initial Catalog=myDataReader;Integrated Security=True";
            using (SqlConnection mySqlCn = new SqlConnection(mySqlCnStr))
            {
                SqlCommand mySqlCmd = new SqlCommand("select * from myTable", mySqlCn);
                mySqlCn.Open();
                SqlDataReader myDR = mySqlCmd.ExecuteReader();

                // Now just it uses myDR - DataReader 
                List<DataColumn> dataClmnList = new List<DataColumn>(); 
                for( int i = 0; i < myDR.FieldCount; i ++ )
                    dataClmnList.Add( new DataColumn( myDR.GetName(i), myDR.GetFieldType(i) ) );

                DataTable myDT = new DataTable();
                for( int i = 0; i < myDR.FieldCount; i ++ )
                    myDT.Columns.Add( dataClmnList[i] );

                int iCntr = 0;
                while (myDR.Read())
                {
                    myDT.Rows.Add();
                    for (int i = 0; i < myDR.FieldCount; i++)
                        myDT.Rows[iCntr][i] = myDR.GetValue(i);
                    iCntr++;
                }

                dgvDataReader.DataSource = myDT;
                mySqlCn.Close();
            }
Hope this one helps
Previous
Reply
Map
View

Click here to load this message in the networking platform