I have a query versus an SQL database in C#. From this query I get a List like:
List<List<string>>()
this list can be changed to any other table type if required..
In Microsoft Server Management Studio it looks like this:
In Microsoft SQL Server Management Studio you can simply output the result as text, which looks like this:
Now as I am automating some tasks I will use C# rather than manually using MSSQL. Due to a GXP environment it is important to have every datachange in the Database Documented separately with a text report attached to a "ticket".
For now my query with Table Text generation looks like this:
SqlCommand command = new SqlCommand("SELECT '" + description + "' AS '" + description + "', CPPS_Site,Study_Id,CustNo,CPPS_Job FROM CDS.dbo.Studies " +
"WHERE Study_Id = " + studyID, con);
List<string> overview = new List<string>();
overview.Add("|=========================================================|");
overview.Add("|"+description + "\t|CPPS_Site\t|Study_ID\t|CustNo\t|CPPS_Job|");
overview.Add("|---------------------------------------------------------|");
using (SqlDataReader resultQuery = command.ExecuteReader())
{
var schemaTable = resultQuery.GetSchemaTable();
while (resultQuery.Read())
{
string build = "|";
build += resultQuery.GetSqlValue(0) + "\t|"
+ resultQuery.GetSqlValue(1) + "\t\t|"
+ resultQuery.GetSqlValue(2) + "\t\t|"
+ resultQuery.GetSqlValue(3) + "\t|"
+ resultQuery.GetSqlValue(4);
build += "|";
overview.Add(build);
}
overview.Add("|=========================================================|");
return overview;
}
Result:
while this is technically working and would also be accepted, I am personally very unpleased with this ugly solution. For instance with strings of different length, the format will not fit anymore. Do you know a more clean solution?



IEnumerable. You just don't know it... :-)