<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3999553910216009235</id><updated>2011-11-27T18:15:06.499-05:00</updated><category term='XML'/><category term='&quot;SSMS&quot; &quot;SQL Server&quot;'/><category term='System.DirectoryServices.AccountManagement C# AD'/><category term='Backup'/><category term='SQL Server 2005 Express'/><category term='sp_msforeachtable &quot;SQL Server&quot;'/><category term='SQL Server CLR XML XSL'/><category term='SQL Server'/><title type='text'>Lode of Code</title><subtitle type='html'>Code and stuff</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://lodeofcode.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3999553910216009235/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://lodeofcode.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Simon</name><uri>http://www.blogger.com/profile/16209703097357837049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>7</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3999553910216009235.post-3970302666532584516</id><published>2010-07-20T16:55:00.000-04:00</published><updated>2010-07-20T16:55:46.111-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL Server CLR XML XSL'/><title type='text'>Sending SQL Server Query Results as Pretty HTML Email</title><content type='html'>&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&amp;nbsp;So my goal here is to be able to send out a pretty HTML email based on data from a SQL Query using only SQL Server 2005 (or greater).&lt;br /&gt;&lt;br /&gt;&amp;nbsp;First we need to make sure the environment is all set up by doing the following&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Make sure that CLR integration is enabled on the instance of SQL Server&lt;/li&gt;&lt;li&gt;Make sure that Database Mail is enabled and configured.&lt;/li&gt;&lt;li&gt;Make sure Openrowset is enabled. &lt;/li&gt;&lt;li&gt;Create our SampleData table and populate it with some data.&lt;/li&gt;&lt;li&gt;Create a table to store our XSL data. &lt;/li&gt;&lt;/ol&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;Step 1.&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&amp;nbsp;Make sure that CLR Integration is enabled on your SQL Server Instance by opening the SQL Server Surface Area Configuration tool and clicking on the enable CLR Integration checkbox like shown below.&amp;nbsp;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&amp;nbsp; &lt;/div&gt;&lt;a href="http://4.bp.blogspot.com/_k2x9t7unP_A/TDc8qB4SShI/AAAAAAAAARM/t1rKI8EJzF0/s1600/SQLDBMailEnable.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="302" src="http://4.bp.blogspot.com/_k2x9t7unP_A/TDc8qB4SShI/AAAAAAAAARM/t1rKI8EJzF0/s400/SQLDBMailEnable.jpg" width="400" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;Step 2.&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;While you are there also make sure that Database Mail is enabled by checking the checkbox like shown here.&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;If you haven't configured DBMail yet&amp;nbsp;&lt;a href="http://blog.sqlauthority.com/2008/08/23/sql-server-2008-configure-database-mail-send-email-from-sql-database/"&gt;here&lt;/a&gt; is a helpful step by step guide. &lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;a href="http://3.bp.blogspot.com/_k2x9t7unP_A/TDc8Nqpn6qI/AAAAAAAAAQ8/-KmkSCQorGw/s1600/SQLDBMailEnable.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="302" src="http://3.bp.blogspot.com/_k2x9t7unP_A/TDc8Nqpn6qI/AAAAAAAAAQ8/-KmkSCQorGw/s400/SQLDBMailEnable.jpg" width="400" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Step 3.&lt;br /&gt;And also make sure the OPENROWSET is enabled.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_k2x9t7unP_A/TEX5GTvXjyI/AAAAAAAAARs/a8ebUSzEXGo/s1600/EnableOpenRowset.bmp" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="302" src="http://4.bp.blogspot.com/_k2x9t7unP_A/TEX5GTvXjyI/AAAAAAAAARs/a8ebUSzEXGo/s400/EnableOpenRowset.bmp" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Step 4. &lt;br /&gt;Next we need some sample data to work with.&lt;br /&gt;This will create the SampleData table and populate it with some sample data.&lt;br /&gt;&lt;br /&gt;&lt;blockquote style="background-color: #eeeeee;"&gt;&lt;blockquote&gt;&lt;span style="background-color: #eeeeee;"&gt;CREATE TABLE dbo.SampleData&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #eeeeee;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;(&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #eeeeee;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;ID int NOT NULL IDENTITY (1, 1),&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #eeeeee;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;ItemNumber nvarchar(50) NULL,&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #eeeeee;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;ItemDescription nvarchar(50) NULL,&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #eeeeee;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;Cost decimal(18, 2) NULL,&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #eeeeee;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;QtySold int NULL&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #eeeeee;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;)&lt;/span&gt;&lt;/blockquote&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;blockquote&gt;&lt;span style="background-color: #eeeeee;"&gt;Insert into SampleData (ItemNumber, ItemDescription, Cost, QtySold)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;div style="background-color: #eeeeee;"&gt;&lt;span style="background-color: #eeeeee;"&gt;values ('2WE4R5T', 'Thinga Ma Bob', '2.10', '1000')&lt;/span&gt;&lt;/div&gt;&lt;div style="background-color: #eeeeee;"&gt;&lt;span style="background-color: #eeeeee;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="background-color: #eeeeee;"&gt;&lt;span style="background-color: #eeeeee;"&gt;Insert into SampleData (ItemNumber, ItemDescription, Cost, QtySold) &lt;/span&gt;&lt;/div&gt;&lt;div style="background-color: #eeeeee;"&gt;&lt;span style="background-color: #eeeeee;"&gt;values ('96KL69YLK', 'DooHickey', '995.10', '22')&lt;/span&gt;&lt;/div&gt;&lt;div style="background-color: #eeeeee;"&gt;&lt;span style="background-color: #eeeeee;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="background-color: #eeeeee;"&gt;&lt;span style="background-color: #eeeeee;"&gt;Insert into SampleData (ItemNumber, ItemDescription, Cost, QtySold) &lt;/span&gt;&lt;/div&gt;&lt;div style="background-color: #eeeeee;"&gt;&lt;span style="background-color: #eeeeee;"&gt;values ('9FJ438JD', 'Whatchama Calut', '2256.25', '56')&lt;/span&gt;&lt;/div&gt;&lt;/blockquote&gt;&lt;/blockquote&gt;&lt;span style="background-color: #eeeeee;"&gt;&lt;span style="background-color: white;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Step 5.&lt;br /&gt;&amp;nbsp;We could just store the XSL Files in the filesystem but we want to do as much in SQL Server as possible so we run the following to create a table to store our XSL.&lt;br /&gt;&lt;br /&gt;&lt;blockquote style="background-color: #eeeeee;"&gt;CREATE TABLE [dbo].[XMLTransforms](&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;[id] [int] IDENTITY(1,1) NOT NULL,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;[XSL] [xml] NULL&lt;br /&gt;)&lt;/blockquote&gt;Now that our environment is all set up we are ready to have some fun.&lt;br /&gt;&lt;ol start="6"&gt;&lt;li&gt;Write the query to get the data we want to send.&lt;/li&gt;&lt;li&gt;Write the XSL file to transform the XML returned in the above query to pretty HTML.&lt;/li&gt;&lt;li&gt;Import the XSL into the Table created in step 4 above.&amp;nbsp; &lt;/li&gt;&lt;li&gt;Write the CLR Function in C# to transform the XML to HTML.&lt;/li&gt;&lt;li&gt;Add the CLR Function to SQL Server.&lt;/li&gt;&lt;li&gt;Put it all together and watch the magic happen&lt;/li&gt;&lt;/ol&gt;&lt;ol start="4"&gt;&lt;/ol&gt;&lt;ol&gt;&lt;/ol&gt;Step 6.&lt;br /&gt;&amp;nbsp; The only requirement is that the query returns it's data as the XML Data Type. So this will return an XML doc for us to work with.&lt;br /&gt;&lt;blockquote&gt;&lt;div style="background-color: #eeeeee;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Declare @xmlDoc XML;&lt;/div&gt;&lt;div style="background-color: #eeeeee;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Select @xmlDoc = (Select * from SampleData for xml auto, Root('Sample'), elements) &lt;/div&gt;&lt;/blockquote&gt;Here I am declaring a variable @xmlDoc as being of the XML Data Type.&lt;br /&gt;I am just pulling all the data from the table we created above and sticking it into the @xmlDoc variable.&lt;br /&gt;&lt;ul&gt;&lt;li&gt;The "for xml auto" portion of the above statement tells SQL to return the query results as nested XML elements.&lt;/li&gt;&lt;li&gt;The "Root('Sample')" portion nests the results of the query in a root element named 'Sample'&lt;/li&gt;&lt;li&gt;The "elements" portion tells SQL to return each column as a subelement rather than a an attribute of an element (read more about this &lt;a href="http://msdn.microsoft.com/en-us/library/ms188273.aspx"&gt;here&lt;/a&gt;)&lt;/li&gt;&lt;/ul&gt;The resulting XML from the statement above should look like this.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_k2x9t7unP_A/TECz2gMBuGI/AAAAAAAAARU/HRqX0nus6dk/s1600/sqlXML.bmp" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/_k2x9t7unP_A/TECz2gMBuGI/AAAAAAAAARU/HRqX0nus6dk/s320/sqlXML.bmp" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Step 7.&lt;br /&gt;Now we need to specify how we want to transform the XML that we have into html. We do this using XSL. We are going to keep things simple and just return the data in an html table and base our XSL file on the example shown &lt;a href="http://www.w3schools.com/xsl/default.asp"&gt;here&lt;/a&gt;&amp;nbsp; &lt;br /&gt;You can use Visual Studio or&amp;nbsp;&lt;a href="http://sourceforge.net/projects/notepad-plus/"&gt;Notepad++&lt;/a&gt; or your favorite text editor to edit the file.&amp;nbsp; &lt;br /&gt;Here is what our XSL file looks like or you can download it &lt;a href="http://sites.google.com/site/futureme47/Sample.xsl"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_k2x9t7unP_A/TEX_Pvkc0lI/AAAAAAAAAR8/pMecFCdkzBs/s1600/Sample.xsl" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="386" src="http://2.bp.blogspot.com/_k2x9t7unP_A/TEX_Pvkc0lI/AAAAAAAAAR8/pMecFCdkzBs/s400/Sample.xsl" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Step 8.&lt;br /&gt;&amp;nbsp;To import the XSL file from your Filesystem into the SQL Table created in step 5 run the following&lt;br /&gt;&lt;blockquote style="background-color: #eeeeee;"&gt;&lt;br /&gt;&lt;pre&gt;INSERT INTO XMLTransforms(XSL)&lt;br /&gt;    SELECT * FROM OPENROWSET(&lt;br /&gt;     BULK 'c:\XSL\Sample.xsl', SINGLE_BLOB) AS x&lt;/pre&gt;&lt;/blockquote&gt;&lt;br /&gt;Step 9. &lt;br /&gt;Now we need a way to apply the XSL to the XML and spit out the pretty HTML we are looking for. We do this using a CLR function. &lt;br /&gt;The CLR function looks like this and you can download the code &lt;a href="http://sites.google.com/site/futureme47/XML2HTML.cs"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_k2x9t7unP_A/TEI1YNugkuI/AAAAAAAAARg/g0THggHXDLM/s1600/CLR.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="245" src="http://1.bp.blogspot.com/_k2x9t7unP_A/TEI1YNugkuI/AAAAAAAAARg/g0THggHXDLM/s400/CLR.jpg" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;To compile the above code you can use the&amp;nbsp;&lt;a href="http://msdn.microsoft.com/en-us/library/78f4aasd%28VS.80%29.aspx"&gt;csc.exe&lt;/a&gt; command line compiler that comes with the .Net Framework.In the case of my PC I ran the following from a command prompt&lt;br /&gt;&lt;div style="color: black;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;blockquote style="background-color: #eeeeee; color: black;"&gt;&lt;span style="font-size: small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; cd C:\Windows\Microsoft.NET\Framework\v3.5&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; csc.exe /target:library /out:C:\Code\XML2HTML.dll C:\Code\XML2HTML.cs&lt;/span&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;span style="background-color: #eeeeee;"&gt;&lt;/span&gt;&lt;/blockquote&gt;This creates the XML2HTML.dll in the C:\Code Directory&lt;br /&gt;&lt;br /&gt;Step 10.&lt;br /&gt;The next step is to add the map the Function in the DLL to a function in SQL Server. We do this by doing the following.&lt;br /&gt;&lt;br /&gt;First we need to add the Assembly to SQL Server like this:&lt;br /&gt;&lt;blockquote&gt;&lt;pre&gt;&lt;span style="background-color: #eeeeee;"&gt;CREATE ASSEMBLY XML2HTML &lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #eeeeee;"&gt;FROM 'C:\Code\XML2HTML.dll'&lt;/span&gt;&lt;br /&gt;&lt;span style="background-color: #eeeeee;"&gt;WITH PERMISSION_SET = SAFE;&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;Then we need to map a SQL Function to the CLR Function in the DLL.&lt;br /&gt;&lt;blockquote&gt;&lt;span style="background-color: #eeeeee; font-size: x-small;"&gt;CREATE FUNCTION [dbo].[ApplyXslTransform](@inputXML [xml], @inputTransform [xml])&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;RETURNS [xml] WITH EXECUTE AS CALLER&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;AS&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;EXTERNAL NAME [XML2HTML].[UserDefinedFunctions].[Transform]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/blockquote&gt;Step 11.&lt;br /&gt;Now we can finally send out our HTML Email by executing the following&lt;br /&gt;&lt;blockquote style="background-color: #eeeeee;"&gt;Declare @xmlDoc XML;&lt;br /&gt;Select @xmlDoc = (Select * from SampleData for xml auto, Root('Sample'), elements)&lt;br /&gt;Declare @xsl XML;&lt;br /&gt;Set @xsl = (Select xsl from XMLTransforms where id = 1)&lt;br /&gt;Declare @messBody nvarchar(max);&lt;br /&gt;Set @messBody = cast((Select dbo.ApplyXslTransform(@xmlDoc, @xsl)) as nvarchar(max))&lt;br /&gt;EXEC msdb.dbo.sp_send_dbmail&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @profile_name = 'Profile Name Here',&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @recipients = 'someone@sample.com',&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @body = @messBody,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @body_format = 'HTML',&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @subject = 'Sample Info.' ;&lt;/blockquote&gt;&lt;br /&gt;Ok so maybe it isn't the prettiest HTML, but you should be able to see the potential. As you create new and different XSL files and import them into the XMLTransforms table you can change the whole layout of your email just by changing the&amp;nbsp; id in the&lt;br /&gt;"Set @xsl = (Select xsl from XMLTransforms where id = 1)" statement.&lt;br /&gt;You could return a customized "Thanks For Your Order" email in the format of a letter or an automated "Friendly Reminder" that a customer's account is overdue from your AR system.&lt;br /&gt;Once you have the framework detailed above you are only limited by your HTML skills.&lt;br /&gt;Good Luck!&amp;nbsp; &lt;br /&gt;&lt;span style="background-color: #eeeeee;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3999553910216009235-3970302666532584516?l=lodeofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lodeofcode.blogspot.com/feeds/3970302666532584516/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lodeofcode.blogspot.com/2010/07/sending-sql-server-query-results-as.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3999553910216009235/posts/default/3970302666532584516'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3999553910216009235/posts/default/3970302666532584516'/><link rel='alternate' type='text/html' href='http://lodeofcode.blogspot.com/2010/07/sending-sql-server-query-results-as.html' title='Sending SQL Server Query Results as Pretty HTML Email'/><author><name>Simon</name><uri>http://www.blogger.com/profile/16209703097357837049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_k2x9t7unP_A/TDc8qB4SShI/AAAAAAAAARM/t1rKI8EJzF0/s72-c/SQLDBMailEnable.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3999553910216009235.post-1911291278792503966</id><published>2010-07-08T11:00:00.000-04:00</published><updated>2010-07-08T11:00:42.626-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='XML'/><category scheme='http://www.blogger.com/atom/ns#' term='SQL Server'/><title type='text'>Importing XML from a file into an XML column</title><content type='html'>The following will import the XML from a file into a column in a table with the XML Data Type in SQL Server.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;INSERT INTO T(XmlCol)&lt;br /&gt;    SELECT * FROM OPENROWSET(&lt;br /&gt;     BULK 'c:\SampleFolder\SampleData3.txt', SINGLE_BLOB) AS x&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;pre&gt;You must include the as X to name the column or the import will fail.&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;pre&gt;THis example and more can be taken from here &lt;a href="http://msdn.microsoft.com/en-us/library/ms191184.aspx"&gt;http://msdn.microsoft.com/en-us/library/ms191184.aspx&lt;/a&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3999553910216009235-1911291278792503966?l=lodeofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lodeofcode.blogspot.com/feeds/1911291278792503966/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lodeofcode.blogspot.com/2010/07/importing-xml-from-file-into-xml-column.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3999553910216009235/posts/default/1911291278792503966'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3999553910216009235/posts/default/1911291278792503966'/><link rel='alternate' type='text/html' href='http://lodeofcode.blogspot.com/2010/07/importing-xml-from-file-into-xml-column.html' title='Importing XML from a file into an XML column'/><author><name>Simon</name><uri>http://www.blogger.com/profile/16209703097357837049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3999553910216009235.post-5059617059123905803</id><published>2010-03-12T09:36:00.000-05:00</published><updated>2010-03-12T09:36:35.804-05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='&quot;SSMS&quot; &quot;SQL Server&quot;'/><title type='text'>Recovering T-SQl Code when SSMS crashes</title><content type='html'>By default SQL Server 2005 and 2008 Management Studio will save whatever you are working on every 5 minutes or so into your&amp;nbsp; &lt;span&gt;&lt;span id="_ctl5_ctlTopic"&gt;&lt;span id="_ctl5_ctlTopic_ctlPanelBar"&gt;&lt;span id="_ctl5_ctlTopic_ctlPanelBar_ctlTopicsRepeater__ctl8_lblFullMessage"&gt;\Documents and Settings\username\My Documents\SQL Server Management Studio\Backup Files\ dir.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&lt;span id="_ctl5_ctlTopic"&gt;&lt;span id="_ctl5_ctlTopic_ctlPanelBar"&gt;&lt;span id="_ctl5_ctlTopic_ctlPanelBar_ctlTopicsRepeater__ctl8_lblFullMessage"&gt;This has saved me time on multiple occassions.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&lt;span id="_ctl5_ctlTopic"&gt;&lt;span id="_ctl5_ctlTopic_ctlPanelBar"&gt;&lt;span id="_ctl5_ctlTopic_ctlPanelBar_ctlTopicsRepeater__ctl8_lblFullMessage"&gt;There is no way to turn this feature off in SSMS but you can control it by changing the following registry settings where 1 is enabled and 0 is disabled.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&lt;span id="_ctl5_ctlTopic"&gt;&lt;span id="_ctl5_ctlTopic_ctlPanelBar"&gt;&lt;span id="_ctl5_ctlTopic_ctlPanelBar_ctlTopicsRepeater__ctl8_lblFullMessage"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;[HKEY_CURRENT_USER\Software\Microsoft\Microsoft SQL Server\90\Tools\Shell\General\AutoRecover]   &lt;br /&gt;"AutoRecover Always Enabled"=dword:00000000    &lt;br /&gt;"AutoRecover Enabled"=dword:00000000&lt;span&gt;&lt;span id="_ctl5_ctlTopic"&gt;&lt;span id="_ctl5_ctlTopic_ctlPanelBar"&gt;&lt;span id="_ctl5_ctlTopic_ctlPanelBar_ctlTopicsRepeater__ctl8_lblFullMessage"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&lt;span id="_ctl5_ctlTopic"&gt;&lt;span id="_ctl5_ctlTopic_ctlPanelBar"&gt;&lt;span id="_ctl5_ctlTopic_ctlPanelBar_ctlTopicsRepeater__ctl8_lblFullMessage"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&lt;span id="_ctl5_ctlTopic"&gt;&lt;span id="_ctl5_ctlTopic_ctlPanelBar"&gt;&lt;span id="_ctl5_ctlTopic_ctlPanelBar_ctlTopicsRepeater__ctl8_lblFullMessage"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3999553910216009235-5059617059123905803?l=lodeofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lodeofcode.blogspot.com/feeds/5059617059123905803/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lodeofcode.blogspot.com/2010/03/recovering-t-sql-code-when-ssms-crashes.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3999553910216009235/posts/default/5059617059123905803'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3999553910216009235/posts/default/5059617059123905803'/><link rel='alternate' type='text/html' href='http://lodeofcode.blogspot.com/2010/03/recovering-t-sql-code-when-ssms-crashes.html' title='Recovering T-SQl Code when SSMS crashes'/><author><name>Simon</name><uri>http://www.blogger.com/profile/16209703097357837049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3999553910216009235.post-6912414673744841705</id><published>2009-10-22T21:00:00.000-04:00</published><updated>2009-10-22T21:00:00.496-04:00</updated><title type='text'>How to retrieve all enabled users from Active Directory.</title><content type='html'>The following code will return the name and email address of all users in a specified Active Directory domain that have not been disabled.&lt;br /&gt;The only tricky part is setting up the LDAP query to only return users that have not been disabled. This is accomplished by setting this bitwise filter&lt;br /&gt;"(!(userAccountControl:1.2.840.113556.1.4.803:=2))"&lt;br /&gt;&lt;br /&gt;In this line we are saying look at the "UserAccountControl" attribute in Active Directory and if it is not equal to "2" then return true.&lt;br /&gt;The 1.2.840.113556.1.4.803 tells it to use the LDAP_MATCHING_RULE_BIT_AND operator to make the comparison.&lt;br /&gt;There is a little more on using the bitwise operators in ActiveDirectory&amp;nbsp;&lt;a href="http://support.microsoft.com/kb/269181"&gt;here&lt;/a&gt; .&lt;br /&gt;&lt;br /&gt;public static DataTable GetListOfActiveUsers(string domainName)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DirectoryEntry entry = new DirectoryEntry("LDAP://DC=" + domainName + ",DC=com");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DirectorySearcher search = new DirectorySearcher(entry);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string query = "(&amp;amp;(objectCategory=person)(objectClass=user)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(&amp;amp;(mail=*)))";&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; search.Filter = query;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; search.PropertiesToLoad.Add("name");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; search.PropertiesToLoad.Add("mail");&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SearchResultCollection mySearchResultColl = search.FindAll();&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DataTable results = new DataTable();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; results.Columns.Add("name");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; results.Columns.Add("mail");&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach (SearchResult sr in mySearchResultColl)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DataRow dr = results.NewRow();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DirectoryEntry de = sr.GetDirectoryEntry();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dr["name"] = de.Properties["Name"].Value;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dr["mail"] = de.Properties["mail"].Value;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; results.Rows.Add(dr);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; de.Close();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return results;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3999553910216009235-6912414673744841705?l=lodeofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lodeofcode.blogspot.com/feeds/6912414673744841705/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lodeofcode.blogspot.com/2009/10/how-to-retrieve-all-enabled-users-from.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3999553910216009235/posts/default/6912414673744841705'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3999553910216009235/posts/default/6912414673744841705'/><link rel='alternate' type='text/html' href='http://lodeofcode.blogspot.com/2009/10/how-to-retrieve-all-enabled-users-from.html' title='How to retrieve all enabled users from Active Directory.'/><author><name>Simon</name><uri>http://www.blogger.com/profile/16209703097357837049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3999553910216009235.post-3647685718070507748</id><published>2009-10-22T09:52:00.000-04:00</published><updated>2009-10-22T09:52:09.508-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='System.DirectoryServices.AccountManagement C# AD'/><title type='text'>How to retrive a list of all computers with Last Logon time from Active Directory</title><content type='html'>I was driving myself crazy trying to retrieve the LastLogonTimestamp from Active Directory with the System.DirectoryServices namespace.&lt;br /&gt;You can read here about the joy of converting the &lt;span style="font-family: Courier New; font-size: 85%;"&gt;IADSLargeInteger data type (which is what the LastLogonTimestamp is) to something usable&amp;nbsp;&lt;/span&gt;&lt;a href="http://securitythroughabsurdity.com/2005/12/active-directory-and.html"&gt; here.&lt;/a&gt;&lt;br /&gt;It was pointed out to me that new in .Net 3.5 is the System.DirectoryServices.AccountManagement namespace which make this task much simpler.&lt;br /&gt;Here is the code that finally did the trick for me.&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&amp;nbsp; public static DataTable GetListOfComputers(string domainName)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DataTable results = new DataTable();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; results.Columns.Add("name");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; results.Columns.Add("lastLogonTimestamp");&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PrincipalContext pc = new PrincipalContext(ContextType.Domain, domainName);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PrincipalSearcher ps = new PrincipalSearcher(new ComputerPrincipal(pc));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PrincipalSearchResult&lt;principal&gt; psr = ps.FindAll();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach (ComputerPrincipal cp in psr)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DataRow dr = results.NewRow();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dr["name"] = cp.Name;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dr["lastLogonTimestamp"] = cp.LastLogon;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; results.Rows.Add(dr);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return results;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/principal&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3999553910216009235-3647685718070507748?l=lodeofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lodeofcode.blogspot.com/feeds/3647685718070507748/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lodeofcode.blogspot.com/2009/10/how-to-retrive-list-of-all-computers.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3999553910216009235/posts/default/3647685718070507748'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3999553910216009235/posts/default/3647685718070507748'/><link rel='alternate' type='text/html' href='http://lodeofcode.blogspot.com/2009/10/how-to-retrive-list-of-all-computers.html' title='How to retrive a list of all computers with Last Logon time from Active Directory'/><author><name>Simon</name><uri>http://www.blogger.com/profile/16209703097357837049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3999553910216009235.post-1904938902450833670</id><published>2009-10-16T13:54:00.000-04:00</published><updated>2009-10-16T13:54:20.547-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='sp_msforeachtable &quot;SQL Server&quot;'/><title type='text'>Truncating all tables in a database with sp_msforeachtable</title><content type='html'>I recently wrote an application that retrieves a list of all computers for a specific domain from Active Directory and proceeds to query each computer for specific info. like Make, Model, Serial Number, OS, Service Packs, Service Tags, RAM, Processor info, Installed Applications etc.&lt;br /&gt;Basically it just automates the process of keeping an up to date inventory of all the computers in my domain and stores the results in a SQL Server 2005 DB.&lt;br /&gt;During the development and testing phase I wound up populating the database with loads of worthless data. after each execution of the partially completed app I wanted to be able to simply Truncate each user table in the user database.&lt;br /&gt;Using the sp_msforeachtable undocumented stored procedure is as simple as this:&lt;br /&gt;&lt;br /&gt;use MyDB &lt;br /&gt;exec sp_msforeachtable 'truncate table ?'&lt;br /&gt;If I only want to truncate the tables that belong to a specific schema I could run the following.&lt;br /&gt;&lt;br /&gt;Use MyDB&lt;br /&gt;Exec sp_msforeachtable @command1 = '&lt;br /&gt;if (Select Object_Schema_name(object_id(''?''))) = ''dbo''&lt;br /&gt;Begin &lt;br /&gt;TRUNCATE TABLE ?&lt;br /&gt;print ''truncated '' + ''?'' &lt;br /&gt;End&lt;br /&gt;'&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3999553910216009235-1904938902450833670?l=lodeofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lodeofcode.blogspot.com/feeds/1904938902450833670/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lodeofcode.blogspot.com/2009/10/truncating-all-tables-in-database-with.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3999553910216009235/posts/default/1904938902450833670'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3999553910216009235/posts/default/1904938902450833670'/><link rel='alternate' type='text/html' href='http://lodeofcode.blogspot.com/2009/10/truncating-all-tables-in-database-with.html' title='Truncating all tables in a database with sp_msforeachtable'/><author><name>Simon</name><uri>http://www.blogger.com/profile/16209703097357837049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3999553910216009235.post-4581082768957057803</id><published>2009-09-23T13:16:00.000-04:00</published><updated>2009-09-23T13:22:22.084-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL Server'/><category scheme='http://www.blogger.com/atom/ns#' term='SQL Server 2005 Express'/><category scheme='http://www.blogger.com/atom/ns#' term='Backup'/><title type='text'>How to schedule backups in SQL Server 2005 Express</title><content type='html'>&lt;p&gt; I recently had a customer that needed a database to serve as the backend for a small Document Management system. SQL Server Standard or Enterprise would have been overkill for such a small application.&lt;/p&gt; &lt;p&gt; I decided to use the free SQL Server 2005 Express Edition. The problem is that it is free for a reason. See some of the limitations listed here.&lt;/p&gt; &lt;p&gt;&lt;a href="http://www.dotnetspider.com/tutorials/SqlServer-Tutorial-158.aspx"&gt;http://www.dotnetspider.com/tutorials/SqlServer-Tutorial-158.aspx&lt;/a&gt;&lt;/p&gt; &lt;p&gt;In order to make this work I had to come up with a way to schedule backups. The following stored procedure takes as parameter the type of backup (db OR Log) and will back up all databases on the target server to the specified target folder.&lt;/p&gt;Create PROCEDURE usp_Backup&lt;br /&gt;    @Type as varchar(10),&lt;br /&gt;    @Path as varchar(255)&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;br /&gt;&lt;br /&gt;SET NOCOUNT ON;&lt;br /&gt;&lt;br /&gt;DECLARE @dbs as TABLE&lt;br /&gt;(&lt;br /&gt;    id int identity,&lt;br /&gt;    name varchar(50),&lt;br /&gt;    recovery_model varchar(25)&lt;br /&gt;)&lt;br /&gt;Insert into @dbs Select name, recovery_model_desc from sys.databases where name &lt;&gt; 'tempdb'&lt;br /&gt;&lt;br /&gt;Declare @dest as varchar(max)&lt;br /&gt;Declare @sql as varchar(max)&lt;br /&gt;Declare @dbname as varchar(50)&lt;br /&gt;Declare @recovery_model as varchar(50)&lt;br /&gt;Declare @retention_days as int&lt;br /&gt;Declare @cnt as int&lt;br /&gt;Declare @id as int&lt;br /&gt;Select @cnt = count(*) from @dbs&lt;br /&gt;set @id = 1&lt;br /&gt;while @id &lt; @cnt&lt;br /&gt;    Begin&lt;br /&gt;        Select @dbname = name from @dbs where id = @id&lt;br /&gt;        Select @recovery_model = recovery_model from @dbs where id = @id&lt;br /&gt;        set @dest = @path + '\' + @dbname + '_' + @type + '_' &lt;br /&gt;                    + convert(varchar(8),getdate(),112) + '_'&lt;br /&gt;                    + replace(convert(varchar(8),getdate(),108),':','') + '.bak'&lt;br /&gt;&lt;br /&gt;    if @type = 'DB'&lt;br /&gt;        Backup Database @dbname to disk = @dest       &lt;br /&gt;    else&lt;br /&gt;        If @recovery_model &lt;&gt; 'simple'&lt;br /&gt;        Backup Log @dbname to disk = @dest   &lt;br /&gt;&lt;br /&gt;    set @id = @id + 1&lt;br /&gt;End&lt;br /&gt;END&lt;br /&gt;GO&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;To schedule it I simply created a text file like so.&lt;br /&gt;&lt;/p&gt;Use Master&lt;br /&gt;Go&lt;br /&gt;Exec usp_backup @Type='Log', @Path='C:\SQL_Backups'&lt;br /&gt;Go&lt;br /&gt;&lt;br /&gt; And scheduled the following to execute using Windows Task Scheduler.  &lt;p&gt;"C:\Program Files\Microsoft SQL Server\90\Tools\Binn\SQLCMD.EXE" -S Server -E -i C:\SQL_Scripts\AllDBBackups.sql&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Works like a charm so far.&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3999553910216009235-4581082768957057803?l=lodeofcode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lodeofcode.blogspot.com/feeds/4581082768957057803/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://lodeofcode.blogspot.com/2009/09/how-to-schedule-backups-in-sql-server.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3999553910216009235/posts/default/4581082768957057803'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3999553910216009235/posts/default/4581082768957057803'/><link rel='alternate' type='text/html' href='http://lodeofcode.blogspot.com/2009/09/how-to-schedule-backups-in-sql-server.html' title='How to schedule backups in SQL Server 2005 Express'/><author><name>Simon</name><uri>http://www.blogger.com/profile/16209703097357837049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
