0

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!

3
Contributors
5
Replies
7
Views
5 Years
Discussion Span
Last Post by Ssnowlin
0

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

Edited by urtrivedi: n/a

0

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 by pritaeas: n/a

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.