<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Pushing Pixels &#187; c#</title>
	<atom:link href="http://familywhitfield.co.uk/wordpress/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://familywhitfield.co.uk/wordpress</link>
	<description>Computing and Digital Imaging</description>
	<lastBuildDate>Thu, 24 Mar 2011 20:54:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Determining row counts for all tables in a database</title>
		<link>http://familywhitfield.co.uk/wordpress/2007/01/11/determining-row-counts-for-all-tables-in-a-database/</link>
		<comments>http://familywhitfield.co.uk/wordpress/2007/01/11/determining-row-counts-for-all-tables-in-a-database/#comments</comments>
		<pubDate>Thu, 11 Jan 2007 22:02:33 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[ASP2]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[test driven development]]></category>
		<category><![CDATA[unit-testing]]></category>

		<guid isPermaLink="false">http://familywhitfield.co.uk/wordpress/?p=13</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://familywhitfield.co.uk/wordpress/2007/01/11/determining-row-counts-for-all-tables-in-a-database/' addthis:title='Determining row counts for all tables in a database' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_digg"></a><a class="addthis_button_compact"></a></div>Here is a usful snippet if you want a way to get the number of rows in each table in your database. I use this in an admin-only page of the web application to provide some at-a-glance statistics. It is also really useful in unit tests for checking the correctness of business logic that may [...]]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://familywhitfield.co.uk/wordpress/2007/01/11/determining-row-counts-for-all-tables-in-a-database/' addthis:title='Determining row counts for all tables in a database' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_digg"></a><a class="addthis_button_compact"></a></div><p>Here is a usful snippet if you want a way to get the number of rows in each table in your database. I use this in an admin-only page of the web application to provide some at-a-glance statistics. It is also really useful in unit tests for checking the correctness of business logic that may create new entries in several tables in one transaction.</p>
<p><span id="more-13"></span></p>
<h3>SQL snippet</h3>
<p>The bit of SQL is quite straightforward, once you delve down into the depths of SQLServer&#8217;s system tables:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>
   so<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> TableName<span style="color: #66cc66;">,</span>
   <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span>si<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">ROWS</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>RowCount<span style="color: #66cc66;">&#93;</span>
<span style="color: #993333; font-weight: bold;">FROM</span>
   sys<span style="color: #66cc66;">.</span>sysobjects <span style="color: #993333; font-weight: bold;">AS</span> so
<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span>
   sys<span style="color: #66cc66;">.</span>sysindexes <span style="color: #993333; font-weight: bold;">AS</span> si
   <span style="color: #993333; font-weight: bold;">ON</span> OBJECT_ID<span style="color: #66cc66;">&#40;</span>so<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> si<span style="color: #66cc66;">.</span>id
<span style="color: #993333; font-weight: bold;">WHERE</span>
   <span style="color: #66cc66;">&#40;</span>so<span style="color: #66cc66;">.</span>xtype <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'U'</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> so<span style="color: #66cc66;">.</span>name
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> TableName</pre></div></div>

<h2>Presenting in a page</h2>
<p>There are a number of ways of getting this into an ASP2 page. I chose to do this via a new table adaptor, and to wrap the query in an Administration class in the business logic layer.</p>
<p>Paste the SQL snippet from above into the query builder to create a new table adapter on the data designer &#8211; I have a separate &#8220;admin.xsd&#8221; to keep these non-user level types away from the core application data types. By default you will get an  adapter called &#8220;sysobjectsTableAdapter&#8221; as it picks its name up from the first table in the query. You can change this if you need to.</p>
<p>You can now go direct to an ASP page by pointing an ObjectDataSource directly at this table adapter, or you can wrap it in an intermediate layer.</p>
<h2>Unit test example</h2>
<p>My unit test framework has a database reset mechanism that empties all tables and then creates a small set of known test data using the business logic layer (BLL). This empty and reset is done in the test fixture setup, and the subsequent raft of tests use a variety of methods to valid the integrity of the data.</p>
<p>A key test is ensuring that the expected number of rows in various tables have been created. This is particularly important as there are a number of linking and cross reference tables that are not directly seen by a user, but are there for the efficiency of the schema. So checking  all these have been set correctly after the BLL has done its work is vital.</p>
<p>In the test class I have something like this :</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>TestFixture<span style="color: #008000;">&#93;</span> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> DBReset
<span style="color: #008000;">&#123;</span> <span style="color: #008000;">....</span>
   <span style="color: #0600FF; font-weight: bold;">private</span> Dictionary TableRowCounts  <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary <span style="color: #008000;">;</span>
   <span style="color: #008000;">...</span>
    <span style="color: #008000;">&#91;</span>TestFixtureSetup<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> FixtureSetup<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        TableRowCounts<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span> <span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Addresses&quot;</span>, <span style="color: #FF0000;">6</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        TableRowCounts<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span> <span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Agencies&quot;</span>, <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">...</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This sets up a list of all the tables I am interested in and maps the expected number of rows for each table after my fixture setup has initialised the database with known data.</p>
<p>I then have the following unit test as part of this fixture:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"> <span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
 <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> CheckRowCountsExpected<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
 <span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Verifies that the correct number of rows in each database</span>
    <span style="color: #008080; font-style: italic;">// has been created as a result of the reset and test</span>
    <span style="color: #008080; font-style: italic;">// data initialisation.</span>
    AdminBll abll <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> AdminBll<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    Admin<span style="color: #008000;">.</span><span style="color: #0000FF;">sysobjectsDataTable</span> tabs <span style="color: #008000;">=</span> abll<span style="color: #008000;">.</span><span style="color: #0000FF;">GetTableRowCounts</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Admin<span style="color: #008000;">.</span><span style="color: #0000FF;">sysobjectsRow</span> trow <span style="color: #0600FF; font-weight: bold;">in</span> tabs<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
       <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>TableRowCounts<span style="color: #008000;">.</span><span style="color: #0000FF;">ContainsKey</span> <span style="color: #008000;">&#40;</span>trow<span style="color: #008000;">.</span><span style="color: #0000FF;">TableName</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span>
          <span style="color: #008080; font-style: italic;">//Only check the tables specified in the dictionary</span>
          Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span>TableRowCounts<span style="color: #008000;">&#91;</span>trow<span style="color: #008000;">.</span><span style="color: #0000FF;">TableName</span><span style="color: #008000;">&#93;</span>,
             trow<span style="color: #008000;">.</span><span style="color: #0000FF;">RowCount</span>,
             <span style="color: #666666;">&quot;Wrong rowcount for table &quot;</span> <span style="color: #008000;">+</span> trow<span style="color: #008000;">.</span><span style="color: #0000FF;">TableName</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
       <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>I also have a second test that verifies the admin query against each table one at a time using direct SELECT COUNT queries in SQL.</p>
]]></content:encoded>
			<wfw:commentRss>http://familywhitfield.co.uk/wordpress/2007/01/11/determining-row-counts-for-all-tables-in-a-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Useful CSV reader</title>
		<link>http://familywhitfield.co.uk/wordpress/2007/01/10/useful-csv-reader/</link>
		<comments>http://familywhitfield.co.uk/wordpress/2007/01/10/useful-csv-reader/#comments</comments>
		<pubDate>Wed, 10 Jan 2007 09:51:04 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[unit-testing]]></category>

		<guid isPermaLink="false">http://familywhitfield.co.uk/wordpress/?p=12</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://familywhitfield.co.uk/wordpress/2007/01/10/useful-csv-reader/' addthis:title='Useful CSV reader' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_digg"></a><a class="addthis_button_compact"></a></div>To aid my unit testing I needed to populate a database with a load of test data. This data needs to be generated by some of the less technical guys on the project and so exporting CSV (Comma Separated Values) files from Excel seemed a sensible approach. So I then cast around for any existing [...]]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://familywhitfield.co.uk/wordpress/2007/01/10/useful-csv-reader/' addthis:title='Useful CSV reader' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_digg"></a><a class="addthis_button_compact"></a></div><p>To aid my unit testing I needed to populate a database with a load of test data. This data needs to be generated by some of the less technical guys on the project and so exporting CSV (Comma Separated Values) files from Excel seemed a sensible approach. So I then cast around for any existing code that would save me having to write a robust CSV parser. The search turned up this on <a href="http://www.codeproject.com/">CodeProject</a>:</p>
<p><a href="http://www.codeproject.com/cs/database/CsvReader.asp">LumenWorks.Framework </a></p>
<p>The neat thing about it is that you can get field values either by index position or column name, assuming you put column headers in your file. The column name feature really helps out with maintainance of the test data as we evolve and vary the schema slightly &#8211; we can insert columns with no worries about upsetting existing reader code.<br />
It&#8217;s easy to use and install in your own project, and the CodeProject article describes the basic usage pattern. At the moment it is just linked in to the unit test framework, but is so useful I will probably make it a part of the core application libary to facilitate importing data from customers as we anticipate some of our customers could have thousands of records to import when they move to our system.</p>
]]></content:encoded>
			<wfw:commentRss>http://familywhitfield.co.uk/wordpress/2007/01/10/useful-csv-reader/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t believe NUnit</title>
		<link>http://familywhitfield.co.uk/wordpress/2007/01/05/dont-believe-nunit/</link>
		<comments>http://familywhitfield.co.uk/wordpress/2007/01/05/dont-believe-nunit/#comments</comments>
		<pubDate>Fri, 05 Jan 2007 11:07:39 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[ASP2]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[Nunit]]></category>
		<category><![CDATA[test driven development]]></category>
		<category><![CDATA[unit-testing]]></category>

		<guid isPermaLink="false">http://familywhitfield.co.uk/wordpress/?p=11</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://familywhitfield.co.uk/wordpress/2007/01/05/dont-believe-nunit/' addthis:title='Don&#8217;t believe NUnit' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_digg"></a><a class="addthis_button_compact"></a></div>Just a quick note about a little problem I found using Nunit (2.2.9, but affects earlier revisions too). After a bunch of edits to my project, I suddenly started getting messages from NUnit GUI &#8220;could not load file or assembly nunit.core&#8221;. Try as I could, this would not go away. This included everything from reinstalling [...]]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://familywhitfield.co.uk/wordpress/2007/01/05/dont-believe-nunit/' addthis:title='Don&#8217;t believe NUnit' ><a class="addthis_button_twitter"></a><a class="addthis_button_facebook"></a><a class="addthis_button_email"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_digg"></a><a class="addthis_button_compact"></a></div><p><em>Just a quick note about a little problem I found using Nunit (2.2.9, but affects earlier revisions too).</em></p>
<p>After a bunch of edits to my project, I suddenly started getting messages from NUnit GUI &#8220;could not load file or assembly nunit.core&#8221;. Try as I could, this would not go away. This included everything from reinstalling NUnit to tracking DLL loading via SysInternal&#8217;s Process Explorer tool.<br />
My project is non trivial &#8211; a top level solution containing an ASP2 web project, a couple of library DLL&#8217;s for the business logic, a unit test executable, and a testing helper library to help cross reference test results against direct SQL calls. As I&#8217;ll be writing about in another post shortly, incorporating unit testing into an ASP project is a pain in the rear and so I had been restructuring things. My natural assumption based on the error message was I had messed up something in the linking/referencing.</p>
<p>But it turned out to be an error in the application config file &#8211; for some reason NUnit reports this as a DLL missing, not that you have mistyped a bit of config text. I simply had a closing  tag in the wrong place &#8211; still syntactically correct, however.<br />
<a title="Nunit DLL load issue" href="http://vibhu.info/2007/01/02/wierd-issue-could-not-load-file-or-assembly-nunitcore/">This link</a> is by someone else who hit the exact same problem, and he has written about in more detail.</p>
<p>So remember folks: <em>never assume a system generates the correct error message for all circumstances. </em></p>
]]></content:encoded>
			<wfw:commentRss>http://familywhitfield.co.uk/wordpress/2007/01/05/dont-believe-nunit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

