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!