KBD

Keith Devens .com

Friday, August 29, 2008 Flag waving
YAGNI: You ain't gonna need it. – XP slogan

Archive: February 10, 2006

← February 09, 2006February 11, 2006 →

Daily link icon Friday, February 10, 2006

Dump a db table in ASP.NET

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 ? "&nbsp;" : 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";
}
?>
  1. ADO.NET for the Java Programmer (to read).

       (0) Tags: [Programming]
← February 09, 2006February 11, 2006 →
August 2008
SunMonTueWedThuFriSat
 12
3456789
10111213141516
17181920212223
24252627282930
31 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 2 posts

Recent comments XML

new⇒Johnny Walker Blue Label

Wow, thanks for the scotch review​:D

Lagavulin and Laphroaig are​some of...

Keith: Aug 29, 3:35pm

Girls, please don't get breast implants

Wow, After all this time, the​comments on this page continue to​grow. It wa...

Ajeet: Aug 25, 2:36am

Generated in about 0.055s.

(Used 7 db queries)

mobile phone