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!

Recommended Answers

All 5 Replies

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

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();
}

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.

You pass false as your function parameter: print(false); If you pass true it will print the table data.

it worked, thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.