0

I need help to develop a logic. Take example data below:

For example:

Emp table

ID  | Fname   | Lname    | Salary  | DeptID
---------------------------------------
1   | John    | Smith    | 50000   | 1
2   | David   | Robinson | 80000   | 2
3   | Frank   | Adams    | 45000   | 10

My codes:

        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();

            SqlCommand cmd = new SqlCommand("Select * from emp", conn);
            rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {
                string EmpID = rdr[0].ToString();
                string Fname = rdr[1].ToString();
                string Lname = rdr[2].ToString();
                double sal = Convert.ToDouble(rdr[3]);
                int deptID = Convert.ToInt(rdr[4]);
            }
        }

What happen is, inside the while (rdr.Read()), the reader will go to the first row and read the first column (ID), second column (Fname), third column (Lname) etc. and once it is done with the first row, it will move to the second row first column, second column and so on.

How do I change my code so that the reader will loop in a way where: It will go to the first column then loop through all row before going to the next column? (The reader must NOT start reading the second column before finishing all rows in the first column.

Note:

  1. The reason I need this kind of looping because I have specific task that requires me (for example, to compare all data in the first column, then taking the outcome from first column and compare to the next column)
  2. There are around 500 columns and 1000 rows in each table, performance is a concern.
  3. I need to reuse the same code for all other 80+ tables, thus I cannot hardcode the column name.

I'm not restricted to use only ADO.NET, but I'm not familiar with Entity Framework, LINQ etc, so if you have any other way to query the database, please let me know

5
  • double sal = rdr[3].ToString() that doesn't compile in C#, use the correct types and methods, f.e. rdr.GetDouble Commented Mar 13, 2014 at 21:21
  • @TimSchmelter sorry, I wasn't careful about the datatype while producing the dummy sample data. Commented Mar 13, 2014 at 21:22
  • Why would you want to do this? This isn't how DataReaders work. Heck, it's not how traditional relational databases work. Commented Mar 13, 2014 at 21:23
  • @MattHamilton It is hard to explain... basically I'm dealing with legacy database from the 1960s, so yeah, it is definitely not relational database Commented Mar 13, 2014 at 21:24
  • Don't use a Reader. Use an Adapter and fill a DataTable instead. Commented Mar 13, 2014 at 21:27

1 Answer 1

1

A datareader is forward-only so you can't do this.

2.There are around 500 columns and 1000 rows in each table, performance is a concern

If you only have 1000 rows and 500 columns, performance is unlikely to be a concern.

Read all the data into a suitable object (DataTable, or list of Employee objects), then do your stuff.

Sign up to request clarification or add additional context in comments.

2 Comments

The problem is, I need to reuse the same code for all 80+ tables, I cannot just create one Employee Object because the other tables may use other classes. Most importantly, I simply CANNOT create a class because I DO NOT know what are the attributes exist in which table. (Unless I go through all 80 tables and 500 column manually to come out 80 classes for each table)
@C.J. - then try using a DataTable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.