<?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; SQL</title>
	<atom:link href="http://familywhitfield.co.uk/wordpress/category/software-development/sql/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>Tips for returning IDENTITY values from INSERT</title>
		<link>http://familywhitfield.co.uk/wordpress/2007/01/14/tips-for-returning-identity-values-from-insert/</link>
		<comments>http://familywhitfield.co.uk/wordpress/2007/01/14/tips-for-returning-identity-values-from-insert/#comments</comments>
		<pubDate>Sun, 14 Jan 2007 17:04:00 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[ASP2]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://familywhitfield.co.uk/wordpress/?p=16</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://familywhitfield.co.uk/wordpress/2007/01/14/tips-for-returning-identity-values-from-insert/' addthis:title='Tips for returning IDENTITY values from INSERT' ><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>There&#8217;s a good article by Scott Guthrie here that describes the basics. Just scroll down to Tutorial 5 for the INSERT specific bit &#8211; it is pretty straightforward and there is no point my repeating it here. One point to note, however, and why I wrote this particular post. The key step in getting this [...]]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://familywhitfield.co.uk/wordpress/2007/01/14/tips-for-returning-identity-values-from-insert/' addthis:title='Tips for returning IDENTITY values from INSERT' ><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>There&#8217;s a good article by Scott Guthrie <a href="http://aspalliance.com/914_Building_a_DAL_using_Strongly_Typed_TableAdapters_and_DataTables_in_VS_2005_and_ASPNET_20">here</a> that describes the basics. Just scroll down to Tutorial 5 for the INSERT specific bit &#8211; it is pretty straightforward and there is no point my repeating it here.</p>
<p>One point to note, however, and why I wrote this particular post. The key step in getting this working is changing the query type from NonQuery to Scalar. But this can get reset by Visual Studio if you choose the Configure option on the ObjectDataSource&#8217;s smart tags to modify the query in any way. If you don&#8217;t notice this, then suddenly the identity value stops getting returned and your code breaks.</p>
<p>The second tip is that by default the type returned by the insert query is decimal. This can cause some extra casting in your code if you were expecting it to be int. To fix this, just modify the SQL in Scott&#8217;s original article to be this:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">CAST</span> <span style="color: #66cc66;">&#40;</span>SCOPE_IDENTITY<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://familywhitfield.co.uk/wordpress/2007/01/14/tips-for-returning-identity-values-from-insert/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>Adding ASP Role tables to your own database</title>
		<link>http://familywhitfield.co.uk/wordpress/2006/12/29/adding-asp-role-tables-to-your-own-database/</link>
		<comments>http://familywhitfield.co.uk/wordpress/2006/12/29/adding-asp-role-tables-to-your-own-database/#comments</comments>
		<pubDate>Fri, 29 Dec 2006 23:17:25 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[ASP2]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://familywhitfield.co.uk/wordpress/?p=5</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://familywhitfield.co.uk/wordpress/2006/12/29/adding-asp-role-tables-to-your-own-database/' addthis:title='Adding ASP Role tables to your own 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>This is still work in progress, but I want to incorporate the user role management tables within my own database, as the concept of users and their distinct roles ties right in with the core business model of the new site. The following link explains the standalone tool to create the tables: ASP.NET SQL Server [...]]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://familywhitfield.co.uk/wordpress/2006/12/29/adding-asp-role-tables-to-your-own-database/' addthis:title='Adding ASP Role tables to your own 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 class="MsoNormal" style="margin-bottom: 12pt;"><span lang="EN-GB">This is still work in progress, but I want to incorporate the user role management tables within my own database, as the concept of users and their distinct roles ties right in with the core business model of the new site.<br />
The following link explains the standalone tool to create the tables:<br />
<a href="http://msdn2.microsoft.com/en-us/library/ms229862.aspx" target="_blank">ASP.NET SQL Server Registration Tool</a></span></p>
<p>Invoking the exe runs a wizard requesting the database service &#8211; it needs to match the connection string as mentioned in the gotcha above. Since no parameters are specified, it then creates a database &#8220;aspnetdb&#8221; with all the role tables. This is not quite what I want, as I need these added to my application database.</p>
<p>The answer is to use the command line:<br />
aspnet_regsql -S  -E -A all -d</p>
<p>If you run SQL Management Server Studio Express before and after &#8211; not forgetting to hit &#8216;refresh&#8217; &#8211; then a bunch of new tables are added: aspnet_Applications, aspnet_Roles, aspnet_SchemaVersions, aspnet_Users, aspnet_UsersInRoles etc. Now it is just a matter of hooking those up with the application tables.</p>
<p>Since I wanted membership and roles as well as basic authentication I used the &#8220;-A all&#8221; option to add everything in. Of course I get profile and web-part support too with this, but I may choose to exploit such features in the future.<a name="References"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://familywhitfield.co.uk/wordpress/2006/12/29/adding-asp-role-tables-to-your-own-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

