public static void ToConsole(this DbDataReader reader, int maxRow = 0) { while (reader.HasRows) { DataTable schemaTable = reader.GetSchemaTable(); var schema = schemaTable .AsEnumerable() .Select(row => new { Name = row.Field <string>("ColumnName"), Size = row.Field <int>("ColumnSize"), Type = row.Field <Type>("DataType") }); string[] headers = schema.Select(row => row.Name).ToArray(); var D = new OutputDataLine(cout.TrimWriteLine, headers.Length); D.MeasureWidth(schema.Select(row => row.Size).ToArray()); D.MeasureWidth(headers); D.MeasureWidth(schema.Select(row => row.Type).ToArray()); D.DisplayLine(); D.DisplayLine(headers); D.DisplayLine(); if (!reader.HasRows) { cout.WriteLine("<0 row>"); return; } object[] values = new object[headers.Length]; int count = 0; bool limited = false; while (reader.Read()) { reader.GetValues(values); D.DisplayLine(values); if (++count == maxRow) { limited = true; break; } } D.DisplayLine(); cout.WriteLine("<{0} row{1}> {2}", count, count > 1 ? "s" : "", limited ? "limit reached" : "" ); reader.NextResult(); } }
internal OutputDataTable(DataTable dt, Action <string> writeLine, bool vertical) { this.dt = dt; this.vertical = vertical; List <string> list = new List <string>(); foreach (DataColumn column in this.dt.Columns) { list.Add(column.ColumnName); } this.headers = list.ToArray(); if (vertical) { line = new OutputDataLine(writeLine, this.dt.Rows.Count + 1); } else { line = new OutputDataLine(writeLine, headers.Length); } }