0

I have the following code:

string result = null;
        string query = "SELECT * FROM Energy";
        int index = 0;

        sql.ConnOpen();

        cmd = sql.Command(query);

        try
        {
            reader = cmd.ExecuteReader();

            while (reader.Read())
            {

                result += "Date: " + reader["Date"].ToString() + "Here goes columnName " +  (reader[index++].ToString().Equals("0") ? "No" : "Yes") + "\n";
            }
        }

Where it says: Here goes columnName (in the while loop), I want to add the name of the current column. How can I obtain this? So notice that I want the name of the current column, NOT the value at the current column

States
  • 141
  • 11

1 Answers1

1

SqlDataReader has a GetName method which takes the column index and returns the column name. The GetOrdinal method works in reverse to return the column index when passed the column name.

cjthompson
  • 26
  • 5