One of the first things I do whenever I'm working with a new database technology (JDBC, ADO, ADO.NET, etc.) is write a table dumper to learn the API. It's also usually because I usually find myself without access to any handy db tools, so I have to write my own. Here's a dumper for ASP.NET/ADO.NET:
void DumpTable(System.Data.IDataReader reader){
System.Data.DataTable fields = reader.GetSchemaTable();
Response.Write("<table border='1'>");
Response.Write("<tr>");
foreach(System.Data.DataRow field in fields.Rows){
Response.Write("<th>");
Response.Write(HttpUtility.HtmlEncode(field["ColumnName"].ToString()));
Response.Write("</th>");
}
Response.Write("</tr>");
int numfields = reader.FieldCount;
while(reader.Read()){
Response.Write("<tr>");
for(int i=0; i<numfields; i++){
Response.Write("<td>");
string s = reader[i].ToString();
Response.Write(s.Length == 0 ? " " : HttpUtility.HtmlEncode(s));
Response.Write("</td>");
}
Response.Write("</tr>");
}
Response.Write("</table>");
}
There may be a better way to do some of that (if there is, let me know). Just for reference, to connect and get a table dump (happens to be Oracle, but there's an equivalent for every db API in ADO.NET):
OracleConnection conn = new OracleConnection(
"Data Source=dbtable;User Id=user;Password=pass"
); // wrapped to not break my layout
conn.Open();
OracleCommand cmd = new OracleCommand("SELECT * FROM table WHERE ROWNUM<100", conn);
OracleDataReader reader = cmd.ExecuteReader();
And of course, after dumping:
conn.Close();
That handy WHERE ROWNUM < n bit from here.
Update: For informational purposes, here's a similar utility function for PHP I have sitting around in my db library (modified slightly):
<?php
function printTable(&$table){
if(!$table){
echo 'Empty table';
return;
}
echo '<table border="1">',"\n<tr>\n\t";
foreach(array_keys($table[0]) as $key)
echo '<th>',htmlspecialchars($key),'</th>';
foreach($table as $row){
echo "\n</tr>\n<tr>\n\t";
foreach($row as $data)
echo '<td>',htmlspecialchars($data),'</td>';
}
echo "\n</tr>\n</table>\n\n";
}
?>
new⇒Johnny Walker Blue Label
Wow, thanks for the scotch review:D
Lagavulin and Laphroaig aresome of...
Keith: Aug 29, 3:35pm