Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>EDIT</h2> <p>Here's an example using the DataReader. You could be even more specific looking at NumericPrecision columns and such in the schema table, but I think this will get you started.</p> <pre><code>using (SqlConnection conn = new SqlConnection("Data Source=server;Initial Catalog=db;User Id=user;Password=pass;")) { using (SqlCommand cmd = new SqlCommand("select * from table", conn)) { conn.Open(); StreamWriter sw = new StreamWriter(File.Open("test.txt", FileMode.Append)); DataTable schema = null; using (SqlDataReader rdr = cmd.ExecuteReader()) { schema = rdr.GetSchemaTable(); for (int i = 0; i &lt; rdr.FieldCount; i++) { string name = schema.Rows[i]["ColumnName"].ToString(); sw.Write(name.PadRight(name.Length + Convert.ToInt32(schema.Rows[i]["ColumnSize"])) + " "); } sw.WriteLine(); for (int i = 0; i &lt; rdr.FieldCount; i++) { string name = schema.Rows[i]["ColumnName"].ToString(); sw.Write(new string('-', name.Length + Convert.ToInt32(schema.Rows[i]["ColumnSize"])) + " "); } rdr.Close();//can't have two open datareaders on the same connection rdr.Dispose(); sw.WriteLine(); while (dataReader.Read()) { for (int i = 0; i &lt; dataReader.FieldCount; i++) { string name = schema.Rows[i]["ColumnName"].ToString(); sw.Write(dataReader[i].ToString().PadRight(name.Length + Convert.ToInt32(schema.Rows[i]["ColumnSize"])) + " "); } sw.WriteLine(); } } sw.Close(); sw.Dispose(); } } </code></pre> <p>I couldn't spend enough time to find out how to do this with a DataReader, but if you use a DataAdapter instead, you can do this:</p> <pre><code> using (SqlConnection conn = new SqlConnection("Data Source=someserver;Initial Catalog=somedb;User Id=user;Password=password;")) { using (SqlCommand cmd = new SqlCommand("select * from sometable", conn)) { conn.Open(); using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) { DataSet set = new DataSet(); set.Tables.Add("Schema"); set.Tables.Add("Data"); adapter.Fill(set, "Data"); adapter.FillSchema(set, SchemaType.Source, "Schema"); StreamWriter sw = new StreamWriter(File.Open("test.txt", FileMode.Append)); for(int i = 0; i &lt; set.Tables["Schema"].Columns.Count; i++) { sw.Write(set.Tables["Schema"].Columns[i].ColumnName.PadRight(set.Tables["Schema"].Columns[i].MaxLength + set.Tables["Schema"].Columns[i].ColumnName.Length, ' ') + " "); } sw.WriteLine(); for(int i = 0; i &lt; set.Tables["Schema"].Columns.Count; i++) { sw.Write(new string('-', set.Tables["Schema"].Columns[i].MaxLength + set.Tables["Schema"].Columns[i].ColumnName.Length) + " "); } sw.WriteLine(); foreach(DataRow row in set.Tables["Data"].Rows) { foreach(DataColumn col in set.Tables["Data"].Columns) { sw.Write(row[col].ToString().PadRight(set.Tables["Schema"].Columns[col.ColumnName].MaxLength + col.ColumnName.Length) + " "); } sw.WriteLine(); } sw.Close(); sw.Dispose(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload