I am not sure I am under the write forum but here is my problem:
I have a pretty simple program written that works just have a lot of repeated code and am not sure how to condense it in this situation.
The program reads in table names from the external SQL database and prints the table. I have attached the code for class I want to condense, if someone wants the main class I will attach that as well.

class Tableinfo
    {
        String tableName = "";
        int mone = 0;
        
        public Tableinfo(String name)
        {
            tableName= name;
        }
        public void printColHeaders()
        {
            SqlConnection conn = new SqlConnection("server=SHELI-PC\\SQLEXPRESS;database=Tables;integrated security=SSPI");
            SqlCommand command = conn.CreateCommand();
            command.CommandText = "SELECT * FROM " + tableName;
            conn.Open();
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.SchemaOnly);
            DataTable table = reader.GetSchemaTable();
            foreach (DataRow row in table.Rows)
            {
                Console.Write("{0}\t\t",row["ColumnName"]);
                mone++;
            }
            Console.WriteLine("");
            reader.Close();
            conn.Close();
        }
        public void printTableData()
        {
            SqlConnection conn = new SqlConnection("server=SHELI-PC\\SQLEXPRESS;database=Tables;integrated security=SSPI");
            SqlCommand command = conn.CreateCommand();
            command.CommandText = "SELECT * FROM " + tableName;
            conn.Open();
            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                for (int i = 0; i < mone; i++)
                {
                    Console.Write("{0}\t", reader[i]);
                }
                Console.WriteLine("");
            }
            Console.WriteLine("");
            reader.Close();
            conn.Close();
        }
    }

As you can see I have one function printing the coulmn headers the other printing the table data, but the bulk of the code is repeated.
Thanks in advance for your help!

You are using parameter CommandBehavior.SchemaOnly, so I think its not a problem in performance.

Edited 5 Years Ago by urtrivedi: n/a

Basically do this (crude, but effective). Am sure you can do the rest.

public void print(bool tableData)
{
  SqlConnection conn = new SqlConnection("server=SHELI-PC\\SQLEXPRESS;database=Tables;integrated security=SSPI");
  SqlCommand command = conn.CreateCommand();
  command.CommandText = "SELECT * FROM " + tableName;
  conn.Open();

  if (tableData)
  {
    // Table Data
  }
  else
  {
    // Column Headers
  }

  Console.WriteLine("");
  reader.Close();
  conn.Close();
}

Edited 5 Years Ago by pritaeas: n/a

Thanks urtrivedi, I think you are right and if probably doesn't make much of a difference, but its for a class and I will probably loose some points on code like that.
pritaeas, I am not understanding you idea so much, if I put the coulmn print in the else how will it ever reach there? I am a new to programming and need some guidance.

This question has already been answered. Start a new discussion instead.