<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;DkUCQnw5eCp7ImA9WhRbEk8.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542</id><updated>2012-02-02T17:11:03.220-06:00</updated><category term="NUnit" /><category term="C#" /><category term="Code Snippets" /><category term="Visual Studio 2008" /><category term="Project Management" /><category term="ASP.NET Impersonation" /><category term="Good Articles to Read" /><category term="SQL Server" /><category term="AJAX" /><category term="Thoughts" /><category term="JavaScript" /><category term=".NET Framework 3.5" /><category term="Code Samples" /><category term="ATLAS" /><category term=".NET" /><category term="ASP.NET" /><title>Sanjeev Hasiza's Blog - .NETArchitect</title><subtitle type="html">Welcome to my blog! On this page you'll find the most recent posts on .NET including SOA (services oriented architecture), ASP.NET, AJAX, Web Parts Technology and others. For older blogs, check out my archive Categories section on the right.
&lt;br&gt;&lt;br&gt;&lt;b&gt;&amp;lt;@blog.NETArchitect=&amp;quot;Sanjeev Hasiza&amp;quot; runat=&amp;quot;server&amp;gt;&lt;/b&gt;</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://architectdotnet.blogspot.com/" /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>23</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/SanjeevHasizasBlog-netarchitect" /><feedburner:info uri="sanjeevhasizasblog-netarchitect" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;DkUCQnw4eyp7ImA9WhRbEk8.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-3800326513680403244</id><published>2012-02-02T17:11:00.002-06:00</published><updated>2012-02-02T17:11:03.233-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-02-02T17:11:03.233-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Code Snippets" /><category scheme="http://www.blogger.com/atom/ns#" term="Code Samples" /><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server" /><title>Snippet: Drop all connections to a database (SQL 2005)</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;
&lt;div class="story-information" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: arial, verdana, tahoma, helvetica, sans-serif; font-size: 0.85em; padding-bottom: 3px; padding-left: 3px; padding-right: 3px; padding-top: 3px;"&gt;&lt;span style="font-size: 13px;"&gt;If you want to drop all the connections to a database immediately, you can use the following commands:&lt;/span&gt;&lt;br /&gt;
&lt;div style="padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="story-body" style="background-color: white; font-family: arial, verdana, tahoma, helvetica, sans-serif; font-size: 13px; padding-bottom: 3px; padding-left: 3px; padding-right: 3px; padding-top: 3px;"&gt;&lt;div class="code"&gt;USE master&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
ALTER DATABASE&amp;nbsp;&lt;i&gt;database name&lt;/i&gt;&lt;br /&gt;
SET OFFLINE WITH ROLLBACK IMMEDIATE&lt;br /&gt;
ALTER DATABASE&amp;nbsp;&lt;i&gt;database name&lt;/i&gt;&lt;br /&gt;
SET ONLINE&lt;/div&gt;&lt;div style="margin-top: 5px;"&gt;&lt;/div&gt;&lt;div style="margin-top: 5px;"&gt;Alternatively you can kill all the processes using a database with this code:&lt;/div&gt;&lt;div class="code"&gt;USE master&lt;br /&gt;
go&lt;br /&gt;
&lt;br /&gt;
DECLARE @dbname sysname&lt;br /&gt;
&lt;br /&gt;
SET @dbname = '&lt;i&gt;name of database you want to drop connections from&lt;/i&gt;'&lt;br /&gt;
&lt;br /&gt;
DECLARE @spid int&lt;br /&gt;
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname)&lt;br /&gt;
WHILE @spid IS NOT NULL&lt;br /&gt;
BEGIN&lt;br /&gt;
EXECUTE ('KILL ' + @spid)&lt;br /&gt;
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname) AND spid &amp;gt; @spid&lt;br /&gt;
END&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-3800326513680403244?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/qdXIGWjax01MBvqIXL8B_Rq0mz8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/qdXIGWjax01MBvqIXL8B_Rq0mz8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/qdXIGWjax01MBvqIXL8B_Rq0mz8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/qdXIGWjax01MBvqIXL8B_Rq0mz8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/x0dfUuTI2Do" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/3800326513680403244/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=3800326513680403244&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/3800326513680403244?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/3800326513680403244?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/x0dfUuTI2Do/snippet-drop-all-connections-to_02.html" title="Snippet: Drop all connections to a database (SQL 2005)" /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2012/02/snippet-drop-all-connections-to_02.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkUFQ3w9fCp7ImA9WhRbEk8.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-3638032354499844237</id><published>2012-02-02T17:10:00.000-06:00</published><updated>2012-02-02T17:10:12.264-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-02-02T17:10:12.264-06:00</app:edited><title>Snippet: Drop all connections to a database (SQL 2005)</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;
&lt;div class="story-information" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; font-family: arial, verdana, tahoma, helvetica, sans-serif; font-size: 0.85em; padding-bottom: 3px; padding-left: 3px; padding-right: 3px; padding-top: 3px;"&gt;&lt;span style="font-size: 13px;"&gt;If you want to drop all the connections to a database immediately, you can use the following commands:&lt;/span&gt;&lt;div style="padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="story-body" style="background-color: white; font-family: arial, verdana, tahoma, helvetica, sans-serif; font-size: 13px; padding-bottom: 3px; padding-left: 3px; padding-right: 3px; padding-top: 3px;"&gt;&lt;div class="code"&gt;USE master&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
ALTER DATABASE&amp;nbsp;&lt;i&gt;database name&lt;/i&gt;&lt;br /&gt;
SET OFFLINE WITH ROLLBACK IMMEDIATE&lt;br /&gt;
ALTER DATABASE&amp;nbsp;&lt;i&gt;database name&lt;/i&gt;&lt;br /&gt;
SET ONLINE&lt;/div&gt;&lt;div style="margin-top: 5px;"&gt;&lt;/div&gt;&lt;div style="margin-top: 5px;"&gt;Alternatively you can kill all the processes using a database with this code:&lt;/div&gt;&lt;div class="code"&gt;USE master&lt;br /&gt;
go&lt;br /&gt;
&lt;br /&gt;
DECLARE @dbname sysname&lt;br /&gt;
&lt;br /&gt;
SET @dbname = '&lt;i&gt;name of database you want to drop connections from&lt;/i&gt;'&lt;br /&gt;
&lt;br /&gt;
DECLARE @spid int&lt;br /&gt;
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname)&lt;br /&gt;
WHILE @spid IS NOT NULL&lt;br /&gt;
BEGIN&lt;br /&gt;
EXECUTE ('KILL ' + @spid)&lt;br /&gt;
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname) AND spid &amp;gt; @spid&lt;br /&gt;
END&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-3638032354499844237?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/0A5_h4eeypc7Ge2d8iZyrY4EVYw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/0A5_h4eeypc7Ge2d8iZyrY4EVYw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/0A5_h4eeypc7Ge2d8iZyrY4EVYw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/0A5_h4eeypc7Ge2d8iZyrY4EVYw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/_2_wcKkUBgA" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/3638032354499844237/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=3638032354499844237&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/3638032354499844237?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/3638032354499844237?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/_2_wcKkUBgA/snippet-drop-all-connections-to.html" title="Snippet: Drop all connections to a database (SQL 2005)" /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2012/02/snippet-drop-all-connections-to.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DE4CQnk4eSp7ImA9WxBaE0o.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-9116595267060098623</id><published>2010-03-23T15:33:00.005-05:00</published><updated>2010-03-23T15:42:43.731-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-03-23T15:42:43.731-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Code Samples" /><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server" /><title>Execute SQL Server DTS Package using your .NET applications / code</title><content type="html">&lt;span class="Apple-style-span" style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class="MsoNormal" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span style="color: navy; font-family: Arial; font-size: x-small;"&gt;&lt;span style="color: navy; font-family: Arial; font-size: 10pt;"&gt;Use this code to execute DTS package using your .NET application/code:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span style="color: navy; font-family: Arial; font-size: x-small;"&gt;&lt;span style="color: navy; font-family: Arial; font-size: 10pt;"&gt;(Just change the variables in red below to match your SQL Server environment)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="border-bottom-color: windowtext; border-bottom-style: solid; border-bottom-width: 1pt; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; padding-bottom: 1pt; padding-left: 0in; padding-right: 0in; padding-top: 0in;"&gt;&lt;div class="MsoNormal" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style="border-bottom-color: windowtext; border-bottom-style: solid; border-bottom-width: 1pt; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; padding-bottom: 1pt; padding-left: 0in; padding-right: 0in; padding-top: 0in;"&gt;&lt;div class="MsoNormal" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;using&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;System;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;using&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;System.Runtime.&lt;wbr&gt;&lt;/wbr&gt;InteropServices;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;using&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;DTS;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;namespace&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;DotNetArchitect.DTSPackage&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span style="color: grey; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: grey; font-family: 'Courier New'; font-size: 10pt;"&gt;///&lt;/span&gt;&lt;/span&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: grey; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: grey; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span style="color: grey; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: grey; font-family: 'Courier New'; font-size: 10pt;"&gt;///&lt;/span&gt;&lt;/span&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span class="Apple-style-span" style="color: #274e13;"&gt;C&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Arial;"&gt;&lt;span class="Apple-style-span" style="color: #274e13;"&gt;ode to execute DTS package using your .NET application/code:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span style="font-family: Arial; font-size: x-small;"&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman'; font-size: medium;"&gt;&lt;span style="font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span class="Apple-style-span" style="color: #274e13;"&gt;///&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span class="Apple-style-span" style="color: #274e13;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #274e13;"&gt;(Just change the variables in red below to match your SQL Server environment)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span style="color: grey; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: grey; font-family: 'Courier New'; font-size: 10pt;"&gt;///&lt;/span&gt;&lt;/span&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: grey; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: grey; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;Class1&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;{&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span style="color: grey; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: grey; font-family: 'Courier New'; font-size: 10pt;"&gt;///&lt;/span&gt;&lt;/span&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: grey; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: grey; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span style="color: grey; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: grey; font-family: 'Courier New'; font-size: 10pt;"&gt;///&lt;/span&gt;&lt;/span&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;The main entry point for the application.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span style="color: grey; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: grey; font-family: 'Courier New'; font-size: 10pt;"&gt;///&lt;/span&gt;&lt;/span&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: grey; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: grey; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp; [STAThread]&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp; static&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;void&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;Main(&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;string&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;[] args)&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&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;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp; Package2Class package =&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;new&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;Package2Class();&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;object&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;pVarPersistStgOfHost =&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;null&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;try&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp; {&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; package.LoadFromSQLServer(&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: red; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: red; font-family: 'Courier New'; font-size: 10pt;"&gt;"&lt;wbr&gt;&lt;/wbr&gt;SERVER_NAME"&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: 10pt;"&gt;// server name&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="color: red; font-weight: normal;"&gt;SERVER_USER_NAME&lt;/span&gt;,&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: 10pt;"&gt;// server username&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="color: red; font-weight: normal;"&gt;SERVER_PASSWORD&lt;/span&gt;,&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: 10pt;"&gt;// server password&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; DTSSQLServerStorageFlags.&lt;wbr&gt;&lt;/wbr&gt;DTSSQLStgFlag_&lt;wbr&gt;&lt;/wbr&gt;UseTrustedConnection,&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: blue;"&gt;&lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;null&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: 10pt;"&gt;// if the package has a password, otherwise null&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;null&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: 10pt;"&gt;// probably null&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;null&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: 10pt;"&gt;// probably null&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: red; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: red; font-family: 'Courier New'; font-size: 10pt;"&gt;"PACKAGE_NAME"&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;,&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: 10pt;"&gt;// name of the DTS package&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;ref&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;pVarPersistStgOfHost);&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: blue;"&gt;&lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span style="color: green; font-family: 'Times New Roman'; font-size: small;"&gt;&lt;span style="color: green; font-size: 12pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;strong&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Times New Roman';"&gt;&lt;span style="color: black;"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&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;wbr&gt;&lt;/wbr&gt;&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;/span&gt;&lt;/b&gt;&lt;/strong&gt;&lt;span style="color: blue;"&gt;&lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;strong&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Times New Roman'; font-size: small;"&gt;&lt;span style="color: black; font-size: 12pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;package.Execute();&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/strong&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: 10pt;"&gt;// execute the package&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;catch&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp; {&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;throw&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;finally&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp; {&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; package.UnInitialize();&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: 10pt;"&gt;// unwrap the package&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; System.Runtime.&lt;wbr&gt;&lt;/wbr&gt;InteropServices.Marshal.&lt;wbr&gt;&lt;/wbr&gt;ReleaseComObject(package);&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: green; font-family: 'Courier New'; font-size: 10pt;"&gt;// tell interop to release the reference&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;&amp;nbsp;}&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="border-bottom-color: windowtext; border-bottom-style: solid; border-bottom-width: 1pt; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; padding-bottom: 1pt; padding-left: 0in; padding-right: 0in; padding-top: 0in;"&gt;&lt;div class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;b&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: x-small;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 10pt; font-weight: bold;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-9116595267060098623?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/UT4ujR0E0n6fnd2rWl-i39l9yj0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/UT4ujR0E0n6fnd2rWl-i39l9yj0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/UT4ujR0E0n6fnd2rWl-i39l9yj0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/UT4ujR0E0n6fnd2rWl-i39l9yj0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/zR6Jp2DNcss" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/9116595267060098623/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=9116595267060098623&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/9116595267060098623?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/9116595267060098623?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/zR6Jp2DNcss/execute-sql-server-dts-package-using.html" title="Execute SQL Server DTS Package using your .NET applications / code" /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2010/03/execute-sql-server-dts-package-using.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkADRns7eyp7ImA9WxBaEkU.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-5028696480470843209</id><published>2010-03-22T15:12:00.000-05:00</published><updated>2010-03-22T15:12:57.503-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-03-22T15:12:57.503-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term=".NET Framework 3.5" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="NUnit" /><title>What is NUnit and How to Use it?</title><content type="html">&lt;div class="ArticleTypeTitle" id="S5"&gt;NUnit&lt;/div&gt;&lt;div class="ArticleTypeTitle"&gt;&lt;/div&gt;&lt;div class="ArticleNormalPara"&gt;NUnit is an open source unit testing framework built for the .NET Framework. NUnit allows you to write tests in the language of your choice to test a specific function of your application. Unit tests are an excellent way to test the functionality of your code when you first write it, and also to provide a method for regression testing of your application. The NUnit application provides a framework for writing unit tests, as well as a graphical interface to run these tests and view the results.&lt;/div&gt;&lt;br /&gt;
&lt;div class="ArticleTypeTitle" id="S6"&gt;Writing an NUnit Test&lt;/div&gt;&lt;div class="ArticleTypeTitle"&gt;&lt;/div&gt;&lt;div class="ArticleNormalPara"&gt;As an example, I'm going to test the functionality of the Hashtable class in the .NET Framework to determine if two objects can be added and then retrieved. My first step will be to add a reference to the NUnit.Framework assembly, which will give me access to the attributes and methods of the NUnit framework. Next I'll create a class and mark it with the TestFixture attribute. This attribute lets NUnit know that this class contains NUnit tests:&lt;br /&gt;
&lt;br /&gt;
&lt;span id="ctl00_MTContentSelector1_mainContentContainer_ctl11"&gt;&lt;div class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl11_"&gt;&lt;div dir="ltr" style="background-color: #dddddd;"&gt;&lt;pre class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl11_code"&gt;using System; using System.Collections;&amp;nbsp;&lt;/pre&gt;&lt;pre class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl11_code"&gt;using NUnit.Framework; &amp;nbsp;&lt;/pre&gt;&lt;pre class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl11_code"&gt;&lt;/pre&gt;&lt;pre class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl11_code"&gt;namespace NUnitExample&amp;nbsp;&lt;/pre&gt;&lt;pre class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl11_code"&gt;{    &amp;nbsp;&lt;/pre&gt;&lt;pre class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl11_code"&gt;[TestFixture]    &amp;nbsp;&lt;/pre&gt;&lt;pre class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl11_code"&gt;public class HashtableTest&amp;nbsp;&lt;/pre&gt;&lt;pre class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl11_code"&gt;{        &amp;nbsp;&lt;/pre&gt;&lt;pre class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl11_code"&gt;public HashtableTest()&amp;nbsp;&lt;/pre&gt;&lt;pre class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl11_code"&gt;{  }    &amp;nbsp;&lt;/pre&gt;&lt;pre class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl11_code"&gt;}&amp;nbsp;&lt;/pre&gt;&lt;pre class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl11_code"&gt;} &lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="ArticleNormalPara"&gt;&lt;/div&gt;&lt;div class="ArticleNormalPara"&gt;Next I'll create a method and mark it with the [Test] attribute so that NUnit knows that this method is a test. Then I'll set up a Hashtable and add two values to it, then use the Assert.AreEqual method to see if I can retrieve the same values that I added to the Hashtable, as shown in the following:&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;&lt;div class="ArticleNormalPara"&gt;&lt;span id="ctl00_MTContentSelector1_mainContentContainer_ctl12"&gt;&lt;div class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl12_"&gt;&lt;div dir="ltr" style="background-color: #dddddd;"&gt;&lt;pre class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl12_code"&gt;[Test] public void HashtableAddTest()&amp;nbsp;&lt;/pre&gt;&lt;pre class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl12_code"&gt;{    &amp;nbsp;&lt;/pre&gt;&lt;pre class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl12_code"&gt;Hashtable ht = new Hashtable();                 &amp;nbsp;&lt;/pre&gt;&lt;pre class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl12_code"&gt;ht.Add("Key1", "Value1");    &amp;nbsp;&lt;/pre&gt;&lt;pre class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl12_code"&gt;ht.Add("Key2", "Value2");     &amp;nbsp;&lt;/pre&gt;&lt;pre class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl12_code"&gt;Assert.AreEqual("Value1", ht["Key1"], "Wrong object returned!");    &amp;nbsp;&lt;/pre&gt;&lt;pre class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl12_code"&gt;Assert.AreEqual("Value2", ht["Key2"], "Wrong object returned!");&amp;nbsp;&lt;/pre&gt;&lt;pre class="libCScode" id="ctl00_MTContentSelector1_mainContentContainer_ctl12_code"&gt;} &lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="ArticleNormalPara"&gt;&lt;/div&gt;&lt;div class="ArticleNormalPara"&gt;This will confirm that I can add and then retrieve values from the Hashtable—a simple test, but one that showcases the capabilities of NUnit. There are a number of test types, as well as various Assert methods, that can be used to test every part of your code.&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;&lt;div class="ArticleNormalPara"&gt;To run this test, I'll need to build the project, open the generated assembly in the NUnit application, and then click the Run button. &lt;strong&gt;Figure below &lt;/strong&gt;shows the results. I get a warm and fuzzy feeling when I see that big green bar because it lets me know that the test passed. This simple example shows how easy and powerful NUnit and unit testing can be. Being able to write a unit test that can be saved and rerun whenever you change code not only makes it easier for you to detect defects in your code, but the result is that you can deliver better applications.&lt;/div&gt;&lt;div class="ArticleNormalPara"&gt;&lt;/div&gt;&lt;div class="ArticleImageSpacer"&gt;&lt;img alt="" src="http://i.msdn.microsoft.com/cc300497.fig05(en-us).gif" /&gt;&lt;/div&gt;&lt;div class="ArticleImageSpacer"&gt;&lt;/div&gt;&lt;div class="ArticleNormalPara"&gt;NUnit is an open-source project that is available for download from &lt;a href="http://www.nunit.org/"&gt;&lt;span style="color: #0066dd;"&gt;http://www.nunit.org&lt;/span&gt;&lt;/a&gt;. There is also an excellent NUnit Visual Studio .NET add-in which allows you to run unit tests directly from Visual Studio. This can be found at &lt;a href="http://sourceforge.net/projects/nunitaddin"&gt;&lt;span style="color: #0066dd;"&gt;http://sourceforge.net/projects/nunitaddin&lt;/span&gt;&lt;/a&gt;.&amp;nbsp;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-5028696480470843209?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/tnuCW7VZDAiSYMRmSU5LwGX1QyA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/tnuCW7VZDAiSYMRmSU5LwGX1QyA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/tnuCW7VZDAiSYMRmSU5LwGX1QyA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/tnuCW7VZDAiSYMRmSU5LwGX1QyA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/H4FcvJj6M6I" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/5028696480470843209/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=5028696480470843209&amp;isPopup=true" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/5028696480470843209?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/5028696480470843209?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/H4FcvJj6M6I/what-is-nunit-and-how-to-use-it.html" title="What is NUnit and How to Use it?" /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>2</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2010/03/what-is-nunit-and-how-to-use-it.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D04EQns-eSp7ImA9WxBVE0o.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-3645627237103367172</id><published>2010-02-16T21:03:00.001-06:00</published><updated>2010-02-16T21:05:03.551-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-16T21:05:03.551-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET" /><category scheme="http://www.blogger.com/atom/ns#" term="Thoughts" /><category scheme="http://www.blogger.com/atom/ns#" term="Good Articles to Read" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><title>Ten Must-Have Tools Every .NET Developer Should Have</title><content type="html">Today, I ran across Microsoft's Article written by "&lt;span class="Apple-style-span" style="color: #26252a; font-family: 'Segoe UI', Verdana, sans-serif; font-size: 14px; line-height: 15px;"&gt;James Avery"&amp;nbsp;&lt;/span&gt;on .NET must have tools. I thought I should share it with everyone. Here is the list of must-have .NET tools every .net developer should use:&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;.NET Tools&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="color: #26252a; font-family: 'Segoe UI', Arial; font-size: 14px; line-height: 15px;"&gt;NUnit to write unit tests&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="color: #26252a; font-family: 'Segoe UI', Arial; font-size: 14px; line-height: 15px;"&gt;NDoc to create code documentation&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="color: #26252a; font-family: 'Segoe UI', Arial; font-size: 14px; line-height: 15px;"&gt;NAnt to build your solutions&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="color: #26252a; font-family: 'Segoe UI', Arial; font-size: 14px; line-height: 15px;"&gt;CodeSmith to generate code&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="color: #26252a; font-family: 'Segoe UI', Arial; font-size: 14px; line-height: 15px;"&gt;FxCop to police your code&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="color: #26252a; font-family: 'Segoe UI', Arial; font-size: 14px; line-height: 15px;"&gt;Snippet Compiler to compile small bits of code&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="color: #26252a; font-family: 'Segoe UI', Arial; font-size: 14px; line-height: 15px;"&gt;Two different switcher tools, the &lt;a href="http://asp.net/"&gt;ASP.NET&lt;/a&gt; Version Switcher and the Visual Studio .NET Project Converter&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="color: #26252a; font-family: 'Segoe UI', Arial; font-size: 14px; line-height: 15px;"&gt;Regulator to build regular expressions&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="color: #26252a; font-family: 'Segoe UI', Arial; font-size: 14px; line-height: 15px;"&gt;.NET Reflector to examine assemblies&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-3645627237103367172?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/lnczJW3KuR-aL50aN8cdCc_aGT4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/lnczJW3KuR-aL50aN8cdCc_aGT4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/lnczJW3KuR-aL50aN8cdCc_aGT4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/lnczJW3KuR-aL50aN8cdCc_aGT4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/m1qiAtPlNdw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/3645627237103367172/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=3645627237103367172&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/3645627237103367172?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/3645627237103367172?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/m1qiAtPlNdw/ten-must-have-tools-every-net-developer.html" title="Ten Must-Have Tools Every .NET Developer Should Have" /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2010/02/ten-must-have-tools-every-net-developer.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkUERXsyfSp7ImA9WxBVEko.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-7784603864710050186</id><published>2010-02-15T16:23:00.002-06:00</published><updated>2010-02-15T17:56:44.595-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-15T17:56:44.595-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET Impersonation" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET Framework 3.5" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Code Samples" /><title>ASP.NET Impersonation - Part 2</title><content type="html">This article is in continuation with my previous article "&lt;a href="http://architectdotnet.blogspot.com/2010/02/aspnet-impersonation-part-1.html"&gt;ASP.NET Impersonation - Part 1&lt;/a&gt;", in Part-1 I described how can we do impersonation using IIS and NTFS folder permissions. In this article I am going to explain how can we do Impersonation using custom code. The code example I am giving below is actually what I wrote long time back to authenticate my application over the network and there was a need to Impersonate users using the user credentials and I couldn't use the&lt;a href="http://architectdotnet.blogspot.com/2010/02/aspnet-impersonation-part-1.html"&gt; IIS way of&amp;nbsp;impersonation&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Custom code to Impersonate application users&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
I have published the source code below which is a custom Impersonation class I wrote. Just copy this source code in your project as a new class and name it "cSiteImpersonation.cs" and add a reference of this class in your client project. After that is done, just call the function "ImpersonateUser(..... )" with valid credentials right before the point where you need to access the network drive/folder (where impersonation required to access the resources) and that should work.&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Here is the definition&amp;nbsp;for the&amp;nbsp;function&amp;nbsp;&lt;b&gt;ImpersonateUser&lt;/b&gt;:&lt;/li&gt;
&lt;/ul&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ImpersonateUser(&lt;/b&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;ByVal sUserName As String, _&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-size: small;"&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ByVal sPassword As String, _&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-size: small;"&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ByVal sDomain As String&lt;/span&gt;&lt;/i&gt;&lt;b&gt;)&lt;/b&gt;&amp;nbsp;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;Here is the source code for the Impersonation class:&lt;/li&gt;
&lt;/ul&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;/********************************************/&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #38761d;"&gt;/**** &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SOURCE CODE EXAMPLE &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;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;/********************************************/&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;using &lt;/span&gt;System;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;using&amp;nbsp;&lt;/span&gt;System.Security.Principal;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;using&amp;nbsp;&lt;/span&gt;System.Security.Permissions;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;using&amp;nbsp;&lt;/span&gt;System.Runtime.InteropServices;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;namespace &lt;/span&gt;Sanjeev.NETArchitect.Apps.Security.AuthenticateAndAuthorize&lt;/div&gt;&lt;div&gt;{&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &lt;span class="Apple-style-span" style="color: blue;"&gt;&amp;nbsp;public class &lt;/span&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;span class="Apple-style-span" style="color: #3d85c6;"&gt;cSiteImpersonation&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class="Apple-style-span" style="color: blue;"&gt;#region&lt;/span&gt; "Declarations"&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class="Apple-style-span" style="color: blue;"&gt;private enum&lt;/span&gt; Logon&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Network = 3,&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;NetworkCleartext = 8&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class="Apple-style-span" style="color: blue;"&gt;private enum&lt;/span&gt; Provider&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;UseDefault = 0,&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;WindowsNT35 = 1,&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;WindowsNT40 = 2,&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Windows2000 = 3&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;#endregion&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;#region "Public Functions"&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class="Apple-style-span" style="color: blue;"&gt;public WindowsImpersonationContex&lt;/span&gt;t ImpersonateUser(string sUserName, string sPassword, string sDomain)&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;//-----------------------------------------&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Function: &amp;nbsp; &amp;nbsp; ImpersonateUser&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Description: &amp;nbsp;Changes to windows identity given the input parameters.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Inputs: &amp;nbsp; &amp;nbsp; &amp;nbsp; sUserName - The User to impersonate&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&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; sPassword - The Password for the impersonated User.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&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; sDomain - &amp;nbsp; The Domain that is being accessed&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Outputs: &amp;nbsp; &amp;nbsp; &amp;nbsp;WindowsImpersonationContext, the Context for the Impersonation&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class="Apple-style-span" style="color: black;"&gt;&amp;nbsp;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;//-----------------------------------------&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;WindowsIdentity objNewIdentity = default(WindowsIdentity);&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;objNewIdentity = GetWindowsIdentity(sUserName, sDomain, sPassword);&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return objNewIdentity.Impersonate;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class="Apple-style-span" style="color: blue;"&gt;public WindowsIdentity&lt;/span&gt; GetCurrentIdentity()&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;//-----------------------------------------&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Function: &amp;nbsp; &amp;nbsp; GetCurrentIdentity&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Description: &amp;nbsp;Returns the current windows identity.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Inputs: &amp;nbsp; &amp;nbsp; &amp;nbsp; None&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Outputs: &amp;nbsp; &amp;nbsp; &amp;nbsp;WindowsIdentity, the current identity&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span class="Apple-style-span" style="color: black;"&gt;&amp;nbsp;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;//-----------------------------------------&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return WindowsIdentity.GetCurrent();&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class="Apple-style-span" style="color: blue;"&gt;public WindowsIdentity&lt;/span&gt; RevertIdentity(WindowsImpersonationContext CurrentImpersonation)&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;//-----------------------------------------&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Function: &amp;nbsp; &amp;nbsp; RevertIdentity&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Description: &amp;nbsp;Undoes the impersonation, which reverts to the windows identity&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&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; prior to the impersonation&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Inputs: &amp;nbsp; &amp;nbsp; &amp;nbsp; None&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Outputs: &amp;nbsp; &amp;nbsp; &amp;nbsp;WindowsIdentity, the current identity&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class="Apple-style-span" style="color: black;"&gt;&amp;nbsp;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;//-----------------------------------------&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;CurrentImpersonation.Undo();&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;#endregion&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;#region "Private Functions"&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;[DllImport("advapi32.dll")]&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class="Apple-style-span" style="color: blue;"&gt;&amp;nbsp;private static extern bool LogonUse&lt;/span&gt;r(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref int phToken);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;//-----------------------------------------&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Function: &amp;nbsp; &amp;nbsp; LogonUser&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Description: &amp;nbsp;Uses the windows32 advapi32.dll to logon to the windows&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&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; environment as another user.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Inputs: &amp;nbsp; &amp;nbsp; &amp;nbsp; lpszUsername - &amp;nbsp; &amp;nbsp; &amp;nbsp;The User to impersonate&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&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; lpszDomain - &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;The Domain that is being accessed&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&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; lpszPassword - &amp;nbsp; &amp;nbsp; &amp;nbsp;The Password for the impersonated User&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&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; dwLogonType - &amp;nbsp; &amp;nbsp; &amp;nbsp; Logon type&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&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; dwLogonProvider - &amp;nbsp; The type of windows provider&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&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; phToken - &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Returned Security Token&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Outputs: &amp;nbsp; &amp;nbsp; &amp;nbsp;Boolean, Success or Failure&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class="Apple-style-span" style="color: black;"&gt;&amp;nbsp;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;//-----------------------------------------&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;[DllImport("Kernel32.dll")]&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class="Apple-style-span" style="color: blue;"&gt;private static extern int GetLastError&lt;/span&gt;();&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&lt;span class="Apple-style-span" style="color: black;"&gt;&amp;nbsp;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;//-----------------------------------------&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Function: &amp;nbsp; &amp;nbsp; GetLastError&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Description: &amp;nbsp;Used the Kernal32.dll to get the last error .&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Inputs: &amp;nbsp; &amp;nbsp; &amp;nbsp; None&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Outputs: &amp;nbsp; &amp;nbsp; &amp;nbsp;WindowsIdentity, the Context for the Impersonation&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span class="Apple-style-span" style="color: black;"&gt;&amp;nbsp;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;//-----------------------------------------&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;[SecurityPermissionAttribute(SecurityAction.Demand, ControlPrincipal = true, UnmanagedCode = true)]&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class="Apple-style-span" style="color: blue;"&gt;private static WindowsIdentity GetWindowsIdentity&lt;/span&gt;(string UserName, string Domain, string Password)&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;//-----------------------------------------&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="background-color: #f3f3f3;"&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Function: &amp;nbsp; &amp;nbsp; GetWindowsIdentity&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="background-color: #f3f3f3;"&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Description: &amp;nbsp;Logon the user and return the windows identity given&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="background-color: #f3f3f3;"&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&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; the input parameters.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="background-color: #f3f3f3;"&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="background-color: #f3f3f3;"&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Inputs: &amp;nbsp; &amp;nbsp; &amp;nbsp; sUserName - The User to impersonate&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="background-color: #f3f3f3;"&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&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; sDomain - &amp;nbsp; The Domain that is being accessed&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="background-color: #f3f3f3;"&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&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; sPassword - The Password for the impersonated User.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="background-color: #f3f3f3;"&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="background-color: #f3f3f3;"&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Outputs: &amp;nbsp; &amp;nbsp; &amp;nbsp;WindowsIdentity, the Context for the Impersonation&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;//-----------------------------------------&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;int SecurityToken = 0;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;bool Success = false;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Success = LogonUser(UserName, Domain, Password, Logon.Network, Provider.UseDefault, SecurityToken);&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (!Success)&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;throw new Exception("Logon Failed. Error: " + GetLastError());&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new WindowsIdentity(new IntPtr(SecurityToken));&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;#endregion&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;/********************************************/&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-7784603864710050186?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/RBSM9r-YyJNXDwqYtWTl7Yrsh7Q/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/RBSM9r-YyJNXDwqYtWTl7Yrsh7Q/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/RBSM9r-YyJNXDwqYtWTl7Yrsh7Q/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/RBSM9r-YyJNXDwqYtWTl7Yrsh7Q/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/_e5uhlV_8KI" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/7784603864710050186/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=7784603864710050186&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/7784603864710050186?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/7784603864710050186?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/_e5uhlV_8KI/aspnet-impersonation-part-2.html" title="ASP.NET Impersonation - Part 2" /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2010/02/aspnet-impersonation-part-2.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkUFRns8eyp7ImA9WxBVEko.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-7056077407671880884</id><published>2010-02-14T15:42:00.001-06:00</published><updated>2010-02-15T17:56:57.573-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-15T17:56:57.573-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET Impersonation" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET Framework 3.5" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Code Samples" /><title>ASP.NET Impersonation - Part 1</title><content type="html">&lt;div style="text-align: left;"&gt;There are two ways of doing Impersonation in .NET:&lt;/div&gt;&lt;div&gt;1. &lt;a href="http://architectdotnet.blogspot.com/2010/02/aspnet-impersonation-part-1.html"&gt;Let the IIS handle it&lt;/a&gt;&lt;/div&gt;&lt;div&gt;2. &lt;a href="http://architectdotnet.blogspot.com/2010/02/aspnet-impersonation-part-2.html"&gt;Custom code to Impersonate application users&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;I will try to explain both ways of doing Impersonation as two different articles. Let me start with the option-1, which is the easiest out of these two and is&amp;nbsp;preferred&amp;nbsp;these days.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;u&gt;IIS way of doing Impersonation&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;When using impersonation, ASP.NET applications can optionally execute with the identity of the client on whose behalf they are operating. The usual reason for doing this is to avoid dealing with authentication and authorization issues in the ASP.NET application code. Instead, you rely on IIS to authenticate the user and either pass an authenticated token to the ASP.NET application or, if unable to authenticate the user, pass an unauthenticated token. In either case, the ASP.NET application impersonates whichever token is received if Impersonation is enabled. The ASP.NET application, now impersonating the client, then relies on the settings in the NTFS directories and files to allow it to gain access, or not.&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;For ASP compatibility, impersonation is disabled by default. If impersonation is enabled for a given application, ASP.NET always impersonates the access token that IIS provides to ISAPI extensions. That token can be either an authenticated user token, or the token for the anonymous user ( such as IUSR_MACHINENAME ). The impersonation occurs regardless of the type of authentication being used in the application and whether the user is authenticated, in which case, it is the anonymous user token.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;Only application code is impersonated; compilation and configuration are read as the process token. If an application is on a UNC share, ASP.NET will always impersonate the token provided to IIS to access that share unless a configured account is used. If an explicit configured account is provided, ASP.NET will use that account in preference to the IIS UNC token. Applications that do want per-request impersonation can simply be configured to impersonate the user making the request.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;Impersonation is disabled at the computer level by default and, unless overridden, all the application domains inherit this setting. You can enable impersonation by putting a configuration file in the application root directory.&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;As is the case with other configuration directives, this directive applies hierarchically. It is respected by nested applications in the hierarchy, unless explicitly overridden. The default value for this setting is as follows:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;i&gt;&lt;impersonation enable="false"&gt;&lt;/impersonation&gt;&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;A minimal configuration file to enable impersonation for an application might look similar to the following example:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;i&gt;&lt;identity impersonate="true" name="username" password="password"&gt;&lt;/identity&gt;&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;There is also name support for running an application as a configurable identity. For example:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;i&gt;&lt;identity impersonate="true" name="domain\username" password="password"&gt;&lt;/identity&gt;&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;This enables the entire application to run as domain\username, regardless of the identity of the request, so long as the password is correct. This type of impersonation can be delegated to another computer.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;This concludes the IIS way of handling Impersonation. I will write another article tomorrow to explain how to write a custom code to Impersonation users for the application.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-7056077407671880884?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Srn9QqpezLSiY7WNJ9CGW-fc3Gw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Srn9QqpezLSiY7WNJ9CGW-fc3Gw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Srn9QqpezLSiY7WNJ9CGW-fc3Gw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Srn9QqpezLSiY7WNJ9CGW-fc3Gw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/N6MlQ3kgX8Y" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/7056077407671880884/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=7056077407671880884&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/7056077407671880884?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/7056077407671880884?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/N6MlQ3kgX8Y/aspnet-impersonation-part-1.html" title="ASP.NET Impersonation - Part 1" /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2010/02/aspnet-impersonation-part-1.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkUBRXs_cCp7ImA9WxBVEko.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-1747225770663126312</id><published>2010-02-09T14:22:00.001-06:00</published><updated>2010-02-15T17:57:34.548-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-15T17:57:34.548-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Good Articles to Read" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><title>Better Coding Rules &amp; Coding Standards for .NET Apps and SQL Server</title><content type="html">&lt;span class="Apple-style-span" style="font-family: Arial; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman';"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;span class="Apple-style-span" style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman';"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman';"&gt;&lt;div&gt;Followings are the great links I use for coding standards/rules references...&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
Rules to Better ...&lt;/div&gt;&lt;div&gt;&lt;a href="http://www.ssw.com.au/SSW/Standards/default.aspx" style="color: #1c51a8;" target="_blank"&gt;http://www.ssw.com.au/SSW/&lt;wbr&gt;&lt;/wbr&gt;Standards/default.aspx&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;u&gt;.NET Apps&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;Rules to Better .NET Projects:&lt;/div&gt;&lt;div&gt;&lt;a href="http://www.ssw.com.au/ssw/Standards/Rules/RulesToBetterdotNETProjects.aspx" style="color: #1c51a8;" target="_blank"&gt;http://www.ssw.com.au/ssw/&lt;wbr&gt;&lt;/wbr&gt;Standards/Rules/&lt;wbr&gt;&lt;/wbr&gt;RulesToBetterdotNETProjects.&lt;wbr&gt;&lt;/wbr&gt;aspx&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;Rules to Better Code:&lt;/div&gt;&lt;div&gt;&lt;a href="http://www.ssw.com.au/ssw/Standards/Rules/RulestoBetterCode.aspx#NamingConventions" style="color: #1c51a8;" target="_blank"&gt;http://www.ssw.com.au/ssw/&lt;wbr&gt;&lt;/wbr&gt;Standards/Rules/&lt;wbr&gt;&lt;/wbr&gt;RulestoBetterCode.aspx#&lt;wbr&gt;&lt;/wbr&gt;NamingConventions&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;u&gt;SQL Server&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Rules to Better SQL Server Databases:&lt;/div&gt;&lt;div&gt;&lt;a href="http://www.ssw.com.au/SSW/standards/rules/RulesToBetterSQLServerDatabases.aspx" style="color: #1c51a8;" target="_blank"&gt;http://www.ssw.com.au/SSW/&lt;wbr&gt;&lt;/wbr&gt;standards/rules/&lt;wbr&gt;&lt;/wbr&gt;RulesToBetterSQLServerDatabase&lt;wbr&gt;&lt;/wbr&gt;s.aspx&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;SQL Server Object Naming Standard:&lt;/div&gt;&lt;div&gt;&lt;a href="http://www.ssw.com.au/ssw/Standards/DeveloperSQLServer/SQLServerStandard_1_ObjectNaming.aspx" style="color: #1c51a8;" target="_blank"&gt;http://www.ssw.com.au/ssw/&lt;wbr&gt;&lt;/wbr&gt;Standards/DeveloperSQLServer/&lt;wbr&gt;&lt;/wbr&gt;SQLServerStandard_1_&lt;wbr&gt;&lt;/wbr&gt;ObjectNaming.aspx&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;SQL Stored Procedure Naming Standard:&lt;/div&gt;&lt;div&gt;&lt;a href="http://www.ssw.com.au/ssw/Standards/DeveloperSQLServer/SQLServerStandard_2_StoredProcedureNaming.aspx" style="color: #1c51a8;" target="_blank"&gt;http://www.ssw.com.au/ssw/&lt;wbr&gt;&lt;/wbr&gt;Standards/DeveloperSQLServer/&lt;wbr&gt;&lt;/wbr&gt;SQLServerStandard_2_&lt;wbr&gt;&lt;/wbr&gt;StoredProcedureNaming.aspx&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;SQL Stored Procedure Formatting Standard:&lt;/div&gt;&lt;div&gt;&lt;a href="http://www.ssw.com.au/ssw/Standards/DeveloperSQLServer/SQLServerStandard_3_StoredProcedureFormatting.aspx" style="color: #1c51a8;" target="_blank"&gt;http://www.ssw.com.au/ssw/&lt;wbr&gt;&lt;/wbr&gt;Standards/DeveloperSQLServer/&lt;wbr&gt;&lt;/wbr&gt;SQLServerStandard_3_&lt;wbr&gt;&lt;/wbr&gt;StoredProcedureFormatting.aspx&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;SQL Server Indexes Naming Standard:&lt;/div&gt;&lt;div&gt;&lt;a href="http://www.ssw.com.au/ssw/Standards/DeveloperSQLServer/SQLServerStandard_4_IndexesNaming.aspx" style="color: #1c51a8;" target="_blank"&gt;http://www.ssw.com.au/ssw/&lt;wbr&gt;&lt;/wbr&gt;Standards/DeveloperSQLServer/&lt;wbr&gt;&lt;/wbr&gt;SQLServerStandard_4_&lt;wbr&gt;&lt;/wbr&gt;IndexesNaming.aspx&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;SQL Server Relationship Naming Standard:&lt;/div&gt;&lt;div&gt;&lt;a href="http://www.ssw.com.au/ssw/Standards/DeveloperSQLServer/SQLServerStandard_5_RelationshipNaming.aspx" style="color: #1c51a8;" target="_blank"&gt;http://www.ssw.com.au/ssw/&lt;wbr&gt;&lt;/wbr&gt;Standards/DeveloperSQLServer/&lt;wbr&gt;&lt;/wbr&gt;SQLServerStandard_5_&lt;wbr&gt;&lt;/wbr&gt;RelationshipNaming.aspx&lt;/a&gt;&lt;/div&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/8272135192268931542-1747225770663126312?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/XSCaLunk6RkXq_7MrFZmodgbEpU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XSCaLunk6RkXq_7MrFZmodgbEpU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/XSCaLunk6RkXq_7MrFZmodgbEpU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XSCaLunk6RkXq_7MrFZmodgbEpU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/JlXGGIu2KXk" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/1747225770663126312/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=1747225770663126312&amp;isPopup=true" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/1747225770663126312?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/1747225770663126312?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/JlXGGIu2KXk/better-coding-rules-coding-standards_09.html" title="Better Coding Rules &amp; Coding Standards for .NET Apps and SQL Server" /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>1</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2010/02/better-coding-rules-coding-standards_09.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkUNRno_eip7ImA9WxBVEko.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-7686755319524399948</id><published>2010-02-08T14:06:00.001-06:00</published><updated>2010-02-15T17:58:17.442-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-15T17:58:17.442-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Code Samples" /><category scheme="http://www.blogger.com/atom/ns#" term="SQL Server" /><title>Automating Database maintenance in SQL 2005 Express Edition Part I</title><content type="html">I had a requirement to Automate the database backups etc in SQL Server 2005 and found &lt;a href="http://www.sqldbatips.com/showarticle.asp?ID=27"&gt;this article&lt;/a&gt;. It is very interesting. If have same type of requirement and you need a script to automate the backups, you can follow this script below, just change the variable names to match your server and database.&lt;br /&gt;
&lt;br /&gt;
It is a long script, make sure you copy everything below.&lt;br /&gt;
&lt;br /&gt;
/***********************************************/&lt;br /&gt;
/*     Script Starts Here                      */ &lt;br /&gt;
/***********************************************/&lt;br /&gt;
use master&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[expressmaint]') &lt;br /&gt;
and OBJECTPROPERTY(id, N'IsProcedure') = 1)&lt;br /&gt;
drop procedure [dbo].[expressmaint]&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
CREATE PROCEDURE expressmaint&lt;br /&gt;
(&lt;br /&gt;
@database      sysname,                   -- database name | ALL_USER | ALL_SYSTEM&lt;br /&gt;
@optype        varchar(7),                -- LOG | DB | DIFF | REINDEX | REORG | CHECKDB&lt;br /&gt;
@backupwith    varchar(500) = NULL,       -- additional backup options&lt;br /&gt;
@backupfldr    varchar(200) = NULL,       -- folder to write backup to &lt;br /&gt;
@reportfldr    varchar(200) = NULL,       -- folder to write text report &lt;br /&gt;
@verify        bit = 1,                   -- verify backup&lt;br /&gt;
@verifywith    varchar(500) = NULL,       -- additional verify options&lt;br /&gt;
@dbretainunit  varchar(10)  = NULL,       -- minutes | hours | days | weeks | months | copies&lt;br /&gt;
@dbretainval   int = 1,                   -- specifies how many retainunits to keep backup&lt;br /&gt;
@report        bit = 1,                   -- flag to indicate whether to generate report&lt;br /&gt;
@rptretainunit varchar(10)  = NULL,       -- minutes | hours | days | weeks | months | copies&lt;br /&gt;
@rptretainval  int = 1,                   -- specifies how many retainunits to keep reports&lt;br /&gt;
@checkattrib   bit = 0,                   -- check if archive bit is cleared before deleting&lt;br /&gt;
@delfirst      bit = 0,                   -- delete before backup (handy if space issues)&lt;br /&gt;
@debug         bit = 0                    -- print commands to be executed&lt;br /&gt;
)&lt;br /&gt;
AS&lt;br /&gt;
&lt;br /&gt;
SET NOCOUNT ON&lt;br /&gt;
SET DATEFORMAT YMD&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
VARIABLE DECLARATION&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
DECLARE @fso             int &lt;br /&gt;
DECLARE @file            int &lt;br /&gt;
DECLARE @reportfilename  varchar(500) &lt;br /&gt;
DECLARE @backupfilename  varchar(500) &lt;br /&gt;
DECLARE @delfilename     varchar(500)&lt;br /&gt;
DECLARE @cmd             varchar(650)&lt;br /&gt;
DECLARE @backupfldrorig  varchar(200)&lt;br /&gt;
DECLARE @databaseorig    sysname&lt;br /&gt;
DECLARE @table           nvarchar(600)&lt;br /&gt;
DECLARE @exists          varchar(5)&lt;br /&gt;
DECLARE @err             int&lt;br /&gt;
DECLARE @start           datetime&lt;br /&gt;
DECLARE @finish          datetime&lt;br /&gt;
DECLARE @runtime         datetime&lt;br /&gt;
DECLARE @output          varchar(200)&lt;br /&gt;
DECLARE @errormsg        varchar(210)&lt;br /&gt;
DECLARE @datepart        nchar(2)&lt;br /&gt;
DECLARE @execmd          nvarchar(1000)&lt;br /&gt;
DECLARE @delcmd          nvarchar(1000)&lt;br /&gt;
DECLARE @exemsg          varchar(8000)&lt;br /&gt;
DECLARE @filecount       int              ; SET @filecount    = 0&lt;br /&gt;
DECLARE @delcount        int              ; SET @delcount     = 0&lt;br /&gt;
DECLARE @hr              int              ; SET @hr           = 0&lt;br /&gt;
DECLARE @ret             int              ; SET @ret          = 0&lt;br /&gt;
DECLARE @cmdret          int              ; SET @cmdret       = 0&lt;br /&gt;
DECLARE @delbkflag       int              ; SET @delbkflag    = 0&lt;br /&gt;
DECLARE @delrptflag      int              ; SET @delrptflag   = 0&lt;br /&gt;
DECLARE @filecrt         int              ; SET @filecrt      = 0&lt;br /&gt;
DECLARE @user            sysname          ; SET @user         = SUSER_SNAME()&lt;br /&gt;
DECLARE @jobdt           datetime         ; SET @jobdt        = GETDATE()&lt;br /&gt;
DECLARE @jobstart        char(12)         ; &lt;br /&gt;
DECLARE @stage           int              ; SET @stage        = 1&lt;br /&gt;
&lt;br /&gt;
SET @jobstart = CONVERT(char(8),@jobdt,112)+LEFT(REPLACE(CONVERT(char(8),@jobdt,108),':',''),4)   &lt;br /&gt;
IF RIGHT(@reportfldr,1)&amp;lt;&amp;gt;'\' SET @reportfldr = @reportfldr + '\'&lt;br /&gt;
IF RIGHT(@backupfldr,1)&amp;lt;&amp;gt;'\' SET @backupfldr = @backupfldr + '\'&lt;br /&gt;
SET @backupfldrorig = @backupfldr&lt;br /&gt;
SET @databaseorig = @database&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE #files(filename varchar(255))  &lt;br /&gt;
CREATE TABLE #exists(exist int,isdir int,parent int)&lt;br /&gt;
CREATE TABLE #databases(dbname sysname)&lt;br /&gt;
&lt;br /&gt;
/**********************************&lt;br /&gt;
INITIALIZE FSO IF @report = 1&lt;br /&gt;
***********************************/&lt;br /&gt;
&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC @hr=sp_OACreate 'Scripting.FileSystemObject',@fso OUT &lt;br /&gt;
IF @hr &amp;lt;&amp;gt; 0 &lt;br /&gt;
BEGIN   &lt;br /&gt;
EXEC sp_OAGetErrorInfo @fso&lt;br /&gt;
RAISERROR('Error creating File System Object',16,1)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
CHECK INPUT&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
-- check SQL2005 or higher&lt;br /&gt;
IF (select SUBSTRING(@@version,(CHARINDEX('-',@@version)+2),1))&amp;lt;9 BEGIN                        RAISERROR('SQL2005 or higher is required for sp_expressmaint',16,1) SET @ret = 1 GOTO CLEANUP  END  -- check sysadmin IF IS_SRVROLEMEMBER('sysadmin') = 0 BEGIN                        RAISERROR('The current user %s is not a member of the sysadmin role',16,1,@user) SET @ret = 1 GOTO CLEANUP  END  -- check database exists and is online IF @database NOT IN ('ALL_USER','ALL_SYSTEM') BEGIN IF (DB_ID(@database) IS NULL) OR ((select state from sys.databases where name = @database) &amp;lt;&amp;gt;0)&lt;br /&gt;
BEGIN                       &lt;br /&gt;
RAISERROR('Database %s is invalid or database status is not ONLINE',16,1,@database)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP  &lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- check @optype is valid&lt;br /&gt;
IF UPPER(@optype) NOT IN ('LOG','DB','DIFF','REINDEX','REORG','CHECKDB')&lt;br /&gt;
BEGIN                       &lt;br /&gt;
RAISERROR('%s is not a valid option for @optype',16,1,@optype)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP  &lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- check recovery mode is correct if trying log backup&lt;br /&gt;
IF @database NOT IN ('ALL_USER','ALL_SYSTEM')&lt;br /&gt;
BEGIN&lt;br /&gt;
IF (@optype = 'LOG' and ((select recovery_model from sys.databases where name = @database) = 3))&lt;br /&gt;
BEGIN                       &lt;br /&gt;
RAISERROR('%s is not a valid option for database %s because it is in SIMPLE recovery mode',16,1,@optype,@database)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- no log backups for system databases&lt;br /&gt;
IF @database = 'ALL_SYSTEM'&lt;br /&gt;
BEGIN&lt;br /&gt;
IF @optype = 'LOG'&lt;br /&gt;
BEGIN                       &lt;br /&gt;
RAISERROR('%s is not a valid option for the option ALL_SYSTEM',16,1,@optype)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- check that @backupfldr exists on the server&lt;br /&gt;
IF @optype NOT IN ('REINDEX','CHECKDB','REORG')&lt;br /&gt;
BEGIN&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @fso,'FolderExists',@exists OUT,@backupfldr&lt;br /&gt;
IF @exists &amp;lt;&amp;gt; 'True'&lt;br /&gt;
BEGIN                       &lt;br /&gt;
RAISERROR('The folder %s does not exist on this server',16,1,@backupfldr)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
ELSE&lt;br /&gt;
BEGIN&lt;br /&gt;
INSERT #exists&lt;br /&gt;
EXEC master.dbo.xp_fileexist @backupfldr&lt;br /&gt;
IF (SELECT MAX(isdir) FROM #exists)&amp;lt;&amp;gt;1&lt;br /&gt;
BEGIN                       &lt;br /&gt;
RAISERROR('The folder %s does not exist on this server',16,1,@backupfldr)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- check that @reportfldr exists on the server&lt;br /&gt;
IF @reportfldr IS NOT NULL or @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @fso,'FolderExists',@exists OUT,@reportfldr&lt;br /&gt;
IF @exists &amp;lt;&amp;gt; 'True'&lt;br /&gt;
BEGIN                       &lt;br /&gt;
RAISERROR('The folder %s does not exist on this server',16,1,@reportfldr)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
ELSE&lt;br /&gt;
BEGIN&lt;br /&gt;
DELETE #exists&lt;br /&gt;
INSERT #exists&lt;br /&gt;
EXEC master.dbo.xp_fileexist @reportfldr&lt;br /&gt;
IF (SELECT MAX(isdir) FROM #exists)&amp;lt;&amp;gt;1&lt;br /&gt;
BEGIN                       &lt;br /&gt;
RAISERROR('The folder %s does not exist on this server',16,1,@reportfldr)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- check @dbretainunit is a vaild value&lt;br /&gt;
IF @optype NOT IN ('REINDEX','CHECKDB','REORG')&lt;br /&gt;
BEGIN&lt;br /&gt;
IF UPPER(@dbretainunit) NOT IN ('MINUTES','HOURS','DAYS','WEEKS','MONTHS','COPIES')&lt;br /&gt;
BEGIN                       &lt;br /&gt;
RAISERROR('%s is not a valid value for @dbretainunit (''minutes | hours | days | weeks | months | copies'')',16,1,@dbretainunit)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
--check @dbretainval is a vaild value&lt;br /&gt;
IF @dbretainval&amp;lt;1 BEGIN                        RAISERROR('%i is not a valid value for @dbretainval (must be &amp;gt;0)',16,1,@dbretainval)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- check @rptretainunit is a vaild value if present&lt;br /&gt;
IF UPPER(@rptretainunit) NOT IN ('MINUTES','HOURS','DAYS','WEEKS','MONTHS','COPIES') and @rptretainunit IS NOT NULL&lt;br /&gt;
BEGIN                       &lt;br /&gt;
RAISERROR('%s is not a valid value for @rptretainunit (''minutes | hours | days | weeks | months | copies'')',16,1,@rptretainunit)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
--check @rptretainval is a vaild value&lt;br /&gt;
IF @rptretainval&amp;lt;1 BEGIN                        RAISERROR('%i is not a valid value for @rptretainval (must be &amp;gt;0)',16,1,@rptretainval)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
/***********************************&lt;br /&gt;
list of databases to process&lt;br /&gt;
************************************/&lt;br /&gt;
&lt;br /&gt;
IF @database IN ('ALL_USER','ALL_SYSTEM')&lt;br /&gt;
BEGIN&lt;br /&gt;
IF @database = 'ALL_USER'&lt;br /&gt;
INSERT #databases(dbname) &lt;br /&gt;
SELECT [name] from sys.databases where database_id &amp;gt; 4&lt;br /&gt;
AND (@optype &amp;lt;&amp;gt; 'LOG' OR recovery_model &amp;lt;&amp;gt; '3')&lt;br /&gt;
ELSE&lt;br /&gt;
INSERT #databases(dbname) &lt;br /&gt;
SELECT [name] from sys.databases where database_id in (1,3,4)&lt;br /&gt;
END&lt;br /&gt;
ELSE&lt;br /&gt;
INSERT #databases(dbname) SELECT @database&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/***********************************&lt;br /&gt;
INITIALIZE REPORT IF @report = 1&lt;br /&gt;
************************************/&lt;br /&gt;
&lt;br /&gt;
-- generate report filename&lt;br /&gt;
SELECT @reportfilename = @reportfldr + REPLACE(@database,' ','_') +&lt;br /&gt;
CASE WHEN UPPER(@optype) = 'DB'   THEN '_FullDBBackup_report_'&lt;br /&gt;
WHEN UPPER(@optype) = 'DIFF' THEN '_DiffDBBackup_report_'&lt;br /&gt;
WHEN UPPER(@optype) = 'LOG'  THEN '_LogBackup_report_'   &lt;br /&gt;
WHEN UPPER(@optype) = 'REINDEX'  THEN '_Reindex_report_'    &lt;br /&gt;
WHEN UPPER(@optype) = 'REORG'  THEN '_Reorg_report_'   &lt;br /&gt;
WHEN UPPER(@optype) = 'CHECKDB'  THEN '_CheckDB_report_'      &lt;br /&gt;
END + @jobstart + '.txt'&lt;br /&gt;
&lt;br /&gt;
-- if no report just set @reportfilename to NULL&lt;br /&gt;
IF @report = 0 SET @reportfilename = NULL&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
PRINT '@reportfilename = ' + ISNULL(@reportfilename,'NULL')&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
-- create report file&lt;br /&gt;
EXEC @hr=sp_OAMethod @fso, 'CreateTextFile',@file OUT, @reportfilename&lt;br /&gt;
IF (@hr &amp;lt;&amp;gt; 0)&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAGetErrorInfo @fso &lt;br /&gt;
RAISERROR('Error creating log file',16,1)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
ELSE&lt;br /&gt;
-- set global flag to indicate we have created a report file&lt;br /&gt;
SET @filecrt = 1&lt;br /&gt;
&lt;br /&gt;
-- write header   &lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
SET @output = 'Expressmaint utility, Logged on to SQL Server [' + @@SERVERNAME + '] as ' + '[' + @user + ']'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output       &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IF UPPER(@optype) NOT IN ('REINDEX','CHECKDB','REORG')&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @output = 'Starting backup on ' + convert(varchar(25),getdate(),100)&lt;br /&gt;
END&lt;br /&gt;
IF UPPER(@optype) = 'CHECKDB'&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @output = 'Starting CheckDB on ' + convert(varchar(25),getdate(),100)&lt;br /&gt;
END      &lt;br /&gt;
IF UPPER(@optype) IN ('REINDEX','REORG')&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @output = 'Starting Reindex on ' + convert(varchar(25),getdate(),100)&lt;br /&gt;
END &lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
BACKUP ACTIONS&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
IF  UPPER(@optype) = 'CHECKDB' GOTO CHECK_DB&lt;br /&gt;
IF  UPPER(@optype) IN ('REINDEX','REORG') GOTO REINDEX&lt;br /&gt;
&lt;br /&gt;
-- if @delfirst = 1  we need to delete prior backups that qualify&lt;br /&gt;
IF @delfirst = 1 GOTO DELFIRST&lt;br /&gt;
&lt;br /&gt;
-- this label is so that we can return here after deleting files if @delfirst = 1&lt;br /&gt;
DOBACKUP:&lt;br /&gt;
&lt;br /&gt;
DECLARE dcur CURSOR LOCAL FAST_FORWARD&lt;br /&gt;
FOR SELECT dbname FROM #databases ORDER BY dbname&lt;br /&gt;
OPEN dcur&lt;br /&gt;
FETCH NEXT FROM dcur into @database&lt;br /&gt;
WHILE @@FETCH_STATUS=0&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
-- set backup start time&lt;br /&gt;
SET @start = GETDATE()&lt;br /&gt;
&lt;br /&gt;
-- write to text report&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @output = '[' + CAST(@stage as varchar(10)) + '] Database ' + @database + ': ' +&lt;br /&gt;
CASE WHEN UPPER(@optype) = 'DB'   THEN 'Full Backup '&lt;br /&gt;
WHEN UPPER(@optype) = 'DIFF' THEN 'Differential Backup '&lt;br /&gt;
WHEN UPPER(@optype) = 'LOG'  THEN 'Log Backup '         &lt;br /&gt;
END + 'starting at ' + CONVERT(varchar(25),@start,100)&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- backup subfolder&lt;br /&gt;
SET @execmd = 'IF NOT EXIST "' + @backupfldrorig + @database + '\" MKDIR "' + @backupfldrorig + @database + '\"'&lt;br /&gt;
EXEC master.dbo.xp_cmdshell @execmd,no_output&lt;br /&gt;
SET @backupfldr = @backupfldrorig + @database + '\'&lt;br /&gt;
&lt;br /&gt;
SELECT @backupfilename = @backupfldr + REPLACE(@database,' ','_') +&lt;br /&gt;
CASE WHEN UPPER(@optype) = 'DB'   THEN '_FullDBBackup_'&lt;br /&gt;
WHEN UPPER(@optype) = 'DIFF' THEN '_DiffDBBackup_'&lt;br /&gt;
WHEN UPPER(@optype) = 'LOG'  THEN '_LogBackup_'         &lt;br /&gt;
END + @jobstart + &lt;br /&gt;
CASE WHEN UPPER(@optype) = 'LOG' THEN '.TRN' ELSE '.BAK' END&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
FULL BACKUP&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
IF UPPER(@optype) = 'DB'&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
SET @execmd = N'BACKUP DATABASE [' + @database + '] TO DISK = ''' + @backupfilename + '''' +&lt;br /&gt;
CASE WHEN @backupwith IS NULL THEN '' ELSE (' WITH ' + @backupwith) END&lt;br /&gt;
&lt;br /&gt;
BEGIN TRY&lt;br /&gt;
&lt;br /&gt;
EXEC(@execmd)&lt;br /&gt;
&lt;br /&gt;
END TRY&lt;br /&gt;
BEGIN CATCH -- backup failure&lt;br /&gt;
SELECT @err = @@ERROR,@ret = @err&lt;br /&gt;
SELECT @errormsg = 'Full backup of database ' + @database + ' failed with error : ' +  CAST(@err as varchar(10))&lt;br /&gt;
SET @output = SPACE(4) + '*** ' + @errormsg + ' ***'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
SET @output = SPACE(4) + 'Refer to SQL Error Log and NT Event Log for further details'&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END&lt;br /&gt;
CLOSE dcur&lt;br /&gt;
DEALLOCATE dcur&lt;br /&gt;
GOTO CLEANUP&lt;br /&gt;
&lt;br /&gt;
END CATCH &lt;br /&gt;
&lt;br /&gt;
-- backup success&lt;br /&gt;
SET @finish = GETDATE()&lt;br /&gt;
SET @output = SPACE(4) + 'Database backed up to ' + @backupfilename&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
--calculate backup runtime&lt;br /&gt;
SET @runtime = (@finish - @start)&lt;br /&gt;
SET @output = SPACE(4) + 'Full database backup completed in '&lt;br /&gt;
+ CAST(DATEPART(hh,@runtime) as varchar(2)) + ' hour(s) '&lt;br /&gt;
+ CAST(DATEPART(mi,@runtime) as varchar(2)) + ' min(s) '&lt;br /&gt;
+ CAST(DATEPART(ss,@runtime) as varchar(2)) + ' second(s)'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
DIFFERENTIAL BACKUP&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
IF UPPER(@optype) = 'DIFF'&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
SET @execmd = N'BACKUP DATABASE [' + @database + '] TO DISK = ''' + &lt;br /&gt;
@backupfilename + ''' WITH DIFFERENTIAL' +&lt;br /&gt;
CASE WHEN @backupwith IS NULL THEN '' ELSE (' , ' + @backupwith) END&lt;br /&gt;
&lt;br /&gt;
BEGIN TRY&lt;br /&gt;
&lt;br /&gt;
EXEC(@execmd)&lt;br /&gt;
&lt;br /&gt;
END TRY&lt;br /&gt;
BEGIN CATCH -- backup failure&lt;br /&gt;
&lt;br /&gt;
SELECT @err = @@ERROR,@ret = @err&lt;br /&gt;
SELECT @errormsg = 'Differential backup of database ' + @database + ' failed with error : ' +  CAST(@err as varchar(10))&lt;br /&gt;
SET @output = SPACE(4) + '*** ' + @errormsg + ' ***'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
SET @output = SPACE(4) + 'Refer to SQL Error Log and NT Event Log for further details'&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
CLOSE dcur&lt;br /&gt;
DEALLOCATE dcur&lt;br /&gt;
GOTO CLEANUP&lt;br /&gt;
&lt;br /&gt;
END CATCH &lt;br /&gt;
&lt;br /&gt;
-- backup success&lt;br /&gt;
SET @finish = GETDATE()&lt;br /&gt;
SET @output = SPACE(4) + 'Database backed up to ' + @backupfilename&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
--calculate backup runtime&lt;br /&gt;
SET @runtime = (@finish - @start)&lt;br /&gt;
SET @output = SPACE(4) + 'Differential database backup completed in '&lt;br /&gt;
+ CAST(DATEPART(hh,@runtime) as varchar(2)) + ' hour(s) '&lt;br /&gt;
+ CAST(DATEPART(mi,@runtime) as varchar(2)) + ' min(s) '&lt;br /&gt;
+ CAST(DATEPART(ss,@runtime) as varchar(2)) + ' second(s)'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
LOG BACKUP&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
IF UPPER(@optype) = 'LOG'&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
SET @execmd = N'BACKUP LOG [' + @database + '] TO DISK = ''' + @backupfilename + '''' +&lt;br /&gt;
CASE WHEN @backupwith IS NULL THEN '' ELSE (' WITH ' + @backupwith) END&lt;br /&gt;
&lt;br /&gt;
BEGIN TRY&lt;br /&gt;
&lt;br /&gt;
EXEC(@execmd)&lt;br /&gt;
&lt;br /&gt;
END TRY&lt;br /&gt;
BEGIN CATCH -- backup failure&lt;br /&gt;
&lt;br /&gt;
SELECT @err = @@ERROR,@ret = @err&lt;br /&gt;
SELECT @errormsg = 'Log backup of database ' + @database + ' failed with error : ' +  CAST(@err as varchar(10))        &lt;br /&gt;
SET @output = SPACE(4) + '*** ' + @errormsg + ' ***'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
SET @output = SPACE(4) + 'Refer to SQL Error Log and NT Event Log for further details'&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
CLOSE dcur&lt;br /&gt;
DEALLOCATE dcur&lt;br /&gt;
GOTO CLEANUP&lt;br /&gt;
&lt;br /&gt;
END CATCH &lt;br /&gt;
&lt;br /&gt;
-- backup success&lt;br /&gt;
SET @finish = GETDATE()&lt;br /&gt;
SET @output = SPACE(4) + 'Log backed up to ' + @backupfilename&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
--calculate backup runtime&lt;br /&gt;
SET @runtime = (@finish - @start)&lt;br /&gt;
SET @output = SPACE(4) + 'Log backup completed in '&lt;br /&gt;
+ CAST(DATEPART(hh,@runtime) as varchar(2)) + ' hour(s) '&lt;br /&gt;
+ CAST(DATEPART(mi,@runtime) as varchar(2)) + ' min(s) '&lt;br /&gt;
+ CAST(DATEPART(ss,@runtime) as varchar(2)) + ' second(s)'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
SET @stage = (@stage + 1)&lt;br /&gt;
&lt;br /&gt;
FETCH NEXT FROM dcur into @database&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
CLOSE dcur&lt;br /&gt;
DEALLOCATE dcur&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
VERIFY BACKUP&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
IF @verify = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
DECLARE dcur CURSOR LOCAL FAST_FORWARD&lt;br /&gt;
FOR SELECT dbname FROM #databases ORDER BY dbname&lt;br /&gt;
OPEN dcur&lt;br /&gt;
FETCH NEXT FROM dcur into @database&lt;br /&gt;
WHILE @@FETCH_STATUS=0&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
SELECT @backupfilename = @backupfldrorig + @database + '\' + REPLACE(@database,' ','_') +&lt;br /&gt;
CASE WHEN UPPER(@optype) = 'DB'   THEN '_FullDBBackup_'&lt;br /&gt;
WHEN UPPER(@optype) = 'DIFF' THEN '_DiffDBBackup_'&lt;br /&gt;
WHEN UPPER(@optype) = 'LOG'  THEN '_LogBackup_'         &lt;br /&gt;
END + @jobstart + &lt;br /&gt;
CASE WHEN UPPER(@optype) = 'LOG' THEN '.TRN' ELSE '.BAK' END&lt;br /&gt;
&lt;br /&gt;
SET @start = GETDATE()&lt;br /&gt;
&lt;br /&gt;
-- write to text report&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
SET @output = '[' + CAST(@stage as varchar(10)) + '] Database ' + @database + ': Verify Backup File...'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output  &lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
SET @execmd = N'RESTORE VERIFYONLY FROM DISK = ''' + @backupfilename + '''' +&lt;br /&gt;
CASE WHEN @verifywith IS NULL THEN '' ELSE (' WITH ' + @verifywith) END&lt;br /&gt;
&lt;br /&gt;
BEGIN TRY&lt;br /&gt;
&lt;br /&gt;
EXEC(@execmd)&lt;br /&gt;
&lt;br /&gt;
END TRY&lt;br /&gt;
BEGIN CATCH&lt;br /&gt;
&lt;br /&gt;
SELECT @err = @@ERROR,@ret = @err&lt;br /&gt;
SET @errormsg = 'Verify of ' + @backupfilename + ' failed with Native Error : ' + CAST(@err as varchar(10))&lt;br /&gt;
SET @output = SPACE(4) + '*** ' + @errormsg + ' ***'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
CLOSE dcur&lt;br /&gt;
DEALLOCATE dcur&lt;br /&gt;
GOTO CLEANUP&lt;br /&gt;
&lt;br /&gt;
END CATCH&lt;br /&gt;
&lt;br /&gt;
-- verify success&lt;br /&gt;
SET @finish = GETDATE()&lt;br /&gt;
SET @output = SPACE(4) + 'Backup file ' + @backupfilename + ' verified'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
--calculate verify runtime&lt;br /&gt;
SET @runtime = (@finish - @start)&lt;br /&gt;
SET @output = SPACE(4) + 'Verify backup completed in '&lt;br /&gt;
+ CAST(DATEPART(hh,@runtime) as varchar(2)) + ' hour(s) '&lt;br /&gt;
+ CAST(DATEPART(mi,@runtime) as varchar(2)) + ' min(s) '&lt;br /&gt;
+ CAST(DATEPART(ss,@runtime) as varchar(2)) + ' second(s)'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
SET @stage = (@stage + 1)&lt;br /&gt;
FETCH NEXT FROM dcur into @database &lt;br /&gt;
END     &lt;br /&gt;
&lt;br /&gt;
CLOSE dcur&lt;br /&gt;
DEALLOCATE dcur&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
DELETE OLD FILES&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-- we have already deleted files so skip to the end&lt;br /&gt;
IF @delfirst = 1 GOTO CLEANUP&lt;br /&gt;
&lt;br /&gt;
-- this label is so that we can delete files prior to backup if @delfirst = 1&lt;br /&gt;
DELFIRST:&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
DELETE OLD BACKUPS&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
SET @datepart = CASE &lt;br /&gt;
WHEN UPPER(@dbretainunit) = 'MINUTES' THEN N'mi'&lt;br /&gt;
WHEN UPPER(@dbretainunit) = 'HOURS'   THEN N'hh'&lt;br /&gt;
WHEN UPPER(@dbretainunit) = 'DAYS'    THEN N'dd'&lt;br /&gt;
WHEN UPPER(@dbretainunit) = 'WEEKS'   THEN N'ww'&lt;br /&gt;
WHEN UPPER(@dbretainunit) = 'MONTHS'  THEN N'yy'&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT '@datepart for backups = ' + @datepart&lt;br /&gt;
&lt;br /&gt;
-- write to text report&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END&lt;br /&gt;
SET @output = '[' + CAST(@stage as varchar(10)) + '] Delete Old Backup Files...'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
DECLARE dcur CURSOR LOCAL FAST_FORWARD&lt;br /&gt;
FOR SELECT dbname FROM #databases ORDER BY dbname&lt;br /&gt;
OPEN dcur&lt;br /&gt;
FETCH NEXT FROM dcur into @database&lt;br /&gt;
WHILE @@FETCH_STATUS=0&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
SET @backupfldr = + @backupfldrorig + @database + '\'&lt;br /&gt;
SELECT @backupfilename = @backupfldr + REPLACE(@database,' ','_') +&lt;br /&gt;
CASE WHEN UPPER(@optype) = 'DB'   THEN '_FullDBBackup_'&lt;br /&gt;
WHEN UPPER(@optype) = 'DIFF' THEN '_DiffDBBackup_'&lt;br /&gt;
WHEN UPPER(@optype) = 'LOG'  THEN '_LogBackup_'         &lt;br /&gt;
END + @jobstart + &lt;br /&gt;
CASE WHEN UPPER(@optype) = 'LOG' THEN '.TRN' ELSE '.BAK' END&lt;br /&gt;
&lt;br /&gt;
-- load files in @backupfldr&lt;br /&gt;
IF @checkattrib = 1&lt;br /&gt;
SET @cmd = 'dir /B /A-D-A /OD "' + @backupfldr + REPLACE(@database,' ','_') +&lt;br /&gt;
CASE WHEN UPPER(@optype) = 'DB' THEN '_FullDBBackup_'&lt;br /&gt;
WHEN UPPER(@optype) = 'DIFF' THEN '_DiffDBBackup_'&lt;br /&gt;
WHEN UPPER(@optype) = 'LOG'  THEN '_LogBackup_' END + '*' +&lt;br /&gt;
CASE WHEN UPPER(@optype) = 'LOG' THEN '.TRN' ELSE '.BAK' END + '"'&lt;br /&gt;
ELSE &lt;br /&gt;
SET @cmd = 'dir /B /A-D /OD "' + @backupfldr + REPLACE(@database,' ','_') +&lt;br /&gt;
CASE WHEN UPPER(@optype) = 'DB' THEN '_FullDBBackup_'&lt;br /&gt;
WHEN UPPER(@optype) = 'DIFF' THEN '_DiffDBBackup_'&lt;br /&gt;
WHEN UPPER(@optype) = 'LOG'  THEN '_LogBackup_' END + '*' +&lt;br /&gt;
CASE WHEN UPPER(@optype) = 'LOG' THEN '.TRN' ELSE '.BAK' END + '"'&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT '@cmd = ' + @cmd&lt;br /&gt;
&lt;br /&gt;
DELETE #files&lt;br /&gt;
INSERT #files EXEC master.dbo.xp_cmdshell @cmd&lt;br /&gt;
DELETE #files WHERE filename IS NULL or filename = ISNULL(REPLACE(@backupfilename,@backupfldr,''),'nothing')&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 SELECT * FROM #files&lt;br /&gt;
&lt;br /&gt;
-- get count of files that match pattern&lt;br /&gt;
SELECT @filecount = COUNT(*) from #files &lt;br /&gt;
WHERE PATINDEX('%File Not Found%',filename) = 0&lt;br /&gt;
AND PATINDEX('%The system cannot find%',filename) = 0 &lt;br /&gt;
&lt;br /&gt;
-- remove files that don't meet retention criteria if there are any files that match pattern&lt;br /&gt;
IF UPPER(@dbretainunit) &amp;lt;&amp;gt; 'COPIES'&lt;br /&gt;
BEGIN&lt;br /&gt;
IF @filecount&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @delcmd = N'DELETE #files WHERE DATEADD(' + @datepart + N',' + CAST(@dbretainval as nvarchar(10)) + N',' +&lt;br /&gt;
'CONVERT(datetime,(SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(''_'',REVERSE(filename)))+2),12),7,2) +''/''&lt;br /&gt;
+ SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(''_'',REVERSE(filename)))+2),12),5,2) +''/''&lt;br /&gt;
+ SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(''_'',REVERSE(filename)))+2),12),1,4) +'' ''&lt;br /&gt;
+ SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(''_'',REVERSE(filename)))+2),12),9,2) +'':''&lt;br /&gt;
+ SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(''_'',REVERSE(filename)))+2),12),11,2)),103)) &amp;gt; ''' + CAST(@jobdt as nvarchar(25)) + N''''&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT '@delcmd=' + @delcmd&lt;br /&gt;
EXEC master.dbo.sp_executesql @delcmd&lt;br /&gt;
&lt;br /&gt;
SELECT @delcount = COUNT(*) from #files&lt;br /&gt;
END&lt;br /&gt;
ELSE&lt;br /&gt;
BEGIN&lt;br /&gt;
SELECT @delcount = 0&lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
ELSE  -- number of copies not date based (include current backup that's not in #files)&lt;br /&gt;
BEGIN&lt;br /&gt;
IF @filecount&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
IF @dbretainval&amp;gt;1 &lt;br /&gt;
BEGIN&lt;br /&gt;
SET @delcmd = N'DELETE #files WHERE filename IN(SELECT TOP ' + CAST((@dbretainval-1) as nvarchar(10)) +&lt;br /&gt;
N' filename FROM #files ORDER BY substring(filename,((len(filename)+2)-charindex(''_'',reverse(filename))),12) DESC)'&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT '@delcmd=' + @delcmd&lt;br /&gt;
EXEC master.dbo.sp_executesql @delcmd&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
SELECT @delcount = COUNT(*) from #files&lt;br /&gt;
&lt;br /&gt;
END&lt;br /&gt;
ELSE&lt;br /&gt;
BEGIN&lt;br /&gt;
SELECT @delcount = 0&lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT '@delcount = ' + STR(@delcount)&lt;br /&gt;
&lt;br /&gt;
-- if there are any matching files&lt;br /&gt;
IF @filecount&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
-- are there any files that need deleting&lt;br /&gt;
IF @delcount&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
DECLARE FCUR CURSOR FORWARD_ONLY FOR&lt;br /&gt;
SELECT * FROM #files&lt;br /&gt;
OPEN FCUR&lt;br /&gt;
FETCH NEXT FROM FCUR INTO @delfilename&lt;br /&gt;
WHILE @@FETCH_STATUS=0&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @cmd = 'DEL /Q "' + @backupfldr + @delfilename + '"'&lt;br /&gt;
EXEC @cmdret = master.dbo.xp_cmdshell @cmd,no_output   &lt;br /&gt;
&lt;br /&gt;
-- log failure to delete but don't abort procedure&lt;br /&gt;
IF @cmdret&amp;lt;&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @output = SPACE(4) + '*** Error: Failed to delete file ' + @backupfldr + @delfilename + ' ***'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
SELECT @delbkflag = 1 , @cmdret = 0, @delcount = (@delcount-1)&lt;br /&gt;
END&lt;br /&gt;
ELSE&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @output = SPACE(4) + 'Deleted file ' + @backupfldr + @delfilename&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
FETCH NEXT FROM FCUR INTO @delfilename&lt;br /&gt;
END&lt;br /&gt;
CLOSE FCUR&lt;br /&gt;
DEALLOCATE FCUR&lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- write to text report&lt;br /&gt;
SET @output = SPACE(4) + CAST(@delcount as varchar(10)) + ' file(s) deleted.'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
FETCH NEXT FROM dcur into @database&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
CLOSE dcur&lt;br /&gt;
DEALLOCATE dcur&lt;br /&gt;
&lt;br /&gt;
-- clear temporary table and variables&lt;br /&gt;
DELETE #files&lt;br /&gt;
SET @cmd = ''&lt;br /&gt;
SET @delcmd = ''&lt;br /&gt;
SET @delfilename = ''&lt;br /&gt;
SET @datepart = ''&lt;br /&gt;
SET @filecount = 0&lt;br /&gt;
SET @delcount = 0&lt;br /&gt;
SET @cmdret = 0&lt;br /&gt;
SET @stage = @stage + 1&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
DELETE OLD REPORTS&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
DELREPORTS:&lt;br /&gt;
&lt;br /&gt;
IF @rptretainunit IS NOT NULL&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @datepart = CASE &lt;br /&gt;
WHEN UPPER(@rptretainunit) = 'MINUTES' THEN N'mi'&lt;br /&gt;
WHEN UPPER(@rptretainunit) = 'HOURS'   THEN N'hh'&lt;br /&gt;
WHEN UPPER(@rptretainunit) = 'DAYS'    THEN N'dd'&lt;br /&gt;
WHEN UPPER(@rptretainunit) = 'WEEKS'   THEN N'ww'&lt;br /&gt;
WHEN UPPER(@rptretainunit) = 'MONTHS'  THEN N'yy'&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT '@datepart for reports = ' + @datepart&lt;br /&gt;
&lt;br /&gt;
-- write to text report&lt;br /&gt;
SET @output = '[' + CAST(@stage as varchar(10)) + '] Delete Old Report Files...'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- load files in @reportfldr&lt;br /&gt;
SET @cmd = 'dir /B /A-D /OD "' + @reportfldr + REPLACE(@databaseorig,' ','_') +&lt;br /&gt;
CASE WHEN UPPER(@optype) = 'DB' THEN '_FullDBBackup_report_'&lt;br /&gt;
WHEN UPPER(@optype) = 'DIFF' THEN '_DiffDBBackup_report_'&lt;br /&gt;
WHEN UPPER(@optype) = 'REINDEX'  THEN '_Reindex_report_'     &lt;br /&gt;
WHEN UPPER(@optype) = 'CHECKDB'  THEN '_CheckDB_report_'     &lt;br /&gt;
WHEN UPPER(@optype) = 'REORG'  THEN '_Reorg_report_' &lt;br /&gt;
WHEN UPPER(@optype) = 'LOG'  THEN '_LogBackup_report_' END + '*.txt"'&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT '@cmd = ' + @cmd&lt;br /&gt;
&lt;br /&gt;
INSERT #files EXEC master.dbo.xp_cmdshell @cmd&lt;br /&gt;
DELETE #files WHERE filename IS NULL&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 SELECT * FROM #files&lt;br /&gt;
&lt;br /&gt;
-- get count of files that match pattern&lt;br /&gt;
SELECT @filecount = COUNT(*) from #files &lt;br /&gt;
WHERE PATINDEX('%File Not Found%',filename) = 0&lt;br /&gt;
AND PATINDEX('%The system cannot find%',filename) = 0 &lt;br /&gt;
&lt;br /&gt;
-- remove files that don't meet retention criteria if there are any files that match pattern&lt;br /&gt;
IF UPPER(@rptretainunit) &amp;lt;&amp;gt; 'COPIES'&lt;br /&gt;
BEGIN&lt;br /&gt;
IF @filecount&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @delcmd = N'DELETE #files WHERE DATEADD(' + @datepart + N',' + CAST(@rptretainval as nvarchar(10)) + N',' +&lt;br /&gt;
'CONVERT(datetime,(SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(''_'',REVERSE(filename)))+2),12),7,2) +''/''&lt;br /&gt;
+ SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(''_'',REVERSE(filename)))+2),12),5,2) +''/''&lt;br /&gt;
+ SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(''_'',REVERSE(filename)))+2),12),1,4) +'' ''&lt;br /&gt;
+ SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(''_'',REVERSE(filename)))+2),12),9,2) +'':''&lt;br /&gt;
+ SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(''_'',REVERSE(filename)))+2),12),11,2)),103)) &amp;gt; ''' + CAST(@jobdt as nvarchar(25)) + N''''&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT '@delcmd=' + @delcmd&lt;br /&gt;
EXEC master.dbo.sp_executesql @delcmd&lt;br /&gt;
&lt;br /&gt;
SELECT @delcount = COUNT(*) from #files&lt;br /&gt;
END&lt;br /&gt;
ELSE&lt;br /&gt;
BEGIN&lt;br /&gt;
SELECT @delcount = 0&lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
ELSE  -- number of copies not date based&lt;br /&gt;
BEGIN&lt;br /&gt;
IF @filecount&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @delcmd = N'DELETE #files WHERE filename IN(SELECT TOP ' + CAST(@rptretainval as nvarchar(10)) +&lt;br /&gt;
N' filename FROM #files ORDER BY substring(filename,((len(filename)+2)-charindex(''_'',reverse(filename))),12) DESC)'&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT '@delcmd=' + @delcmd&lt;br /&gt;
EXEC master.dbo.sp_executesql @delcmd&lt;br /&gt;
&lt;br /&gt;
SELECT @delcount = COUNT(*) from #files&lt;br /&gt;
END&lt;br /&gt;
ELSE&lt;br /&gt;
BEGIN&lt;br /&gt;
SELECT @delcount = 0&lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT STR(@delcount)&lt;br /&gt;
&lt;br /&gt;
-- if there are any matching files&lt;br /&gt;
IF @filecount&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
-- are there any files that need deleting&lt;br /&gt;
IF @delcount&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
DECLARE FCUR CURSOR FORWARD_ONLY FOR&lt;br /&gt;
SELECT * FROM #files&lt;br /&gt;
OPEN FCUR&lt;br /&gt;
FETCH NEXT FROM FCUR INTO @delfilename&lt;br /&gt;
WHILE @@FETCH_STATUS=0&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @cmd = 'DEL /Q "' + @reportfldr + @delfilename + '"'&lt;br /&gt;
EXEC @cmdret = master.dbo.xp_cmdshell @cmd,no_output   &lt;br /&gt;
&lt;br /&gt;
-- log failure to delete but don't abort procedure&lt;br /&gt;
IF @cmdret&amp;lt;&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
SET @output = SPACE(4) + '*** Error: Failed to delete file ' + @reportfldr + @delfilename + ' ***'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
SELECT @delrptflag = 1 , @cmdret = 0, @delcount = (@delcount-1)&lt;br /&gt;
END&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @output = SPACE(4) + 'Deleted file ' + @reportfldr + @delfilename&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
FETCH NEXT FROM FCUR INTO @delfilename&lt;br /&gt;
END&lt;br /&gt;
CLOSE FCUR&lt;br /&gt;
DEALLOCATE FCUR&lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- write to text report&lt;br /&gt;
SET @output = SPACE(4) + CAST(@delcount as varchar(10)) + ' file(s) deleted.'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- update stage&lt;br /&gt;
SET @stage = @stage + 1&lt;br /&gt;
END&lt;br /&gt;
-- if we got here due to @delfirst = 1 go back and do the backups&lt;br /&gt;
IF @delfirst = 1 &lt;br /&gt;
GOTO DOBACKUP&lt;br /&gt;
ELSE &lt;br /&gt;
GOTO CLEANUP&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
CHECKDB &lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
CHECK_DB:&lt;br /&gt;
&lt;br /&gt;
IF @optype = 'CHECKDB'&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
DECLARE dcur CURSOR LOCAL FAST_FORWARD&lt;br /&gt;
FOR SELECT dbname FROM #databases ORDER BY dbname&lt;br /&gt;
OPEN dcur&lt;br /&gt;
FETCH NEXT FROM dcur into @database&lt;br /&gt;
WHILE @@FETCH_STATUS=0&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
-- write to text report&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
SET @output = '[' + CAST(@stage as varchar(10)) + '] Database ' + @database + ': Check Data and Index Linkage...'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output  &lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- set backup start time&lt;br /&gt;
SET @start = GETDATE()&lt;br /&gt;
&lt;br /&gt;
SET @execmd = N'DBCC CHECKDB([' + @database + N']) WITH NO_INFOMSGS'&lt;br /&gt;
IF @debug = 1 PRINT 'DBCC Command : ' + @execmd&lt;br /&gt;
&lt;br /&gt;
BEGIN TRY&lt;br /&gt;
&lt;br /&gt;
EXEC(@execmd)&lt;br /&gt;
&lt;br /&gt;
END TRY&lt;br /&gt;
BEGIN CATCH&lt;br /&gt;
&lt;br /&gt;
SELECT @err = @@ERROR,@ret = @err&lt;br /&gt;
SET @errormsg = 'CheckDB of ' + @database + ' failed with Native Error : ' + CAST(@err as varchar(10))&lt;br /&gt;
SET @output = SPACE(4) + '*** ' + @errormsg + ' ***'&lt;br /&gt;
PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
CLOSE dcur&lt;br /&gt;
DEALLOCATE dcur&lt;br /&gt;
GOTO CLEANUP&lt;br /&gt;
&lt;br /&gt;
END CATCH&lt;br /&gt;
&lt;br /&gt;
SET @finish = GETDATE()&lt;br /&gt;
&lt;br /&gt;
--calculate checkdb runtime&lt;br /&gt;
SET @runtime = (@finish - @start)&lt;br /&gt;
SET @output = SPACE(4) + 'CheckDB completed in '&lt;br /&gt;
+ CAST(DATEPART(hh,@runtime) as varchar(2)) + ' hour(s) '&lt;br /&gt;
+ CAST(DATEPART(mi,@runtime) as varchar(2)) + ' min(s) '&lt;br /&gt;
+ CAST(DATEPART(ss,@runtime) as varchar(2)) + ' second(s)'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END   &lt;br /&gt;
SET @stage = (@stage + 1)&lt;br /&gt;
FETCH NEXT FROM dcur into @database      &lt;br /&gt;
&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
CLOSE dcur&lt;br /&gt;
DEALLOCATE dcur&lt;br /&gt;
&lt;br /&gt;
-- delete reports&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END&lt;br /&gt;
GOTO DELREPORTS&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
REINDEX/REORG&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
REINDEX:&lt;br /&gt;
&lt;br /&gt;
IF @optype in ('REINDEX','REORG')&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
DECLARE dcur CURSOR LOCAL FAST_FORWARD&lt;br /&gt;
FOR SELECT dbname FROM #databases ORDER BY dbname&lt;br /&gt;
OPEN dcur&lt;br /&gt;
FETCH NEXT FROM dcur into @database&lt;br /&gt;
WHILE @@FETCH_STATUS=0&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
-- write to text report&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
IF @optype = 'REINDEX'&lt;br /&gt;
SET @output = '[' + CAST(@stage as varchar(10)) + '] Database ' + @database + ': Index Rebuild (using original fillfactor)...'&lt;br /&gt;
ELSE&lt;br /&gt;
SET @output = '[' + CAST(@stage as varchar(10)) + '] Database ' + @database + ': Index Reorganize...'&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output  &lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''  &lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- set start time&lt;br /&gt;
SET @start = GETDATE()&lt;br /&gt;
&lt;br /&gt;
-- all user tables&lt;br /&gt;
CREATE TABLE #tables(tablename sysname)&lt;br /&gt;
EXEC(N'INSERT #tables(tablename) SELECT DISTINCT(''['' + s.[name] + ''].['' + t.[name] + '']'') FROM [' + @database + N'].sys.tables t ' +&lt;br /&gt;
N'JOIN [' + @database + N'].sys.schemas s on t.schema_id=s.schema_id ' +&lt;br /&gt;
N'JOIN [' + @database + N'].sys.indexes i on t.object_id=i.object_id ' +&lt;br /&gt;
N'WHERE t.is_ms_shipped = 0 AND i.type&amp;gt;0')&lt;br /&gt;
&lt;br /&gt;
DECLARE tcur CURSOR LOCAL FAST_FORWARD &lt;br /&gt;
FOR SELECT tablename FROM #tables ORDER BY tablename&lt;br /&gt;
OPEN tcur&lt;br /&gt;
FETCH NEXT FROM tcur INTO @table&lt;br /&gt;
WHILE @@FETCH_STATUS = 0&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
IF @optype = 'REINDEX'&lt;br /&gt;
SET @output = SPACE(4) + N'Rebuilding indexes for table ' + @table&lt;br /&gt;
ELSE&lt;br /&gt;
SET @output = SPACE(4) + N'Reorganizing indexes for table ' + @table&lt;br /&gt;
&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IF @optype = 'REINDEX'&lt;br /&gt;
SET @execmd = N'ALTER INDEX ALL ON [' + @database + N'].' + @table + N' REBUILD'&lt;br /&gt;
ELSE&lt;br /&gt;
SET @execmd = N'ALTER INDEX ALL ON [' + @database + N'].' + @table + N' REORGANIZE'&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT 'Reindex Command : ' + @execmd&lt;br /&gt;
&lt;br /&gt;
BEGIN TRY&lt;br /&gt;
&lt;br /&gt;
EXEC(@execmd)&lt;br /&gt;
&lt;br /&gt;
END TRY&lt;br /&gt;
BEGIN CATCH&lt;br /&gt;
&lt;br /&gt;
SELECT @err = @@ERROR,@ret = @err&lt;br /&gt;
SET @errormsg = 'Rebuild of indexes on [' + @database + N'].' + @table + ' failed with Native Error : ' + CAST(@err as varchar(10))&lt;br /&gt;
SET @output = SPACE(4) + '*** ' + @errormsg + ' ***'&lt;br /&gt;
PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
CLOSE tcur&lt;br /&gt;
DEALLOCATE tcur&lt;br /&gt;
DROP TABLE #tables&lt;br /&gt;
GOTO CLEANUP&lt;br /&gt;
&lt;br /&gt;
END CATCH&lt;br /&gt;
&lt;br /&gt;
FETCH NEXT FROM tcur INTO @table&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
CLOSE tcur&lt;br /&gt;
DEALLOCATE tcur&lt;br /&gt;
&lt;br /&gt;
SET @finish = GETDATE()&lt;br /&gt;
&lt;br /&gt;
--calculate runtime&lt;br /&gt;
SET @runtime = (@finish - @start)&lt;br /&gt;
SET @output = SPACE(4) + 'Index maintenance completed in '&lt;br /&gt;
+ CAST(DATEPART(hh,@runtime) as varchar(2)) + ' hour(s) '&lt;br /&gt;
+ CAST(DATEPART(mi,@runtime) as varchar(2)) + ' min(s) '&lt;br /&gt;
+ CAST(DATEPART(ss,@runtime) as varchar(2)) + ' second(s)'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END   &lt;br /&gt;
&lt;br /&gt;
DROP TABLE #tables&lt;br /&gt;
&lt;br /&gt;
SET @stage = (@stage + 1)&lt;br /&gt;
FETCH NEXT FROM dcur into @database   &lt;br /&gt;
&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
CLOSE dcur&lt;br /&gt;
DEALLOCATE dcur&lt;br /&gt;
&lt;br /&gt;
-- delete reports&lt;br /&gt;
GOTO DELREPORTS&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
CLEAN UP &lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
CLEANUP:&lt;br /&gt;
&lt;br /&gt;
DROP TABLE #files&lt;br /&gt;
DROP TABLE #exists&lt;br /&gt;
DROP TABLE #databases&lt;br /&gt;
&lt;br /&gt;
-- if we encountered errors deleting old backups return failure&lt;br /&gt;
IF @delbkflag&amp;lt;&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @errormsg = 'Expressmaint encountered errors deleting old backup files' + CHAR(13)&lt;br /&gt;
+ CASE WHEN @report = 1 THEN ('Please see ' + @reportfilename + CHAR(13) + ' for further details') ELSE '' END&lt;br /&gt;
RAISERROR(@errormsg,16,1)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- if we encountered errors deleting old reports return failure&lt;br /&gt;
IF (@delrptflag&amp;lt;&amp;gt;0 AND @delbkflag = 0)&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @errormsg = 'Expressmaint encountered errors deleting old report files' + CHAR(13)&lt;br /&gt;
+ CASE WHEN @report = 1 THEN ('Please see ' + @reportfilename + CHAR(13) + ' for further details') ELSE '' END&lt;br /&gt;
RAISERROR(@errormsg,16,1)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- if we created a file make sure we write trailer and destroy object&lt;br /&gt;
IF @filecrt = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
-- write final part of report&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
SET @output = 'Expressmaint processing finished at ' + CONVERT(varchar(25),GETDATE(),100) &lt;br /&gt;
+ ' (Return Code : ' + CAST(@ret as varchar(10)) + ')' &lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
&lt;br /&gt;
-- destroy file object&lt;br /&gt;
EXEC @hr=sp_OADestroy @file&lt;br /&gt;
IF @hr &amp;lt;&amp;gt; 0 EXEC sp_OAGetErrorInfo @file&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC @hr=sp_OADestroy @fso&lt;br /&gt;
IF @hr &amp;lt;&amp;gt; 0 EXEC sp_OAGetErrorInfo @fso&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
RETURN @ret&lt;br /&gt;
GO&lt;br /&gt;
use master&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[expressmaint]') &lt;br /&gt;
and OBJECTPROPERTY(id, N'IsProcedure') = 1)&lt;br /&gt;
drop procedure [dbo].[expressmaint]&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
CREATE PROCEDURE expressmaint&lt;br /&gt;
(&lt;br /&gt;
@database      sysname,                   -- database name | ALL_USER | ALL_SYSTEM&lt;br /&gt;
@optype        varchar(7),                -- LOG | DB | DIFF | REINDEX | REORG | CHECKDB&lt;br /&gt;
@backupwith    varchar(500) = NULL,       -- additional backup options&lt;br /&gt;
@backupfldr    varchar(200) = NULL,       -- folder to write backup to &lt;br /&gt;
@reportfldr    varchar(200) = NULL,       -- folder to write text report &lt;br /&gt;
@verify        bit = 1,                   -- verify backup&lt;br /&gt;
@verifywith    varchar(500) = NULL,       -- additional verify options&lt;br /&gt;
@dbretainunit  varchar(10)  = NULL,       -- minutes | hours | days | weeks | months | copies&lt;br /&gt;
@dbretainval   int = 1,                   -- specifies how many retainunits to keep backup&lt;br /&gt;
@report        bit = 1,                   -- flag to indicate whether to generate report&lt;br /&gt;
@rptretainunit varchar(10)  = NULL,       -- minutes | hours | days | weeks | months | copies&lt;br /&gt;
@rptretainval  int = 1,                   -- specifies how many retainunits to keep reports&lt;br /&gt;
@checkattrib   bit = 0,                   -- check if archive bit is cleared before deleting&lt;br /&gt;
@delfirst      bit = 0,                   -- delete before backup (handy if space issues)&lt;br /&gt;
@debug         bit = 0                    -- print commands to be executed&lt;br /&gt;
)&lt;br /&gt;
AS&lt;br /&gt;
&lt;br /&gt;
SET NOCOUNT ON&lt;br /&gt;
SET DATEFORMAT YMD&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
VARIABLE DECLARATION&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
DECLARE @fso             int &lt;br /&gt;
DECLARE @file            int &lt;br /&gt;
DECLARE @reportfilename  varchar(500) &lt;br /&gt;
DECLARE @backupfilename  varchar(500) &lt;br /&gt;
DECLARE @delfilename     varchar(500)&lt;br /&gt;
DECLARE @cmd             varchar(650)&lt;br /&gt;
DECLARE @backupfldrorig  varchar(200)&lt;br /&gt;
DECLARE @databaseorig    sysname&lt;br /&gt;
DECLARE @table           nvarchar(600)&lt;br /&gt;
DECLARE @exists          varchar(5)&lt;br /&gt;
DECLARE @err             int&lt;br /&gt;
DECLARE @start           datetime&lt;br /&gt;
DECLARE @finish          datetime&lt;br /&gt;
DECLARE @runtime         datetime&lt;br /&gt;
DECLARE @output          varchar(200)&lt;br /&gt;
DECLARE @errormsg        varchar(210)&lt;br /&gt;
DECLARE @datepart        nchar(2)&lt;br /&gt;
DECLARE @execmd          nvarchar(1000)&lt;br /&gt;
DECLARE @delcmd          nvarchar(1000)&lt;br /&gt;
DECLARE @exemsg          varchar(8000)&lt;br /&gt;
DECLARE @filecount       int              ; SET @filecount    = 0&lt;br /&gt;
DECLARE @delcount        int              ; SET @delcount     = 0&lt;br /&gt;
DECLARE @hr              int              ; SET @hr           = 0&lt;br /&gt;
DECLARE @ret             int              ; SET @ret          = 0&lt;br /&gt;
DECLARE @cmdret          int              ; SET @cmdret       = 0&lt;br /&gt;
DECLARE @delbkflag       int              ; SET @delbkflag    = 0&lt;br /&gt;
DECLARE @delrptflag      int              ; SET @delrptflag   = 0&lt;br /&gt;
DECLARE @filecrt         int              ; SET @filecrt      = 0&lt;br /&gt;
DECLARE @user            sysname          ; SET @user         = SUSER_SNAME()&lt;br /&gt;
DECLARE @jobdt           datetime         ; SET @jobdt        = GETDATE()&lt;br /&gt;
DECLARE @jobstart        char(12)         ; &lt;br /&gt;
DECLARE @stage           int              ; SET @stage        = 1&lt;br /&gt;
&lt;br /&gt;
SET @jobstart = CONVERT(char(8),@jobdt,112)+LEFT(REPLACE(CONVERT(char(8),@jobdt,108),':',''),4)   &lt;br /&gt;
IF RIGHT(@reportfldr,1)&amp;lt;&amp;gt;'\' SET @reportfldr = @reportfldr + '\'&lt;br /&gt;
IF RIGHT(@backupfldr,1)&amp;lt;&amp;gt;'\' SET @backupfldr = @backupfldr + '\'&lt;br /&gt;
SET @backupfldrorig = @backupfldr&lt;br /&gt;
SET @databaseorig = @database&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE #files(filename varchar(255))  &lt;br /&gt;
CREATE TABLE #exists(exist int,isdir int,parent int)&lt;br /&gt;
CREATE TABLE #databases(dbname sysname)&lt;br /&gt;
&lt;br /&gt;
/**********************************&lt;br /&gt;
INITIALIZE FSO IF @report = 1&lt;br /&gt;
***********************************/&lt;br /&gt;
&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC @hr=sp_OACreate 'Scripting.FileSystemObject',@fso OUT &lt;br /&gt;
IF @hr &amp;lt;&amp;gt; 0 &lt;br /&gt;
BEGIN   &lt;br /&gt;
EXEC sp_OAGetErrorInfo @fso&lt;br /&gt;
RAISERROR('Error creating File System Object',16,1)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
CHECK INPUT&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
-- check SQL2005 or higher&lt;br /&gt;
IF (select SUBSTRING(@@version,(CHARINDEX('-',@@version)+2),1))&amp;lt;9 BEGIN                        RAISERROR('SQL2005 or higher is required for sp_expressmaint',16,1) SET @ret = 1 GOTO CLEANUP  END  -- check sysadmin IF IS_SRVROLEMEMBER('sysadmin') = 0 BEGIN                        RAISERROR('The current user %s is not a member of the sysadmin role',16,1,@user) SET @ret = 1 GOTO CLEANUP  END  -- check database exists and is online IF @database NOT IN ('ALL_USER','ALL_SYSTEM') BEGIN IF (DB_ID(@database) IS NULL) OR ((select state from sys.databases where name = @database) &amp;lt;&amp;gt;0)&lt;br /&gt;
BEGIN                       &lt;br /&gt;
RAISERROR('Database %s is invalid or database status is not ONLINE',16,1,@database)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP  &lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- check @optype is valid&lt;br /&gt;
IF UPPER(@optype) NOT IN ('LOG','DB','DIFF','REINDEX','REORG','CHECKDB')&lt;br /&gt;
BEGIN                       &lt;br /&gt;
RAISERROR('%s is not a valid option for @optype',16,1,@optype)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP  &lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- check recovery mode is correct if trying log backup&lt;br /&gt;
IF @database NOT IN ('ALL_USER','ALL_SYSTEM')&lt;br /&gt;
BEGIN&lt;br /&gt;
IF (@optype = 'LOG' and ((select recovery_model from sys.databases where name = @database) = 3))&lt;br /&gt;
BEGIN                       &lt;br /&gt;
RAISERROR('%s is not a valid option for database %s because it is in SIMPLE recovery mode',16,1,@optype,@database)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- no log backups for system databases&lt;br /&gt;
IF @database = 'ALL_SYSTEM'&lt;br /&gt;
BEGIN&lt;br /&gt;
IF @optype = 'LOG'&lt;br /&gt;
BEGIN                       &lt;br /&gt;
RAISERROR('%s is not a valid option for the option ALL_SYSTEM',16,1,@optype)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- check that @backupfldr exists on the server&lt;br /&gt;
IF @optype NOT IN ('REINDEX','CHECKDB','REORG')&lt;br /&gt;
BEGIN&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @fso,'FolderExists',@exists OUT,@backupfldr&lt;br /&gt;
IF @exists &amp;lt;&amp;gt; 'True'&lt;br /&gt;
BEGIN                       &lt;br /&gt;
RAISERROR('The folder %s does not exist on this server',16,1,@backupfldr)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
ELSE&lt;br /&gt;
BEGIN&lt;br /&gt;
INSERT #exists&lt;br /&gt;
EXEC master.dbo.xp_fileexist @backupfldr&lt;br /&gt;
IF (SELECT MAX(isdir) FROM #exists)&amp;lt;&amp;gt;1&lt;br /&gt;
BEGIN                       &lt;br /&gt;
RAISERROR('The folder %s does not exist on this server',16,1,@backupfldr)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- check that @reportfldr exists on the server&lt;br /&gt;
IF @reportfldr IS NOT NULL or @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @fso,'FolderExists',@exists OUT,@reportfldr&lt;br /&gt;
IF @exists &amp;lt;&amp;gt; 'True'&lt;br /&gt;
BEGIN                       &lt;br /&gt;
RAISERROR('The folder %s does not exist on this server',16,1,@reportfldr)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
ELSE&lt;br /&gt;
BEGIN&lt;br /&gt;
DELETE #exists&lt;br /&gt;
INSERT #exists&lt;br /&gt;
EXEC master.dbo.xp_fileexist @reportfldr&lt;br /&gt;
IF (SELECT MAX(isdir) FROM #exists)&amp;lt;&amp;gt;1&lt;br /&gt;
BEGIN                       &lt;br /&gt;
RAISERROR('The folder %s does not exist on this server',16,1,@reportfldr)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- check @dbretainunit is a vaild value&lt;br /&gt;
IF @optype NOT IN ('REINDEX','CHECKDB','REORG')&lt;br /&gt;
BEGIN&lt;br /&gt;
IF UPPER(@dbretainunit) NOT IN ('MINUTES','HOURS','DAYS','WEEKS','MONTHS','COPIES')&lt;br /&gt;
BEGIN                       &lt;br /&gt;
RAISERROR('%s is not a valid value for @dbretainunit (''minutes | hours | days | weeks | months | copies'')',16,1,@dbretainunit)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
--check @dbretainval is a vaild value&lt;br /&gt;
IF @dbretainval&amp;lt;1 BEGIN                        RAISERROR('%i is not a valid value for @dbretainval (must be &amp;gt;0)',16,1,@dbretainval)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- check @rptretainunit is a vaild value if present&lt;br /&gt;
IF UPPER(@rptretainunit) NOT IN ('MINUTES','HOURS','DAYS','WEEKS','MONTHS','COPIES') and @rptretainunit IS NOT NULL&lt;br /&gt;
BEGIN                       &lt;br /&gt;
RAISERROR('%s is not a valid value for @rptretainunit (''minutes | hours | days | weeks | months | copies'')',16,1,@rptretainunit)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
--check @rptretainval is a vaild value&lt;br /&gt;
IF @rptretainval&amp;lt;1 BEGIN                        RAISERROR('%i is not a valid value for @rptretainval (must be &amp;gt;0)',16,1,@rptretainval)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
/***********************************&lt;br /&gt;
list of databases to process&lt;br /&gt;
************************************/&lt;br /&gt;
&lt;br /&gt;
IF @database IN ('ALL_USER','ALL_SYSTEM')&lt;br /&gt;
BEGIN&lt;br /&gt;
IF @database = 'ALL_USER'&lt;br /&gt;
INSERT #databases(dbname) &lt;br /&gt;
SELECT [name] from sys.databases where database_id &amp;gt; 4&lt;br /&gt;
AND (@optype &amp;lt;&amp;gt; 'LOG' OR recovery_model &amp;lt;&amp;gt; '3')&lt;br /&gt;
ELSE&lt;br /&gt;
INSERT #databases(dbname) &lt;br /&gt;
SELECT [name] from sys.databases where database_id in (1,3,4)&lt;br /&gt;
END&lt;br /&gt;
ELSE&lt;br /&gt;
INSERT #databases(dbname) SELECT @database&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/***********************************&lt;br /&gt;
INITIALIZE REPORT IF @report = 1&lt;br /&gt;
************************************/&lt;br /&gt;
&lt;br /&gt;
-- generate report filename&lt;br /&gt;
SELECT @reportfilename = @reportfldr + REPLACE(@database,' ','_') +&lt;br /&gt;
CASE WHEN UPPER(@optype) = 'DB'   THEN '_FullDBBackup_report_'&lt;br /&gt;
WHEN UPPER(@optype) = 'DIFF' THEN '_DiffDBBackup_report_'&lt;br /&gt;
WHEN UPPER(@optype) = 'LOG'  THEN '_LogBackup_report_'   &lt;br /&gt;
WHEN UPPER(@optype) = 'REINDEX'  THEN '_Reindex_report_'    &lt;br /&gt;
WHEN UPPER(@optype) = 'REORG'  THEN '_Reorg_report_'   &lt;br /&gt;
WHEN UPPER(@optype) = 'CHECKDB'  THEN '_CheckDB_report_'      &lt;br /&gt;
END + @jobstart + '.txt'&lt;br /&gt;
&lt;br /&gt;
-- if no report just set @reportfilename to NULL&lt;br /&gt;
IF @report = 0 SET @reportfilename = NULL&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
PRINT '@reportfilename = ' + ISNULL(@reportfilename,'NULL')&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
-- create report file&lt;br /&gt;
EXEC @hr=sp_OAMethod @fso, 'CreateTextFile',@file OUT, @reportfilename&lt;br /&gt;
IF (@hr &amp;lt;&amp;gt; 0)&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAGetErrorInfo @fso &lt;br /&gt;
RAISERROR('Error creating log file',16,1)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
GOTO CLEANUP &lt;br /&gt;
END&lt;br /&gt;
ELSE&lt;br /&gt;
-- set global flag to indicate we have created a report file&lt;br /&gt;
SET @filecrt = 1&lt;br /&gt;
&lt;br /&gt;
-- write header   &lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
SET @output = 'Expressmaint utility, Logged on to SQL Server [' + @@SERVERNAME + '] as ' + '[' + @user + ']'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output       &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IF UPPER(@optype) NOT IN ('REINDEX','CHECKDB','REORG')&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @output = 'Starting backup on ' + convert(varchar(25),getdate(),100)&lt;br /&gt;
END&lt;br /&gt;
IF UPPER(@optype) = 'CHECKDB'&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @output = 'Starting CheckDB on ' + convert(varchar(25),getdate(),100)&lt;br /&gt;
END      &lt;br /&gt;
IF UPPER(@optype) IN ('REINDEX','REORG')&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @output = 'Starting Reindex on ' + convert(varchar(25),getdate(),100)&lt;br /&gt;
END &lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
BACKUP ACTIONS&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
IF  UPPER(@optype) = 'CHECKDB' GOTO CHECK_DB&lt;br /&gt;
IF  UPPER(@optype) IN ('REINDEX','REORG') GOTO REINDEX&lt;br /&gt;
&lt;br /&gt;
-- if @delfirst = 1  we need to delete prior backups that qualify&lt;br /&gt;
IF @delfirst = 1 GOTO DELFIRST&lt;br /&gt;
&lt;br /&gt;
-- this label is so that we can return here after deleting files if @delfirst = 1&lt;br /&gt;
DOBACKUP:&lt;br /&gt;
&lt;br /&gt;
DECLARE dcur CURSOR LOCAL FAST_FORWARD&lt;br /&gt;
FOR SELECT dbname FROM #databases ORDER BY dbname&lt;br /&gt;
OPEN dcur&lt;br /&gt;
FETCH NEXT FROM dcur into @database&lt;br /&gt;
WHILE @@FETCH_STATUS=0&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
-- set backup start time&lt;br /&gt;
SET @start = GETDATE()&lt;br /&gt;
&lt;br /&gt;
-- write to text report&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @output = '[' + CAST(@stage as varchar(10)) + '] Database ' + @database + ': ' +&lt;br /&gt;
CASE WHEN UPPER(@optype) = 'DB'   THEN 'Full Backup '&lt;br /&gt;
WHEN UPPER(@optype) = 'DIFF' THEN 'Differential Backup '&lt;br /&gt;
WHEN UPPER(@optype) = 'LOG'  THEN 'Log Backup '         &lt;br /&gt;
END + 'starting at ' + CONVERT(varchar(25),@start,100)&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- backup subfolder&lt;br /&gt;
SET @execmd = 'IF NOT EXIST "' + @backupfldrorig + @database + '\" MKDIR "' + @backupfldrorig + @database + '\"'&lt;br /&gt;
EXEC master.dbo.xp_cmdshell @execmd,no_output&lt;br /&gt;
SET @backupfldr = @backupfldrorig + @database + '\'&lt;br /&gt;
&lt;br /&gt;
SELECT @backupfilename = @backupfldr + REPLACE(@database,' ','_') +&lt;br /&gt;
CASE WHEN UPPER(@optype) = 'DB'   THEN '_FullDBBackup_'&lt;br /&gt;
WHEN UPPER(@optype) = 'DIFF' THEN '_DiffDBBackup_'&lt;br /&gt;
WHEN UPPER(@optype) = 'LOG'  THEN '_LogBackup_'         &lt;br /&gt;
END + @jobstart + &lt;br /&gt;
CASE WHEN UPPER(@optype) = 'LOG' THEN '.TRN' ELSE '.BAK' END&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
FULL BACKUP&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
IF UPPER(@optype) = 'DB'&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
SET @execmd = N'BACKUP DATABASE [' + @database + '] TO DISK = ''' + @backupfilename + '''' +&lt;br /&gt;
CASE WHEN @backupwith IS NULL THEN '' ELSE (' WITH ' + @backupwith) END&lt;br /&gt;
&lt;br /&gt;
BEGIN TRY&lt;br /&gt;
&lt;br /&gt;
EXEC(@execmd)&lt;br /&gt;
&lt;br /&gt;
END TRY&lt;br /&gt;
BEGIN CATCH -- backup failure&lt;br /&gt;
SELECT @err = @@ERROR,@ret = @err&lt;br /&gt;
SELECT @errormsg = 'Full backup of database ' + @database + ' failed with error : ' +  CAST(@err as varchar(10))&lt;br /&gt;
SET @output = SPACE(4) + '*** ' + @errormsg + ' ***'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
SET @output = SPACE(4) + 'Refer to SQL Error Log and NT Event Log for further details'&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END&lt;br /&gt;
CLOSE dcur&lt;br /&gt;
DEALLOCATE dcur&lt;br /&gt;
GOTO CLEANUP&lt;br /&gt;
&lt;br /&gt;
END CATCH &lt;br /&gt;
&lt;br /&gt;
-- backup success&lt;br /&gt;
SET @finish = GETDATE()&lt;br /&gt;
SET @output = SPACE(4) + 'Database backed up to ' + @backupfilename&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
--calculate backup runtime&lt;br /&gt;
SET @runtime = (@finish - @start)&lt;br /&gt;
SET @output = SPACE(4) + 'Full database backup completed in '&lt;br /&gt;
+ CAST(DATEPART(hh,@runtime) as varchar(2)) + ' hour(s) '&lt;br /&gt;
+ CAST(DATEPART(mi,@runtime) as varchar(2)) + ' min(s) '&lt;br /&gt;
+ CAST(DATEPART(ss,@runtime) as varchar(2)) + ' second(s)'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
DIFFERENTIAL BACKUP&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
IF UPPER(@optype) = 'DIFF'&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
SET @execmd = N'BACKUP DATABASE [' + @database + '] TO DISK = ''' + &lt;br /&gt;
@backupfilename + ''' WITH DIFFERENTIAL' +&lt;br /&gt;
CASE WHEN @backupwith IS NULL THEN '' ELSE (' , ' + @backupwith) END&lt;br /&gt;
&lt;br /&gt;
BEGIN TRY&lt;br /&gt;
&lt;br /&gt;
EXEC(@execmd)&lt;br /&gt;
&lt;br /&gt;
END TRY&lt;br /&gt;
BEGIN CATCH -- backup failure&lt;br /&gt;
&lt;br /&gt;
SELECT @err = @@ERROR,@ret = @err&lt;br /&gt;
SELECT @errormsg = 'Differential backup of database ' + @database + ' failed with error : ' +  CAST(@err as varchar(10))&lt;br /&gt;
SET @output = SPACE(4) + '*** ' + @errormsg + ' ***'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
SET @output = SPACE(4) + 'Refer to SQL Error Log and NT Event Log for further details'&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
CLOSE dcur&lt;br /&gt;
DEALLOCATE dcur&lt;br /&gt;
GOTO CLEANUP&lt;br /&gt;
&lt;br /&gt;
END CATCH &lt;br /&gt;
&lt;br /&gt;
-- backup success&lt;br /&gt;
SET @finish = GETDATE()&lt;br /&gt;
SET @output = SPACE(4) + 'Database backed up to ' + @backupfilename&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
--calculate backup runtime&lt;br /&gt;
SET @runtime = (@finish - @start)&lt;br /&gt;
SET @output = SPACE(4) + 'Differential database backup completed in '&lt;br /&gt;
+ CAST(DATEPART(hh,@runtime) as varchar(2)) + ' hour(s) '&lt;br /&gt;
+ CAST(DATEPART(mi,@runtime) as varchar(2)) + ' min(s) '&lt;br /&gt;
+ CAST(DATEPART(ss,@runtime) as varchar(2)) + ' second(s)'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
LOG BACKUP&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
IF UPPER(@optype) = 'LOG'&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
SET @execmd = N'BACKUP LOG [' + @database + '] TO DISK = ''' + @backupfilename + '''' +&lt;br /&gt;
CASE WHEN @backupwith IS NULL THEN '' ELSE (' WITH ' + @backupwith) END&lt;br /&gt;
&lt;br /&gt;
BEGIN TRY&lt;br /&gt;
&lt;br /&gt;
EXEC(@execmd)&lt;br /&gt;
&lt;br /&gt;
END TRY&lt;br /&gt;
BEGIN CATCH -- backup failure&lt;br /&gt;
&lt;br /&gt;
SELECT @err = @@ERROR,@ret = @err&lt;br /&gt;
SELECT @errormsg = 'Log backup of database ' + @database + ' failed with error : ' +  CAST(@err as varchar(10))        &lt;br /&gt;
SET @output = SPACE(4) + '*** ' + @errormsg + ' ***'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
SET @output = SPACE(4) + 'Refer to SQL Error Log and NT Event Log for further details'&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
CLOSE dcur&lt;br /&gt;
DEALLOCATE dcur&lt;br /&gt;
GOTO CLEANUP&lt;br /&gt;
&lt;br /&gt;
END CATCH &lt;br /&gt;
&lt;br /&gt;
-- backup success&lt;br /&gt;
SET @finish = GETDATE()&lt;br /&gt;
SET @output = SPACE(4) + 'Log backed up to ' + @backupfilename&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
--calculate backup runtime&lt;br /&gt;
SET @runtime = (@finish - @start)&lt;br /&gt;
SET @output = SPACE(4) + 'Log backup completed in '&lt;br /&gt;
+ CAST(DATEPART(hh,@runtime) as varchar(2)) + ' hour(s) '&lt;br /&gt;
+ CAST(DATEPART(mi,@runtime) as varchar(2)) + ' min(s) '&lt;br /&gt;
+ CAST(DATEPART(ss,@runtime) as varchar(2)) + ' second(s)'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
SET @stage = (@stage + 1)&lt;br /&gt;
&lt;br /&gt;
FETCH NEXT FROM dcur into @database&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
CLOSE dcur&lt;br /&gt;
DEALLOCATE dcur&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
VERIFY BACKUP&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
IF @verify = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
DECLARE dcur CURSOR LOCAL FAST_FORWARD&lt;br /&gt;
FOR SELECT dbname FROM #databases ORDER BY dbname&lt;br /&gt;
OPEN dcur&lt;br /&gt;
FETCH NEXT FROM dcur into @database&lt;br /&gt;
WHILE @@FETCH_STATUS=0&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
SELECT @backupfilename = @backupfldrorig + @database + '\' + REPLACE(@database,' ','_') +&lt;br /&gt;
CASE WHEN UPPER(@optype) = 'DB'   THEN '_FullDBBackup_'&lt;br /&gt;
WHEN UPPER(@optype) = 'DIFF' THEN '_DiffDBBackup_'&lt;br /&gt;
WHEN UPPER(@optype) = 'LOG'  THEN '_LogBackup_'         &lt;br /&gt;
END + @jobstart + &lt;br /&gt;
CASE WHEN UPPER(@optype) = 'LOG' THEN '.TRN' ELSE '.BAK' END&lt;br /&gt;
&lt;br /&gt;
SET @start = GETDATE()&lt;br /&gt;
&lt;br /&gt;
-- write to text report&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
SET @output = '[' + CAST(@stage as varchar(10)) + '] Database ' + @database + ': Verify Backup File...'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output  &lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
SET @execmd = N'RESTORE VERIFYONLY FROM DISK = ''' + @backupfilename + '''' +&lt;br /&gt;
CASE WHEN @verifywith IS NULL THEN '' ELSE (' WITH ' + @verifywith) END&lt;br /&gt;
&lt;br /&gt;
BEGIN TRY&lt;br /&gt;
&lt;br /&gt;
EXEC(@execmd)&lt;br /&gt;
&lt;br /&gt;
END TRY&lt;br /&gt;
BEGIN CATCH&lt;br /&gt;
&lt;br /&gt;
SELECT @err = @@ERROR,@ret = @err&lt;br /&gt;
SET @errormsg = 'Verify of ' + @backupfilename + ' failed with Native Error : ' + CAST(@err as varchar(10))&lt;br /&gt;
SET @output = SPACE(4) + '*** ' + @errormsg + ' ***'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
CLOSE dcur&lt;br /&gt;
DEALLOCATE dcur&lt;br /&gt;
GOTO CLEANUP&lt;br /&gt;
&lt;br /&gt;
END CATCH&lt;br /&gt;
&lt;br /&gt;
-- verify success&lt;br /&gt;
SET @finish = GETDATE()&lt;br /&gt;
SET @output = SPACE(4) + 'Backup file ' + @backupfilename + ' verified'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
--calculate verify runtime&lt;br /&gt;
SET @runtime = (@finish - @start)&lt;br /&gt;
SET @output = SPACE(4) + 'Verify backup completed in '&lt;br /&gt;
+ CAST(DATEPART(hh,@runtime) as varchar(2)) + ' hour(s) '&lt;br /&gt;
+ CAST(DATEPART(mi,@runtime) as varchar(2)) + ' min(s) '&lt;br /&gt;
+ CAST(DATEPART(ss,@runtime) as varchar(2)) + ' second(s)'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
SET @stage = (@stage + 1)&lt;br /&gt;
FETCH NEXT FROM dcur into @database &lt;br /&gt;
END     &lt;br /&gt;
&lt;br /&gt;
CLOSE dcur&lt;br /&gt;
DEALLOCATE dcur&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
DELETE OLD FILES&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-- we have already deleted files so skip to the end&lt;br /&gt;
IF @delfirst = 1 GOTO CLEANUP&lt;br /&gt;
&lt;br /&gt;
-- this label is so that we can delete files prior to backup if @delfirst = 1&lt;br /&gt;
DELFIRST:&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
DELETE OLD BACKUPS&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
SET @datepart = CASE &lt;br /&gt;
WHEN UPPER(@dbretainunit) = 'MINUTES' THEN N'mi'&lt;br /&gt;
WHEN UPPER(@dbretainunit) = 'HOURS'   THEN N'hh'&lt;br /&gt;
WHEN UPPER(@dbretainunit) = 'DAYS'    THEN N'dd'&lt;br /&gt;
WHEN UPPER(@dbretainunit) = 'WEEKS'   THEN N'ww'&lt;br /&gt;
WHEN UPPER(@dbretainunit) = 'MONTHS'  THEN N'yy'&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT '@datepart for backups = ' + @datepart&lt;br /&gt;
&lt;br /&gt;
-- write to text report&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END&lt;br /&gt;
SET @output = '[' + CAST(@stage as varchar(10)) + '] Delete Old Backup Files...'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
DECLARE dcur CURSOR LOCAL FAST_FORWARD&lt;br /&gt;
FOR SELECT dbname FROM #databases ORDER BY dbname&lt;br /&gt;
OPEN dcur&lt;br /&gt;
FETCH NEXT FROM dcur into @database&lt;br /&gt;
WHILE @@FETCH_STATUS=0&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
SET @backupfldr = + @backupfldrorig + @database + '\'&lt;br /&gt;
SELECT @backupfilename = @backupfldr + REPLACE(@database,' ','_') +&lt;br /&gt;
CASE WHEN UPPER(@optype) = 'DB'   THEN '_FullDBBackup_'&lt;br /&gt;
WHEN UPPER(@optype) = 'DIFF' THEN '_DiffDBBackup_'&lt;br /&gt;
WHEN UPPER(@optype) = 'LOG'  THEN '_LogBackup_'         &lt;br /&gt;
END + @jobstart + &lt;br /&gt;
CASE WHEN UPPER(@optype) = 'LOG' THEN '.TRN' ELSE '.BAK' END&lt;br /&gt;
&lt;br /&gt;
-- load files in @backupfldr&lt;br /&gt;
IF @checkattrib = 1&lt;br /&gt;
SET @cmd = 'dir /B /A-D-A /OD "' + @backupfldr + REPLACE(@database,' ','_') +&lt;br /&gt;
CASE WHEN UPPER(@optype) = 'DB' THEN '_FullDBBackup_'&lt;br /&gt;
WHEN UPPER(@optype) = 'DIFF' THEN '_DiffDBBackup_'&lt;br /&gt;
WHEN UPPER(@optype) = 'LOG'  THEN '_LogBackup_' END + '*' +&lt;br /&gt;
CASE WHEN UPPER(@optype) = 'LOG' THEN '.TRN' ELSE '.BAK' END + '"'&lt;br /&gt;
ELSE &lt;br /&gt;
SET @cmd = 'dir /B /A-D /OD "' + @backupfldr + REPLACE(@database,' ','_') +&lt;br /&gt;
CASE WHEN UPPER(@optype) = 'DB' THEN '_FullDBBackup_'&lt;br /&gt;
WHEN UPPER(@optype) = 'DIFF' THEN '_DiffDBBackup_'&lt;br /&gt;
WHEN UPPER(@optype) = 'LOG'  THEN '_LogBackup_' END + '*' +&lt;br /&gt;
CASE WHEN UPPER(@optype) = 'LOG' THEN '.TRN' ELSE '.BAK' END + '"'&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT '@cmd = ' + @cmd&lt;br /&gt;
&lt;br /&gt;
DELETE #files&lt;br /&gt;
INSERT #files EXEC master.dbo.xp_cmdshell @cmd&lt;br /&gt;
DELETE #files WHERE filename IS NULL or filename = ISNULL(REPLACE(@backupfilename,@backupfldr,''),'nothing')&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 SELECT * FROM #files&lt;br /&gt;
&lt;br /&gt;
-- get count of files that match pattern&lt;br /&gt;
SELECT @filecount = COUNT(*) from #files &lt;br /&gt;
WHERE PATINDEX('%File Not Found%',filename) = 0&lt;br /&gt;
AND PATINDEX('%The system cannot find%',filename) = 0 &lt;br /&gt;
&lt;br /&gt;
-- remove files that don't meet retention criteria if there are any files that match pattern&lt;br /&gt;
IF UPPER(@dbretainunit) &amp;lt;&amp;gt; 'COPIES'&lt;br /&gt;
BEGIN&lt;br /&gt;
IF @filecount&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @delcmd = N'DELETE #files WHERE DATEADD(' + @datepart + N',' + CAST(@dbretainval as nvarchar(10)) + N',' +&lt;br /&gt;
'CONVERT(datetime,(SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(''_'',REVERSE(filename)))+2),12),7,2) +''/''&lt;br /&gt;
+ SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(''_'',REVERSE(filename)))+2),12),5,2) +''/''&lt;br /&gt;
+ SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(''_'',REVERSE(filename)))+2),12),1,4) +'' ''&lt;br /&gt;
+ SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(''_'',REVERSE(filename)))+2),12),9,2) +'':''&lt;br /&gt;
+ SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(''_'',REVERSE(filename)))+2),12),11,2)),103)) &amp;gt; ''' + CAST(@jobdt as nvarchar(25)) + N''''&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT '@delcmd=' + @delcmd&lt;br /&gt;
EXEC master.dbo.sp_executesql @delcmd&lt;br /&gt;
&lt;br /&gt;
SELECT @delcount = COUNT(*) from #files&lt;br /&gt;
END&lt;br /&gt;
ELSE&lt;br /&gt;
BEGIN&lt;br /&gt;
SELECT @delcount = 0&lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
ELSE  -- number of copies not date based (include current backup that's not in #files)&lt;br /&gt;
BEGIN&lt;br /&gt;
IF @filecount&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
IF @dbretainval&amp;gt;1 &lt;br /&gt;
BEGIN&lt;br /&gt;
SET @delcmd = N'DELETE #files WHERE filename IN(SELECT TOP ' + CAST((@dbretainval-1) as nvarchar(10)) +&lt;br /&gt;
N' filename FROM #files ORDER BY substring(filename,((len(filename)+2)-charindex(''_'',reverse(filename))),12) DESC)'&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT '@delcmd=' + @delcmd&lt;br /&gt;
EXEC master.dbo.sp_executesql @delcmd&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
SELECT @delcount = COUNT(*) from #files&lt;br /&gt;
&lt;br /&gt;
END&lt;br /&gt;
ELSE&lt;br /&gt;
BEGIN&lt;br /&gt;
SELECT @delcount = 0&lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT '@delcount = ' + STR(@delcount)&lt;br /&gt;
&lt;br /&gt;
-- if there are any matching files&lt;br /&gt;
IF @filecount&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
-- are there any files that need deleting&lt;br /&gt;
IF @delcount&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
DECLARE FCUR CURSOR FORWARD_ONLY FOR&lt;br /&gt;
SELECT * FROM #files&lt;br /&gt;
OPEN FCUR&lt;br /&gt;
FETCH NEXT FROM FCUR INTO @delfilename&lt;br /&gt;
WHILE @@FETCH_STATUS=0&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @cmd = 'DEL /Q "' + @backupfldr + @delfilename + '"'&lt;br /&gt;
EXEC @cmdret = master.dbo.xp_cmdshell @cmd,no_output   &lt;br /&gt;
&lt;br /&gt;
-- log failure to delete but don't abort procedure&lt;br /&gt;
IF @cmdret&amp;lt;&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @output = SPACE(4) + '*** Error: Failed to delete file ' + @backupfldr + @delfilename + ' ***'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
SELECT @delbkflag = 1 , @cmdret = 0, @delcount = (@delcount-1)&lt;br /&gt;
END&lt;br /&gt;
ELSE&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @output = SPACE(4) + 'Deleted file ' + @backupfldr + @delfilename&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
FETCH NEXT FROM FCUR INTO @delfilename&lt;br /&gt;
END&lt;br /&gt;
CLOSE FCUR&lt;br /&gt;
DEALLOCATE FCUR&lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- write to text report&lt;br /&gt;
SET @output = SPACE(4) + CAST(@delcount as varchar(10)) + ' file(s) deleted.'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
FETCH NEXT FROM dcur into @database&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
CLOSE dcur&lt;br /&gt;
DEALLOCATE dcur&lt;br /&gt;
&lt;br /&gt;
-- clear temporary table and variables&lt;br /&gt;
DELETE #files&lt;br /&gt;
SET @cmd = ''&lt;br /&gt;
SET @delcmd = ''&lt;br /&gt;
SET @delfilename = ''&lt;br /&gt;
SET @datepart = ''&lt;br /&gt;
SET @filecount = 0&lt;br /&gt;
SET @delcount = 0&lt;br /&gt;
SET @cmdret = 0&lt;br /&gt;
SET @stage = @stage + 1&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
DELETE OLD REPORTS&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
DELREPORTS:&lt;br /&gt;
&lt;br /&gt;
IF @rptretainunit IS NOT NULL&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @datepart = CASE &lt;br /&gt;
WHEN UPPER(@rptretainunit) = 'MINUTES' THEN N'mi'&lt;br /&gt;
WHEN UPPER(@rptretainunit) = 'HOURS'   THEN N'hh'&lt;br /&gt;
WHEN UPPER(@rptretainunit) = 'DAYS'    THEN N'dd'&lt;br /&gt;
WHEN UPPER(@rptretainunit) = 'WEEKS'   THEN N'ww'&lt;br /&gt;
WHEN UPPER(@rptretainunit) = 'MONTHS'  THEN N'yy'&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT '@datepart for reports = ' + @datepart&lt;br /&gt;
&lt;br /&gt;
-- write to text report&lt;br /&gt;
SET @output = '[' + CAST(@stage as varchar(10)) + '] Delete Old Report Files...'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- load files in @reportfldr&lt;br /&gt;
SET @cmd = 'dir /B /A-D /OD "' + @reportfldr + REPLACE(@databaseorig,' ','_') +&lt;br /&gt;
CASE WHEN UPPER(@optype) = 'DB' THEN '_FullDBBackup_report_'&lt;br /&gt;
WHEN UPPER(@optype) = 'DIFF' THEN '_DiffDBBackup_report_'&lt;br /&gt;
WHEN UPPER(@optype) = 'REINDEX'  THEN '_Reindex_report_'     &lt;br /&gt;
WHEN UPPER(@optype) = 'CHECKDB'  THEN '_CheckDB_report_'     &lt;br /&gt;
WHEN UPPER(@optype) = 'REORG'  THEN '_Reorg_report_' &lt;br /&gt;
WHEN UPPER(@optype) = 'LOG'  THEN '_LogBackup_report_' END + '*.txt"'&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT '@cmd = ' + @cmd&lt;br /&gt;
&lt;br /&gt;
INSERT #files EXEC master.dbo.xp_cmdshell @cmd&lt;br /&gt;
DELETE #files WHERE filename IS NULL&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 SELECT * FROM #files&lt;br /&gt;
&lt;br /&gt;
-- get count of files that match pattern&lt;br /&gt;
SELECT @filecount = COUNT(*) from #files &lt;br /&gt;
WHERE PATINDEX('%File Not Found%',filename) = 0&lt;br /&gt;
AND PATINDEX('%The system cannot find%',filename) = 0 &lt;br /&gt;
&lt;br /&gt;
-- remove files that don't meet retention criteria if there are any files that match pattern&lt;br /&gt;
IF UPPER(@rptretainunit) &amp;lt;&amp;gt; 'COPIES'&lt;br /&gt;
BEGIN&lt;br /&gt;
IF @filecount&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @delcmd = N'DELETE #files WHERE DATEADD(' + @datepart + N',' + CAST(@rptretainval as nvarchar(10)) + N',' +&lt;br /&gt;
'CONVERT(datetime,(SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(''_'',REVERSE(filename)))+2),12),7,2) +''/''&lt;br /&gt;
+ SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(''_'',REVERSE(filename)))+2),12),5,2) +''/''&lt;br /&gt;
+ SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(''_'',REVERSE(filename)))+2),12),1,4) +'' ''&lt;br /&gt;
+ SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(''_'',REVERSE(filename)))+2),12),9,2) +'':''&lt;br /&gt;
+ SUBSTRING(SUBSTRING(filename,((LEN(filename)-CHARINDEX(''_'',REVERSE(filename)))+2),12),11,2)),103)) &amp;gt; ''' + CAST(@jobdt as nvarchar(25)) + N''''&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT '@delcmd=' + @delcmd&lt;br /&gt;
EXEC master.dbo.sp_executesql @delcmd&lt;br /&gt;
&lt;br /&gt;
SELECT @delcount = COUNT(*) from #files&lt;br /&gt;
END&lt;br /&gt;
ELSE&lt;br /&gt;
BEGIN&lt;br /&gt;
SELECT @delcount = 0&lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
ELSE  -- number of copies not date based&lt;br /&gt;
BEGIN&lt;br /&gt;
IF @filecount&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @delcmd = N'DELETE #files WHERE filename IN(SELECT TOP ' + CAST(@rptretainval as nvarchar(10)) +&lt;br /&gt;
N' filename FROM #files ORDER BY substring(filename,((len(filename)+2)-charindex(''_'',reverse(filename))),12) DESC)'&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT '@delcmd=' + @delcmd&lt;br /&gt;
EXEC master.dbo.sp_executesql @delcmd&lt;br /&gt;
&lt;br /&gt;
SELECT @delcount = COUNT(*) from #files&lt;br /&gt;
END&lt;br /&gt;
ELSE&lt;br /&gt;
BEGIN&lt;br /&gt;
SELECT @delcount = 0&lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT STR(@delcount)&lt;br /&gt;
&lt;br /&gt;
-- if there are any matching files&lt;br /&gt;
IF @filecount&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
-- are there any files that need deleting&lt;br /&gt;
IF @delcount&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
DECLARE FCUR CURSOR FORWARD_ONLY FOR&lt;br /&gt;
SELECT * FROM #files&lt;br /&gt;
OPEN FCUR&lt;br /&gt;
FETCH NEXT FROM FCUR INTO @delfilename&lt;br /&gt;
WHILE @@FETCH_STATUS=0&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @cmd = 'DEL /Q "' + @reportfldr + @delfilename + '"'&lt;br /&gt;
EXEC @cmdret = master.dbo.xp_cmdshell @cmd,no_output   &lt;br /&gt;
&lt;br /&gt;
-- log failure to delete but don't abort procedure&lt;br /&gt;
IF @cmdret&amp;lt;&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
SET @output = SPACE(4) + '*** Error: Failed to delete file ' + @reportfldr + @delfilename + ' ***'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
SELECT @delrptflag = 1 , @cmdret = 0, @delcount = (@delcount-1)&lt;br /&gt;
END&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @output = SPACE(4) + 'Deleted file ' + @reportfldr + @delfilename&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
FETCH NEXT FROM FCUR INTO @delfilename&lt;br /&gt;
END&lt;br /&gt;
CLOSE FCUR&lt;br /&gt;
DEALLOCATE FCUR&lt;br /&gt;
END&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- write to text report&lt;br /&gt;
SET @output = SPACE(4) + CAST(@delcount as varchar(10)) + ' file(s) deleted.'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- update stage&lt;br /&gt;
SET @stage = @stage + 1&lt;br /&gt;
END&lt;br /&gt;
-- if we got here due to @delfirst = 1 go back and do the backups&lt;br /&gt;
IF @delfirst = 1 &lt;br /&gt;
GOTO DOBACKUP&lt;br /&gt;
ELSE &lt;br /&gt;
GOTO CLEANUP&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
CHECKDB &lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
CHECK_DB:&lt;br /&gt;
&lt;br /&gt;
IF @optype = 'CHECKDB'&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
DECLARE dcur CURSOR LOCAL FAST_FORWARD&lt;br /&gt;
FOR SELECT dbname FROM #databases ORDER BY dbname&lt;br /&gt;
OPEN dcur&lt;br /&gt;
FETCH NEXT FROM dcur into @database&lt;br /&gt;
WHILE @@FETCH_STATUS=0&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
-- write to text report&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
SET @output = '[' + CAST(@stage as varchar(10)) + '] Database ' + @database + ': Check Data and Index Linkage...'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output  &lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- set backup start time&lt;br /&gt;
SET @start = GETDATE()&lt;br /&gt;
&lt;br /&gt;
SET @execmd = N'DBCC CHECKDB([' + @database + N']) WITH NO_INFOMSGS'&lt;br /&gt;
IF @debug = 1 PRINT 'DBCC Command : ' + @execmd&lt;br /&gt;
&lt;br /&gt;
BEGIN TRY&lt;br /&gt;
&lt;br /&gt;
EXEC(@execmd)&lt;br /&gt;
&lt;br /&gt;
END TRY&lt;br /&gt;
BEGIN CATCH&lt;br /&gt;
&lt;br /&gt;
SELECT @err = @@ERROR,@ret = @err&lt;br /&gt;
SET @errormsg = 'CheckDB of ' + @database + ' failed with Native Error : ' + CAST(@err as varchar(10))&lt;br /&gt;
SET @output = SPACE(4) + '*** ' + @errormsg + ' ***'&lt;br /&gt;
PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
CLOSE dcur&lt;br /&gt;
DEALLOCATE dcur&lt;br /&gt;
GOTO CLEANUP&lt;br /&gt;
&lt;br /&gt;
END CATCH&lt;br /&gt;
&lt;br /&gt;
SET @finish = GETDATE()&lt;br /&gt;
&lt;br /&gt;
--calculate checkdb runtime&lt;br /&gt;
SET @runtime = (@finish - @start)&lt;br /&gt;
SET @output = SPACE(4) + 'CheckDB completed in '&lt;br /&gt;
+ CAST(DATEPART(hh,@runtime) as varchar(2)) + ' hour(s) '&lt;br /&gt;
+ CAST(DATEPART(mi,@runtime) as varchar(2)) + ' min(s) '&lt;br /&gt;
+ CAST(DATEPART(ss,@runtime) as varchar(2)) + ' second(s)'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END   &lt;br /&gt;
SET @stage = (@stage + 1)&lt;br /&gt;
FETCH NEXT FROM dcur into @database      &lt;br /&gt;
&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
CLOSE dcur&lt;br /&gt;
DEALLOCATE dcur&lt;br /&gt;
&lt;br /&gt;
-- delete reports&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END&lt;br /&gt;
GOTO DELREPORTS&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
REINDEX/REORG&lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
REINDEX:&lt;br /&gt;
&lt;br /&gt;
IF @optype in ('REINDEX','REORG')&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
DECLARE dcur CURSOR LOCAL FAST_FORWARD&lt;br /&gt;
FOR SELECT dbname FROM #databases ORDER BY dbname&lt;br /&gt;
OPEN dcur&lt;br /&gt;
FETCH NEXT FROM dcur into @database&lt;br /&gt;
WHILE @@FETCH_STATUS=0&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
-- write to text report&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
IF @optype = 'REINDEX'&lt;br /&gt;
SET @output = '[' + CAST(@stage as varchar(10)) + '] Database ' + @database + ': Index Rebuild (using original fillfactor)...'&lt;br /&gt;
ELSE&lt;br /&gt;
SET @output = '[' + CAST(@stage as varchar(10)) + '] Database ' + @database + ': Index Reorganize...'&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output  &lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''  &lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- set start time&lt;br /&gt;
SET @start = GETDATE()&lt;br /&gt;
&lt;br /&gt;
-- all user tables&lt;br /&gt;
CREATE TABLE #tables(tablename sysname)&lt;br /&gt;
EXEC(N'INSERT #tables(tablename) SELECT DISTINCT(''['' + s.[name] + ''].['' + t.[name] + '']'') FROM [' + @database + N'].sys.tables t ' +&lt;br /&gt;
N'JOIN [' + @database + N'].sys.schemas s on t.schema_id=s.schema_id ' +&lt;br /&gt;
N'JOIN [' + @database + N'].sys.indexes i on t.object_id=i.object_id ' +&lt;br /&gt;
N'WHERE t.is_ms_shipped = 0 AND i.type&amp;gt;0')&lt;br /&gt;
&lt;br /&gt;
DECLARE tcur CURSOR LOCAL FAST_FORWARD &lt;br /&gt;
FOR SELECT tablename FROM #tables ORDER BY tablename&lt;br /&gt;
OPEN tcur&lt;br /&gt;
FETCH NEXT FROM tcur INTO @table&lt;br /&gt;
WHILE @@FETCH_STATUS = 0&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
IF @optype = 'REINDEX'&lt;br /&gt;
SET @output = SPACE(4) + N'Rebuilding indexes for table ' + @table&lt;br /&gt;
ELSE&lt;br /&gt;
SET @output = SPACE(4) + N'Reorganizing indexes for table ' + @table&lt;br /&gt;
&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
IF @optype = 'REINDEX'&lt;br /&gt;
SET @execmd = N'ALTER INDEX ALL ON [' + @database + N'].' + @table + N' REBUILD'&lt;br /&gt;
ELSE&lt;br /&gt;
SET @execmd = N'ALTER INDEX ALL ON [' + @database + N'].' + @table + N' REORGANIZE'&lt;br /&gt;
&lt;br /&gt;
IF @debug = 1 PRINT 'Reindex Command : ' + @execmd&lt;br /&gt;
&lt;br /&gt;
BEGIN TRY&lt;br /&gt;
&lt;br /&gt;
EXEC(@execmd)&lt;br /&gt;
&lt;br /&gt;
END TRY&lt;br /&gt;
BEGIN CATCH&lt;br /&gt;
&lt;br /&gt;
SELECT @err = @@ERROR,@ret = @err&lt;br /&gt;
SET @errormsg = 'Rebuild of indexes on [' + @database + N'].' + @table + ' failed with Native Error : ' + CAST(@err as varchar(10))&lt;br /&gt;
SET @output = SPACE(4) + '*** ' + @errormsg + ' ***'&lt;br /&gt;
PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
END&lt;br /&gt;
CLOSE tcur&lt;br /&gt;
DEALLOCATE tcur&lt;br /&gt;
DROP TABLE #tables&lt;br /&gt;
GOTO CLEANUP&lt;br /&gt;
&lt;br /&gt;
END CATCH&lt;br /&gt;
&lt;br /&gt;
FETCH NEXT FROM tcur INTO @table&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
CLOSE tcur&lt;br /&gt;
DEALLOCATE tcur&lt;br /&gt;
&lt;br /&gt;
SET @finish = GETDATE()&lt;br /&gt;
&lt;br /&gt;
--calculate runtime&lt;br /&gt;
SET @runtime = (@finish - @start)&lt;br /&gt;
SET @output = SPACE(4) + 'Index maintenance completed in '&lt;br /&gt;
+ CAST(DATEPART(hh,@runtime) as varchar(2)) + ' hour(s) '&lt;br /&gt;
+ CAST(DATEPART(mi,@runtime) as varchar(2)) + ' min(s) '&lt;br /&gt;
+ CAST(DATEPART(ss,@runtime) as varchar(2)) + ' second(s)'&lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
END   &lt;br /&gt;
&lt;br /&gt;
DROP TABLE #tables&lt;br /&gt;
&lt;br /&gt;
SET @stage = (@stage + 1)&lt;br /&gt;
FETCH NEXT FROM dcur into @database   &lt;br /&gt;
&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
CLOSE dcur&lt;br /&gt;
DEALLOCATE dcur&lt;br /&gt;
&lt;br /&gt;
-- delete reports&lt;br /&gt;
GOTO DELREPORTS&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/************************&lt;br /&gt;
CLEAN UP &lt;br /&gt;
************************/&lt;br /&gt;
&lt;br /&gt;
CLEANUP:&lt;br /&gt;
&lt;br /&gt;
DROP TABLE #files&lt;br /&gt;
DROP TABLE #exists&lt;br /&gt;
DROP TABLE #databases&lt;br /&gt;
&lt;br /&gt;
-- if we encountered errors deleting old backups return failure&lt;br /&gt;
IF @delbkflag&amp;lt;&amp;gt;0&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @errormsg = 'Expressmaint encountered errors deleting old backup files' + CHAR(13)&lt;br /&gt;
+ CASE WHEN @report = 1 THEN ('Please see ' + @reportfilename + CHAR(13) + ' for further details') ELSE '' END&lt;br /&gt;
RAISERROR(@errormsg,16,1)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- if we encountered errors deleting old reports return failure&lt;br /&gt;
IF (@delrptflag&amp;lt;&amp;gt;0 AND @delbkflag = 0)&lt;br /&gt;
BEGIN&lt;br /&gt;
SET @errormsg = 'Expressmaint encountered errors deleting old report files' + CHAR(13)&lt;br /&gt;
+ CASE WHEN @report = 1 THEN ('Please see ' + @reportfilename + CHAR(13) + ' for further details') ELSE '' END&lt;br /&gt;
RAISERROR(@errormsg,16,1)&lt;br /&gt;
SET @ret = 1&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
-- if we created a file make sure we write trailer and destroy object&lt;br /&gt;
IF @filecrt = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
-- write final part of report&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
SET @output = 'Expressmaint processing finished at ' + CONVERT(varchar(25),GETDATE(),100) &lt;br /&gt;
+ ' (Return Code : ' + CAST(@ret as varchar(10)) + ')' &lt;br /&gt;
IF @debug = 1 PRINT @output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,@output&lt;br /&gt;
EXEC sp_OAMethod @file,'WriteLine',NULL,''&lt;br /&gt;
&lt;br /&gt;
-- destroy file object&lt;br /&gt;
EXEC @hr=sp_OADestroy @file&lt;br /&gt;
IF @hr &amp;lt;&amp;gt; 0 EXEC sp_OAGetErrorInfo @file&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
IF @report = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
EXEC @hr=sp_OADestroy @fso&lt;br /&gt;
IF @hr &amp;lt;&amp;gt; 0 EXEC sp_OAGetErrorInfo @fso&lt;br /&gt;
END&lt;br /&gt;
&lt;br /&gt;
RETURN @ret&lt;br /&gt;
GO&lt;br /&gt;
/***********************************************/&lt;br /&gt;
/*     Script Ends Here                        */ &lt;br /&gt;
/***********************************************/&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-7686755319524399948?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/ROY2aa4hpvZhHTA_t4WV8Eh8QkU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ROY2aa4hpvZhHTA_t4WV8Eh8QkU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/ROY2aa4hpvZhHTA_t4WV8Eh8QkU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ROY2aa4hpvZhHTA_t4WV8Eh8QkU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/r2bzQqOVlYM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/7686755319524399948/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=7686755319524399948&amp;isPopup=true" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/7686755319524399948?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/7686755319524399948?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/r2bzQqOVlYM/automating-database-maintenance-in-sql.html" title="Automating Database maintenance in SQL 2005 Express Edition Part I" /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>2</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2010/02/automating-database-maintenance-in-sql.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkcERno4eyp7ImA9WxVVE0w.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-8055741723468903074</id><published>2009-03-05T22:04:00.000-06:00</published><updated>2009-03-05T22:33:27.433-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-03-05T22:33:27.433-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="JavaScript" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET Framework 3.5" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="AJAX" /><category scheme="http://www.blogger.com/atom/ns#" term="Visual Studio 2008" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Code Samples" /><title>JavaScript: Add a prompt dialogue box to confirm the changes when user selects to “close” the InProgress work window.</title><content type="html">Here is the sample on how you can have a prompting dialogue box when user tries to exit the window with work in progress.&lt;br /&gt;&lt;br /&gt;*************************************************&lt;br /&gt;Sample Code: Just copy paste this code in your html file and it should work.&lt;br /&gt;*************************************************&lt;br /&gt;&amp;lt;script language="JavaScript"&amp;gt;&lt;br /&gt;&lt;br /&gt;var isChoice = 0;&lt;br /&gt;&lt;br /&gt;function callAlert(Msg,Title){&lt;br /&gt;&lt;br /&gt;txt = Msg;&lt;br /&gt;caption = Title;&lt;br /&gt;vbMsg(txt,caption)&lt;br /&gt;alert(isChoice);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;script language="VBScript"&amp;gt;&lt;br /&gt;&lt;br /&gt;Function vbMsg(isTxt,isCaption)&lt;br /&gt;&lt;br /&gt;testVal = MsgBox(isTxt,3,isCaption)&lt;br /&gt;isChoice = testVal&lt;br /&gt;&lt;br /&gt;End Function&lt;br /&gt;&lt;br /&gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;input onclick="callAlert('Do you want to save the changes to Document1?','Document1')" type="button" value="Exit"&amp;gt;&lt;br /&gt;&lt;br /&gt;*************************************************&lt;br /&gt;&lt;br /&gt;Please contact me @ &lt;a href="mailto:shasiza@gmail.com"&gt;&lt;strong&gt;shasiza@gmail.com&lt;/strong&gt;&lt;/a&gt; if you have any question or issues implementing this script.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-8055741723468903074?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/kfF5GyCjEUOcc5k8gRSDYatrje4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/kfF5GyCjEUOcc5k8gRSDYatrje4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/kfF5GyCjEUOcc5k8gRSDYatrje4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/kfF5GyCjEUOcc5k8gRSDYatrje4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/42xDxgfvndA" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/8055741723468903074/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=8055741723468903074&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/8055741723468903074?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/8055741723468903074?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/42xDxgfvndA/javascript-add-prompt-dialogue-box-to.html" title="JavaScript: Add a prompt dialogue box to confirm the changes when user selects to “close” the InProgress work window." /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2009/03/javascript-add-prompt-dialogue-box-to.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkQMR3Y5fip7ImA9WxBVEko.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-4047471618630835791</id><published>2008-05-15T23:46:00.001-05:00</published><updated>2010-02-15T17:59:46.826-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-15T17:59:46.826-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Code Samples" /><title>Export DataGrid to a Excel Spreadsheet</title><content type="html">Here is the code to do that:&lt;br /&gt;
&lt;br /&gt;
Steps:&lt;br /&gt;
1. Load your DataGrid any way you want.&lt;br /&gt;
2. Name your DataGrid to be grdDataGrid.&lt;br /&gt;
3. Add a new button on the page "Button1".&lt;br /&gt;
4. Copy and Paste following code, and you are done.&lt;br /&gt;
&lt;br /&gt;
*****************************************************&lt;br /&gt;
protected void Button1_Click(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
Response.Clear();&lt;br /&gt;
&lt;br /&gt;
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", "test"));&lt;br /&gt;
Response.Charset = "";&lt;br /&gt;
&lt;br /&gt;
Response.ContentType = "application/vnd.xls";&lt;br /&gt;
&lt;br /&gt;
StringWriter stringWrite = new StringWriter();&lt;br /&gt;
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);&lt;br /&gt;
&lt;br /&gt;
grdDataGrid.RenderControl(htmlWrite);&lt;br /&gt;
Response.Write(stringWrite.ToString());&lt;br /&gt;
Response.Flush();&lt;br /&gt;
Response.End();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public override void VerifyRenderingInServerForm(Control control)&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
*******************************************************&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When you click on the button1, the following dialog window will show up.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://bp1.blogger.com/_vC4V9dUCDH0/SC0UIXpRdBI/AAAAAAAAAF0/UYHjrpxd75w/s1600-h/download.JPG"&gt;&lt;img alt="" border="0" id="BLOGGER_PHOTO_ID_5200835278701425682" src="http://bp1.blogger.com/_vC4V9dUCDH0/SC0UIXpRdBI/AAAAAAAAAF0/UYHjrpxd75w/s320/download.JPG" style="cursor: hand;" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
I hope it helps, please feel free to contact me if you have any question/comments.&lt;br /&gt;
&lt;br /&gt;
Thanks,&lt;br /&gt;
Sanjeev&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-4047471618630835791?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Sg3LFfpQkph3ssfSMynHIIbtO64/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Sg3LFfpQkph3ssfSMynHIIbtO64/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Sg3LFfpQkph3ssfSMynHIIbtO64/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Sg3LFfpQkph3ssfSMynHIIbtO64/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/spPy3Er77pc" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/4047471618630835791/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=4047471618630835791&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/4047471618630835791?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/4047471618630835791?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/spPy3Er77pc/export-datagrid-to-excel-spreadsheet.html" title="Export DataGrid to a Excel Spreadsheet" /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://bp1.blogger.com/_vC4V9dUCDH0/SC0UIXpRdBI/AAAAAAAAAF0/UYHjrpxd75w/s72-c/download.JPG" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2008/05/export-datagrid-to-excel-spreadsheet.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkMHQHozcSp7ImA9WxBVEko.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-5589317846947022273</id><published>2008-05-15T23:44:00.002-05:00</published><updated>2010-02-15T18:00:31.489-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-15T18:00:31.489-06:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Thoughts" /><title>I am back</title><content type="html">Hi Guys,&lt;br /&gt;
&lt;br /&gt;
It's been long time since I have posted my next post here, I am back now and will start updating my blog again regularly, please provide your feedback, that helps alot.&lt;br /&gt;
&lt;br /&gt;
Thanks,&lt;br /&gt;
Sanjeev&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-5589317846947022273?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/t6_JhLaTAlQG8jWbRvfP083JYy0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/t6_JhLaTAlQG8jWbRvfP083JYy0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/t6_JhLaTAlQG8jWbRvfP083JYy0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/t6_JhLaTAlQG8jWbRvfP083JYy0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/wP2db2AzPNc" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/5589317846947022273/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=5589317846947022273&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/5589317846947022273?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/5589317846947022273?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/wP2db2AzPNc/i-am-back.html" title="I am back" /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2008/05/i-am-back.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkMARng_eyp7ImA9WxBVEko.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-431106754808129754</id><published>2007-10-23T11:42:00.001-05:00</published><updated>2010-02-15T18:00:47.643-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-15T18:00:47.643-06:00</app:edited><title>Get up to speed on Visual Studio 2008 Get up to speed on Visual Studio 2008</title><content type="html">Get up to speed on Visual Studio 2008 Get up to speed on Visual Studio 2008&lt;br /&gt;
&lt;br /&gt;
Visual Studio 2008 (and the .NET Framework 3.5) is set to release to manufacturing in a few weeks (November timeframe). I've been working with it for about 6 months myself, and once you get pass the BETA "issues" it is a nice upgrade over Visual Studio 2005 in a variety of areas.&lt;br /&gt;
&lt;br /&gt;
news features:&lt;br /&gt;
&lt;br /&gt;
* Multi-targeted frameworks&lt;br /&gt;
- Within VS2008, the IDE gives you a choice of working with the .NET 2.0, 3.0, or 3.5 framework&lt;br /&gt;
- Which means that I don't have to have Visual Studio 2005 (for backward compatibility) and Visual Studio 2008 installed to get the new features&lt;br /&gt;
- The ability to switch frameworks overtime, so that I can start with .NET 2.0, for example, and as my company/customer approves .NET 3.0/3.5, I can switch my project to the new framework, and Visual Studio 2008 will automatically give me new choices (UI, Controls, Coding options).&lt;br /&gt;
&lt;br /&gt;
* LINQ (Language INtegrated Query)&lt;br /&gt;
- The ability to treat almost any object as data&lt;br /&gt;
- One querying language for Objects, XML, SQL Data, etc.&lt;br /&gt;
- and more. You should really watch the video on this one&lt;br /&gt;
&lt;br /&gt;
* New features for the Web Developer, among them&lt;br /&gt;
- Split View in the IDE, which will synchronize the HTML view and the GUI view, so that you can work in either and see the results synchronized in both panes.&lt;br /&gt;
- New CSS Support. Features that you've seen in higher end HTML editors, but now in Visual Studio, so that you don't have to switch tools during Development&lt;br /&gt;
&lt;br /&gt;
* Full ASP.NET Ajax integration&lt;br /&gt;
* And much more&lt;br /&gt;
&lt;br /&gt;
Click here to see the Full Video Clips: http://msdn2.microsoft.com/en-us/vstudio/bb655906.aspx&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-431106754808129754?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/bWuL3N5ouwteuUGjjM4aeaHB_ms/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/bWuL3N5ouwteuUGjjM4aeaHB_ms/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/bWuL3N5ouwteuUGjjM4aeaHB_ms/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/bWuL3N5ouwteuUGjjM4aeaHB_ms/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/-GAwPx3O-U8" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/431106754808129754/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=431106754808129754&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/431106754808129754?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/431106754808129754?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/-GAwPx3O-U8/get-up-to-speed-on-visual-studio-2008.html" title="Get up to speed on Visual Studio 2008 Get up to speed on Visual Studio 2008" /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2007/10/get-up-to-speed-on-visual-studio-2008.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkIERn4yfyp7ImA9WxBVEko.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-2002186912634411901</id><published>2007-09-27T14:27:00.001-05:00</published><updated>2010-02-15T18:01:47.097-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-15T18:01:47.097-06:00</app:edited><title>How to load an Excel spreadsheet into an ADO.NET DataSet</title><content type="html">You can do this using following code:&lt;br /&gt;
&lt;br /&gt;
DataSet objDS = new DataSet();&lt;br /&gt;
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + &lt;br /&gt;
"Data Source=" + strFileName.Replace("\\", "\\\\") + ";" +&lt;br /&gt;
"Extended Properties=\"Excel 8.0;\"";&lt;br /&gt;
objOLE = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);&lt;br /&gt;
objOLE.Fill(objDS);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The above code snippet works great, but you must know the name of the worksheet (Sheet1 is the default for Excel). But what if you don't know the Worksheet name? What if you just want the first sheet? Then, you must use the Excel objects to get it:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
1#region AnalyzeSpreadsheet&lt;br /&gt;
2public void AnalyzeSpreadsheet(string strFileName)&lt;br /&gt;
3{&lt;br /&gt;
4 //Excel variables&lt;br /&gt;
5 object con_true = true;&lt;br /&gt;
6 Excel.ApplicationClass objExcel = null;&lt;br /&gt;
7 Excel.Workbook objBook = null;&lt;br /&gt;
8 Excel.Worksheet objSheet = null; &lt;br /&gt;
9 try&lt;br /&gt;
10 {&lt;br /&gt;
11 //Create new instance of Excel Application&lt;br /&gt;
12 objExcel = new Excel.ApplicationClass();&lt;br /&gt;
13 //Set some options&lt;br /&gt;
14 objExcel.DisplayAlerts = false;&lt;br /&gt;
15 objExcel.ScreenUpdating = false;&lt;br /&gt;
16 objExcel.Visible = false;&lt;br /&gt;
17 objExcel.UserControl = false;&lt;br /&gt;
18 //Open spreadsheet&lt;br /&gt;
19 objBook = objExcel.Workbooks.Open(strFileName, Type.Missing,&lt;br /&gt;
20 Type.Missing, Type.Missing, Type.Missing, Type.Missing,&lt;br /&gt;
21 Type.Missing, Type.Missing, Type.Missing, Type.Missing,&lt;br /&gt;
22 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);&lt;br /&gt;
23 //Find the 1st worksheet&lt;br /&gt;
24 objSheet = (Excel.Worksheet)objBook.Worksheets.get_Item(1); &lt;br /&gt;
25 if (objSheet == null)&lt;br /&gt;
26 throw new Exception("Worksheet #1 not found!");&lt;br /&gt;
27 else&lt;br /&gt;
28 {&lt;br /&gt;
29 //Do something...&lt;br /&gt;
30 }&lt;br /&gt;
31 }&lt;br /&gt;
32 catch&lt;br /&gt;
33 {&lt;br /&gt;
34 //Handle exception&lt;br /&gt;
35 }&lt;br /&gt;
36 finally&lt;br /&gt;
37 {&lt;br /&gt;
38 ReleaseComObject(objSheet);&lt;br /&gt;
39 objSheet = null;&lt;br /&gt;
40 objBook.Close(con_true, strFileName, null);&lt;br /&gt;
41 ReleaseComObject(objBook);&lt;br /&gt;
42 objBook = null;&lt;br /&gt;
43 objExcel.Workbooks.Close();&lt;br /&gt;
44 objExcel.Application.Quit();&lt;br /&gt;
45 ReleaseComObject(objExcel);&lt;br /&gt;
46 objExcel = null;&lt;br /&gt;
47 }&lt;br /&gt;
48}&lt;br /&gt;
49#endregion&lt;br /&gt;
50#region ReleaseComObject&lt;br /&gt;
51private void ReleaseComObject(object o)&lt;br /&gt;
52{&lt;br /&gt;
53 Int32 i = 0;&lt;br /&gt;
54 Int32 j = 0;&lt;br /&gt;
55 try &lt;br /&gt;
56 {&lt;br /&gt;
57 for (i = 1; i &amp;lt;= System.Runtime.InteropServices.Marshal.ReleaseComObject(o); i++)&lt;br /&gt;
58 {&lt;br /&gt;
59 j = System.Runtime.InteropServices.Marshal.ReleaseComObject(o);&lt;br /&gt;
60 }&lt;br /&gt;
61 }&lt;br /&gt;
62 catch&lt;br /&gt;
63 {&lt;br /&gt;
64 }&lt;br /&gt;
65 finally&lt;br /&gt;
66 {&lt;br /&gt;
67 o = null;&lt;br /&gt;
68 }&lt;br /&gt;
69}&lt;br /&gt;
70#endregion&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-2002186912634411901?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/LiOrY3Zg9uu3Gg5kbcSGvFkNawg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/LiOrY3Zg9uu3Gg5kbcSGvFkNawg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/LiOrY3Zg9uu3Gg5kbcSGvFkNawg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/LiOrY3Zg9uu3Gg5kbcSGvFkNawg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/44Ye6xwbwQQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/2002186912634411901/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=2002186912634411901&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/2002186912634411901?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/2002186912634411901?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/44Ye6xwbwQQ/how-to-load-excel-spreadsheet-into.html" title="How to load an Excel spreadsheet into an ADO.NET DataSet" /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2007/09/how-to-load-excel-spreadsheet-into.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEYCQno5fCp7ImA9WB5VEUs.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-1967190756997217295</id><published>2007-08-03T13:31:00.000-05:00</published><updated>2007-08-03T13:36:03.424-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2007-08-03T13:36:03.424-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="JavaScript" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET" /><category scheme="http://www.blogger.com/atom/ns#" term="Code Samples" /><title>Catch Web Browser Closing/UnLoad Event</title><content type="html">There is a way to catch browser closing event. The most elegant way to catch browsing closing event in IE and FireFox is the following script:&lt;br /&gt;&lt;br /&gt;&amp;lt; html&amp;gt;&lt;br /&gt;&amp;lt; head&amp;gt;&lt;br /&gt;&amp;lt; title&amp;gt;&amp;lt; /title&amp;gt;&lt;br /&gt;&amp;lt; /head&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt; body onbeforeunload="alert('Closing');"&amp;gt;&lt;br /&gt;&amp;lt; /body&amp;gt;&lt;br /&gt;&amp;lt; /html&amp;gt;&lt;br /&gt;&lt;br /&gt;This Script works in IE 7 and in Firefox.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-1967190756997217295?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/qsgwpZRSh6EaQEb_ivGjV07kFfY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/qsgwpZRSh6EaQEb_ivGjV07kFfY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/qsgwpZRSh6EaQEb_ivGjV07kFfY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/qsgwpZRSh6EaQEb_ivGjV07kFfY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/rf-5JqR4R2o" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/1967190756997217295/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=1967190756997217295&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/1967190756997217295?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/1967190756997217295?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/rf-5JqR4R2o/there-is-way-to-catch-browser-closing.html" title="Catch Web Browser Closing/UnLoad Event" /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2007/08/there-is-way-to-catch-browser-closing.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkENSXo4fip7ImA9WB5XEE0.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-2462103579282243628</id><published>2007-07-09T12:02:00.000-05:00</published><updated>2007-07-09T12:04:58.436-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2007-07-09T12:04:58.436-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Code Samples" /><title>How to Get the IP address of the Host PC.</title><content type="html">This C# code snippet allows you to retrieve the IP address of the Host PC.&lt;br /&gt;&lt;br /&gt;using System.Net;&lt;br /&gt;......&lt;br /&gt;......&lt;br /&gt;......&lt;br /&gt;String strHostName  = Dns.GetHostName();&lt;br /&gt;IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);&lt;br /&gt;IPAddress[] addr    = ipEntry.AddressList;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-2462103579282243628?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/uwuxK3WpxMyffHAzKZV3c63s9v0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/uwuxK3WpxMyffHAzKZV3c63s9v0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/uwuxK3WpxMyffHAzKZV3c63s9v0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/uwuxK3WpxMyffHAzKZV3c63s9v0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/uKIwvA9UNeU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/2462103579282243628/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=2462103579282243628&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/2462103579282243628?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/2462103579282243628?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/uKIwvA9UNeU/how-to-get-ip-address-of-host-pc.html" title="How to Get the IP address of the Host PC." /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2007/07/how-to-get-ip-address-of-host-pc.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEYCSXkzcSp7ImA9WB5QFks.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-3264249909320594990</id><published>2007-07-05T15:00:00.000-05:00</published><updated>2007-07-05T15:09:28.789-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2007-07-05T15:09:28.789-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET" /><title>ERROR: Unable to start debugging on the web server. you do not have permission to debug the application. the URL for this project is in the Internet..</title><content type="html">If you are running .NET Framework 1.1 and 2.0 together on the same machine, you might get this error when you try to run .NET 1.1 web application in debug mode:&lt;br /&gt;&lt;br /&gt;"Unable to start debugging on the web server. you do not have permission to debug the application. the URL for this project is in the Internet zone."&lt;br /&gt;&lt;br /&gt;It's easy to fix, just follow these steps:&lt;br /&gt;1. Go to "Administrative Tools" and open "Internet Information Services(IIS)" .&lt;br /&gt;2. Right click on "Default Web Site" and choose "Properties" option.&lt;br /&gt;3. Select ASP.NET tab.&lt;br /&gt;4. Look for ASP.NET version drop down and Change ASP.NET version from 2.0.xxx to 1.1.xxx.&lt;br /&gt;5. Click Apply button.&lt;br /&gt;6. Click Ok button.&lt;br /&gt;7. Restart your IIS.&lt;br /&gt;&lt;br /&gt;Try now opening your web project, there should not be a problem.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-3264249909320594990?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/OFKEvG7tcW65rBna01wcaO_wrf8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/OFKEvG7tcW65rBna01wcaO_wrf8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/OFKEvG7tcW65rBna01wcaO_wrf8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/OFKEvG7tcW65rBna01wcaO_wrf8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/KjaFmZ5bN6E" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/3264249909320594990/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=3264249909320594990&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/3264249909320594990?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/3264249909320594990?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/KjaFmZ5bN6E/error-unable-to-start-debugging-on-web.html" title="ERROR: Unable to start debugging on the web server. you do not have permission to debug the application. the URL for this project is in the Internet.." /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2007/07/error-unable-to-start-debugging-on-web.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0cGSXg6fCp7ImA9WB5RFEU.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-2341171211494724505</id><published>2007-06-21T22:50:00.000-05:00</published><updated>2007-06-21T23:03:48.614-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2007-06-21T23:03:48.614-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ATLAS" /><category scheme="http://www.blogger.com/atom/ns#" term="AJAX" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET" /><category scheme="http://www.blogger.com/atom/ns#" term="Code Samples" /><title>Drag and Drop ASP.NET 2.0 Web Parts in Firefox with ATLAS</title><content type="html">This Article is for the users who are still using ATLAS and have not yet moved to AJAX. The known issue in ATLAS is that there is no direct drag and drop support for FireFox and Netscape Browsers, in this Article I have explained how you can make it possible for Firefox browser.&lt;br /&gt;&lt;br /&gt;To add this functionality to your existing pages, do the following: &lt;br /&gt;&lt;br /&gt;1. Copy the "Atlas" run-time assembly (Microsoft.Web.Atlas.dll) from its installation folder to the application's Bin folder. &lt;br /&gt;&lt;br /&gt;2. Copy these elements as children of the &lt;configuration&gt; element: &lt;br /&gt;  &amp;lt;!-- ASP.NET Atlas support--&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;configSections&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;sectionGroup name="microsoft.web" type="Microsoft.Web.Configuration.MicrosoftWebSectionGroup"&amp;gt;&lt;br /&gt;&lt;br /&gt;      &amp;lt;section name="converters" type="Microsoft.Web.Configuration.ConvertersSection"/&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;/sectionGroup&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;/configSections&amp;gt;&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;  &amp;lt;!-- Items required for the Atlas framework --&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;microsoft.web&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;converters&amp;gt;&lt;br /&gt;&lt;br /&gt;      &amp;lt;add type="Microsoft.Web.Script.Serialization.Converters.DataSetConverter"/&amp;gt;&lt;br /&gt;&lt;br /&gt;      &amp;lt;add type="Microsoft.Web.Script.Serialization.Converters.DataRowConverter"/&amp;gt;&lt;br /&gt;&lt;br /&gt;      &amp;lt;add type="Microsoft.Web.Script.Serialization.Converters.DataTableConverter"/&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;/converters&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;/microsoft.web&amp;gt; &lt;br /&gt;&lt;br /&gt;3. Copy these elements as children of the &amp;lt;system.web&amp;gt; element: &lt;br /&gt;    &amp;lt;!-- Atlas support --&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;pages&amp;gt;&lt;br /&gt;&lt;br /&gt;      &amp;lt;controls&amp;gt;&lt;br /&gt;&lt;br /&gt;        &amp;lt;add namespace="Microsoft.Web.UI" assembly="Microsoft.Web.Atlas" tagPrefix="atlas"/&amp;gt;&lt;br /&gt;&lt;br /&gt;        &amp;lt;add namespace="Microsoft.Web.UI.Controls" assembly="Microsoft.Web.Atlas" tagprefix="atlas"/&amp;gt; &lt;br /&gt;&lt;br /&gt;      &amp;lt;/controls&amp;gt;&lt;br /&gt;&lt;br /&gt;      &amp;lt;tagMapping&amp;gt;&lt;br /&gt;&lt;br /&gt;        &amp;lt;add tagType="System.Web.UI.WebControls.WebParts.WebPartManager"&lt;br /&gt;&lt;br /&gt;            mappedTagType="Microsoft.Web.UI.Controls.WebParts.WebPartManager"/&amp;gt;&lt;br /&gt;&lt;br /&gt;        &amp;lt;add tagType="System.Web.UI.WebControls.WebParts.WebPartZone"&lt;br /&gt;&lt;br /&gt;           mappedTagType="Microsoft.Web.UI.Controls.WebParts.WebPartZone"/&amp;gt;&lt;br /&gt;&lt;br /&gt;      &amp;lt;/tagMapping&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;/pages&amp;gt;&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;4. &amp;lt;!-- ASMX is mapped to a new handler so that proxy javascripts can also be served. --&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;httpHandlers&amp;gt;&lt;br /&gt;&lt;br /&gt;      &amp;lt;remove verb="*" path="*.asmx"/&amp;gt;&lt;br /&gt;&lt;br /&gt;      &amp;lt;add verb="*" path="*.asmx" type="Microsoft.Web.Services.ScriptHandlerFactory" validate="false"/&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;/httpHandlers&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;httpModules&amp;gt;&lt;br /&gt;&lt;br /&gt;      &amp;lt;add name="ScriptModule" type="Microsoft.Web.Services.ScriptModule"/&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;/httpModules&amp;gt; &lt;br /&gt;&lt;br /&gt;5. Add the Atlas ScriptManager control to the page: &lt;br /&gt;&amp;lt;atlas:ScriptManager ID="ScriptManager1" runat="server" /&amp;gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-2341171211494724505?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/nVyIOosHfy8Gg4sYBVpdNaD6p0Y/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/nVyIOosHfy8Gg4sYBVpdNaD6p0Y/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/nVyIOosHfy8Gg4sYBVpdNaD6p0Y/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/nVyIOosHfy8Gg4sYBVpdNaD6p0Y/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/nmZxxajsc0M" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/2341171211494724505/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=2341171211494724505&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/2341171211494724505?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/2341171211494724505?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/nmZxxajsc0M/drag-and-drop-aspnet-20-web-parts-in.html" title="Drag and Drop ASP.NET 2.0 Web Parts in Firefox with ATLAS" /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2007/06/drag-and-drop-aspnet-20-web-parts-in.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A08BSXg5eCp7ImA9WB5RE0g.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-8768377487044021466</id><published>2007-06-12T12:13:00.000-05:00</published><updated>2007-06-20T12:17:38.620-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2007-06-20T12:17:38.620-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Thoughts" /><category scheme="http://www.blogger.com/atom/ns#" term="Good Articles to Read" /><category scheme="http://www.blogger.com/atom/ns#" term="Code Samples" /><title>What is Digg?</title><content type="html">&lt;span style="font-size:85%;"&gt;Digg is a user driven social content website. Ok, so what the heck does that mean? Well, everything on Digg is submitted by our community (that would be you). After you submit content, other people read your submission and Digg what they like best. If your story rocks and receives enough Diggs, it is promoted to the front page for the millions of visitors to see.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;What can you do as a Digg user? Lots. Every person can digg (help promote), bury (help remove spam), and comment on stories... you can even Digg and bury comments you like or dislike. Digg also allows you to track your friends' activity throughout the site — want to share a video or news story with a friend? Digg it!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;Want to know more how the site works? Read &lt;/span&gt;&lt;a href="http://www.digg.com/how"&gt;&lt;span style="font-size:85%;"&gt;How Digg Works&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;if you decide to post a link on Digg, here's the snippet of code you need to add to your website to add the Digg Link to your website:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span&gt;&lt;br /&gt;&amp;lt;script type="text/javascript"&amp;gt;&lt;br /&gt;    digg_url = "digg url to your link";&lt;br /&gt;&amp;lt;script&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;script src="http://digg.com/tools/diggthis.js" type="text/javascript"&gt;&lt;/script&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-8768377487044021466?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/dpN6uwzrEQJw3-kRIB2Ev3TPAsU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/dpN6uwzrEQJw3-kRIB2Ev3TPAsU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/dpN6uwzrEQJw3-kRIB2Ev3TPAsU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/dpN6uwzrEQJw3-kRIB2Ev3TPAsU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/BEaWybxLdRQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/8768377487044021466/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=8768377487044021466&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/8768377487044021466?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/8768377487044021466?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/BEaWybxLdRQ/what-is-digg.html" title="What is Digg?" /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2007/06/what-is-digg.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0IBSXk-cCp7ImA9WB5SEEo.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-5505023663123963648</id><published>2007-06-04T15:53:00.000-05:00</published><updated>2007-06-05T16:39:18.758-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2007-06-05T16:39:18.758-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="AJAX" /><title>What is AJAX?</title><content type="html">&lt;span style="font-size:85%;"&gt;If you're not familiar with AJAX, here's the &lt;/span&gt;&lt;a href="http://en.wikipedia.org/wiki/AJAX" target="_blank"&gt;&lt;span style="font-size:85%;"&gt;Wikipedia definition&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size:85%;"&gt;.&lt;br /&gt;&lt;br /&gt;ASP.NET AJAX is a new Web development technology from Microsoft that integrates cross-browser script libraries with the ASP.NET 2.0 Web application framework. With ASP.NET AJAX, developers can quickly create pages with sophisticated, responsive user interfaces and more efficient client-server communication by simply adding a few server controls to their pages. &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-5505023663123963648?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/22XNp5eY_ql6Tmb2Ry2UbSmCjOY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/22XNp5eY_ql6Tmb2Ry2UbSmCjOY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/22XNp5eY_ql6Tmb2Ry2UbSmCjOY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/22XNp5eY_ql6Tmb2Ry2UbSmCjOY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/ttRuNEyZ9Z4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/5505023663123963648/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=5505023663123963648&amp;isPopup=true" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/5505023663123963648?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/5505023663123963648?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/ttRuNEyZ9Z4/what-is-ajax.html" title="What is AJAX?" /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>1</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2007/06/what-is-ajax.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0INRX46cSp7ImA9WB5SEEo.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-7206194771150550162</id><published>2007-06-04T15:49:00.000-05:00</published><updated>2007-06-05T16:39:54.019-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2007-06-05T16:39:54.019-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="AJAX" /><title>Create AJAX Animated GIF</title><content type="html">&lt;span style="font-size:85%;"&gt;If you're not familiar with AJAX, here's the &lt;/span&gt;&lt;a href="http://en.wikipedia.org/wiki/AJAX" target="_blank"&gt;&lt;span style="font-size:85%;"&gt;Wikipedia definition&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size:85%;"&gt;.&lt;br /&gt;&lt;br /&gt;Basically, it's the process of updating small portions of a webpage instead of refreshing the entire page. This presents a much more enjoyable user experience and speeds up the website. When a portion of a webpage is being updated, you see an animated image indicating so. While working on AJAX enabled websites, I found the need to create custom animated images. These are not easy to create...until now.&lt;br /&gt;&lt;br /&gt;Visit the following link and you can create your own animated AJAX image in seconds! &lt;/span&gt;&lt;a href="http://www.ajaxload.info" target="_blank"&gt;&lt;span style="font-size:85%;"&gt;http://www.ajaxload.info&lt;/span&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-7206194771150550162?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/LeNcYvkN6jsCKiYladK6Z9YZdHU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/LeNcYvkN6jsCKiYladK6Z9YZdHU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/LeNcYvkN6jsCKiYladK6Z9YZdHU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/LeNcYvkN6jsCKiYladK6Z9YZdHU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/E13GJ2VB2hs" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/7206194771150550162/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=7206194771150550162&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/7206194771150550162?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/7206194771150550162?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/E13GJ2VB2hs/create-ajax-animated-gif.html" title="Create AJAX Animated GIF" /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2007/06/create-ajax-animated-gif.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0MERH85eip7ImA9WB5TGE4.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-647226028021690690</id><published>2007-06-02T21:53:00.000-05:00</published><updated>2007-06-02T21:56:45.122-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2007-06-02T21:56:45.122-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Thoughts" /><category scheme="http://www.blogger.com/atom/ns#" term="Good Articles to Read" /><category scheme="http://www.blogger.com/atom/ns#" term="Project Management" /><title>10 Reasons That Projects Fail (And How to Avoid Them)</title><content type="html">&lt;span style="font-size:85%;"&gt;courtsey: &lt;/span&gt;&lt;a href="http://www.certmag.com/"&gt;&lt;span style="font-size:85%;"&gt;www.certmag.com&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;October 2005 - David Garrett&lt;br /&gt; &lt;br /&gt;It’s called the Chaos Report, and you need to know about it.&lt;br /&gt;&lt;br /&gt;Every year, The Standish Group (&lt;/span&gt;&lt;a href="http://www.standishgroup.com/" target="_blank"&gt;&lt;span style="font-size:85%;"&gt;www.standishgroup.com&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size:85%;"&gt;), which tracks the success rates of IT projects, issues this report to an eager crowd of analysts, managers and techies who wonder why so many IT projects end in failure. And while the numbers vary by year, they’re often dismal: A recent Chaos Report claimed that “unqualified project successes” are well below 50 percent, and sometimes as low as a third of all projects tracked. Even worse, out-and-out failures, which are defined as projects that are abandoned midstream, can reach as high as 15 percent. Projects that simply have time problems, cost problems or poor execution can make up as much as half of all IT projects per year.&lt;br /&gt;These numbers are bad news. Now more than ever, the world depends on IT—but IT and the people who run it don’t always have a dependable record. This leads to the simple but vexing question of why projects fail—and just as important, what you can do about it.&lt;br /&gt;Projects fail for all kinds of reasons, from insanely low budgets to unwisely tight schedules. But some reasons pop up again and again, everywhere from mom-and-pop shops to the Fortune 500, so they’re listed here, along with advice on how to avoid them.&lt;br /&gt;Scope, Scope and More Scope Far and away, scope issues are the number-one reason that projects fail. “ Scope” is simply the project manager’s term for the sum of work to be done on a project—what it entails, what it doesn’t and what its end products are. All too often, projects that fail have poorly defined scopes.&lt;br /&gt;Here’s an example: You’re told to build a simple contact management system for your corporate intranet, and you’re given a list of features, a budget and a basic timeline. You build the system to spec, but as you’re building it, your users, impressed with your work, think of new features to add. It’s called “scope creep”—the insidious way that a project’s end-products get larger by degrees. Three weeks into the project, you find yourself bogged down by details that no one ever mentioned at the start. As a result, you miss your deadline, get slapped on the wrists and buy a large bottle of Jack Daniels.&lt;br /&gt;Solution: Write a scope document—a list of what you will and won’t do by a project’s end. And if you’re really worried about scope creep, write an out-of-scope document, in which you explicitly state what you won’t add, emend or deliver. Then enforce it like the law.&lt;br /&gt;Egos (Big Ones) You won’t find this reason on an official list of project pitfalls, but it’s one of the chief reasons for project failure. People have egos, and all too often they’re loath to swallow their pride, admit they’re wrong and move on. Feelings get bruised and people get mad, and as a result, projects get left undone.&lt;br /&gt;Solution: Remember that a project manager is always a diplomat. True, there are some projects that demand a diplomat of Kofi Annan’s caliber, but even a small amount of courtesy, compromise and common sense can go a long way toward getting things done when politics run amok.&lt;br /&gt;Bad Language This doesn’t mean curse words. It means poor communication. All too often, we speak and write in ways that don’t make things clear. Or we simply don’t speak or write enough in the first place.&lt;br /&gt;Remember that taking a project from coal to diamonds involves keeping all the miners in the loop. You need to have a clear statement of mission, a well-defined scope, daily or weekly status reports and enough meetings to keep the team organized (but not so many that it steals their time). You also need a simple and direct way to learn about problems as soon as they occur. Without it, you’re flying blind—and your project is bound for failure.&lt;br /&gt;Solution: If you’re doing a large project, write out a brief communications plan. Who needs to be kept in the know? Who gets copied on e-mail? When will you meet, and why? Is there a chain of command? And so on. Pin down these answers before you get started to avoid problems at the end.&lt;br /&gt;Bad Budgets This one’s easy: Too many projects don’t have the money they need to succeed. Bootstrapping is a part of life, but a miserly budget can kill a project before it even begins.  &lt;br /&gt;Solution: Make sure you know what a project will cost before you start it—the best way is to write a line-item budget—and factor in a safety cushion for cost overruns. Then fight for your budget with the bean counters and decision-makers who control the purse strings. A few extra dollars can mean the difference between a glowing success and something that people whisper about behind closed doors.&lt;br /&gt;Forgotten Users Too many IT projects commit this sin in spades. Remember that an IT project is built for the sake of its users, and not for the people who built it. All too often, the coders, engineers and network administrators who run a project speak with users at two points in the process only: the beginning, when they ask what users want, and the end, when they present the goods.&lt;br /&gt;The problem, of course, is that users don’t always know what they want until they see it, or they don’t explain what they want very clearly. If you wait until a project is over to show users the results, you’re simply asking for trouble.&lt;br /&gt;Solution: Involve your users at every phase of the project. If you’re building a Web site, get them to sign off on the site map. Get their input on every color, design and word of copy you write. Ask for their opinions weekly, if not more often, and you’ll have a better chance for a happy ending.&lt;br /&gt;The Missing Bigwig Projects are merely the people who run them. Whether or not a project works depends on the team that’s assembled to drive it, and that makes politics and human nature an inherent part of project management.&lt;br /&gt;Since people respond to authority, you need a good project sponsor: an executive with the power and clout to force people to work and free up the dollars your project needs. Some call this “ management buy-in.” Some call it “ management by force.” But whatever you call it, make sure you do it—a project that’s sponsored by a high-end executive is a project that’s less apt to fail.&lt;br /&gt;Solution: Don’t be afraid to ask an executive to chair your project. If his name is tied to the project’s success, he’s more likely to exert the effort that’s needed (and perhaps knock some heads together) to get things done in a pinch.&lt;br /&gt;Lack of Planning It seems easy, but it’s a lesson of such fiendish complexity that people forget it all the time: If you want your project to succeed, plan it thoroughly.&lt;br /&gt;Remember that planning is both art and science, and entails more than a simple calendar. There’s risk management, change management, work breakdowns, documentation and more. So be sure to write up your plan in as much detail as possible. (The mere act of writing makes you view things in a light you may have ignored.) And bear in mind that the days of “cowboy coding,” in which you get to work as soon as you get a concept, are over.&lt;br /&gt;Solution: Write out a project charter, a mission, a scope statement and a communications plan. Invest in a few good project management books for tips and advice on the lost art of planning and how to do it right.&lt;br /&gt;Good Old Force Majeure Of course, not every project fails because of bad execution. There are things beyond our control—the economy, for instance, or simply a flood that wipes out your data center at two in the morning.&lt;br /&gt;Force majeure is the legal term for an act that’s beyond a person’s control. Some call these acts of God. Some people blame the stars. Whatever the reason, it’s fair to say that some projects fail because of calamities beyond our control.  &lt;br /&gt;Solution: While you can’t walk on water and stop floods in their tracks, you can engage in a bit of risk planning to minimize your exposure to force majeure. Think of the disasters that could strike when you’re least ready for them, then make arrangements now. Increase your backups, build mirrors and redundant systems, write out a full-scale disaster recovery or business continuity plan. You’ll be glad you have one.&lt;br /&gt;Dumb People This could be said more gently. It could even be said more subtly. But why bother? We all know that projects fail because some of the people who run them aren’t as swift (or merely as focused) as they should be.&lt;br /&gt;Of course, we’re not immune from this ourselves. Remember that patience—that intangible asset that’s in smaller supply every day—is often a project’s best chance for success.&lt;br /&gt;Solution: Choose your team wisely. Find the best people you can with the money you have and the choices you’re given. And invest in training as if it were a blue-chip stock: Choose it wisely, and it does nothing but increase your value.&lt;br /&gt;The Technician-Manager Last but not least, don’t forget that good technicians don’t always make good managers—or good project mangers, for that matter.&lt;br /&gt;You may be a dazzling writer of database systems. You may even know the rigors of ASP and PHP like some people know their ABCs. But can you pull off a project on time?&lt;br /&gt;Remember that good management, and good project management, take a subtle mix of people, planning and communication skills. It takes a diplomat’s touch and an expert’s knowledge of the field. And it’s a skill that few of us are born with. The fact that you’re a whiz with event handlers or ActionScript doesn’t mean you can make the trains run on time—though you can learn to, and even excel at it with effort.&lt;br /&gt;Last, remember that “project failure” and “project death” are very different things. In the end, failure is merely delay, not defeat, and if you take these lessons to heart and watch your projects like a hawk, you’ll find that success is closer than you think.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-647226028021690690?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/l2ADZ2SbtgAL_zXvRiXDvnoFGfU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/l2ADZ2SbtgAL_zXvRiXDvnoFGfU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/l2ADZ2SbtgAL_zXvRiXDvnoFGfU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/l2ADZ2SbtgAL_zXvRiXDvnoFGfU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/TndfNjIOENk" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/647226028021690690/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=647226028021690690&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/647226028021690690?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/647226028021690690?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/TndfNjIOENk/10-reasons-that-projects-fail-and-how.html" title="10 Reasons That Projects Fail (And How to Avoid Them)" /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2007/06/10-reasons-that-projects-fail-and-how.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Ck4GQX4zeyp7ImA9WB5TFUs.&quot;"><id>tag:blogger.com,1999:blog-8272135192268931542.post-4884803007106048818</id><published>2007-05-30T15:20:00.000-05:00</published><updated>2007-05-30T16:35:20.083-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2007-05-30T16:35:20.083-05:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Code Samples" /><title>Send Email using ASP.NET 2.0</title><content type="html">&lt;span style="font-size:85%;"&gt;&lt;span style="color:#6666cc;"&gt;&lt;strong&gt;Introduction&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;A common thing you do with Web Development is to send emails, I will show you here how to send emails using ASP.Net v 2.0, and also using the authentication for a SMTP server.&lt;br /&gt;&lt;br /&gt;The namespace for sending mail within ASP.Net v 2.0 is not System.Web.Mail anymore this is replaced by System.Net.Mail&lt;br /&gt;&lt;br /&gt;This article explains how to use System.Net.Mail namespace to send emails.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#6666cc;"&gt;Technical Details:&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;The Send mail functionality is similar to Dotnet 1.1 except for few changes.&lt;br /&gt;&lt;br /&gt;1.  System.Net.Mail.SmtpClient is used instead of System.Web.Mail.SmtpMail (obsolete in Dotnet 2.0).&lt;br /&gt;2.  System.Net.MailMessage Class is used instead of System.Web.Mail.MailMessage (obsolete in Dotnet 2.0)&lt;br /&gt;3.  The System.Net.MailMessage class collects From address as MailAddress object.&lt;br /&gt;4.  The System.Net.MailMessage class collects To, CC, Bcc addresses as MailAddressCollection.&lt;br /&gt;5.  MailMessage Body Format is replaced by IsBodyHtml &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;color:#6666cc;"&gt;&lt;strong&gt;Code Sample:&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;using System;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Web;&lt;br /&gt;using System.Web.Security;&lt;br /&gt;using System.Web.UI;&lt;br /&gt;using System.Web.UI.WebControls;&lt;br /&gt;using System.Web.UI.HtmlControls;&lt;br /&gt;using System.Net.Mail;&lt;br /&gt;&lt;br /&gt;public partial class _Default : System.Web.UI.Page&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;#region "Send email"&lt;br /&gt;&lt;br /&gt;    protected void btnSendmail_Click(object sender, EventArgs e)&lt;br /&gt;   {&lt;br /&gt;&lt;br /&gt;      // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0&lt;br /&gt;      // System.Net.Mail.SmtpClient is the alternate class for this in 2.0&lt;br /&gt;      SmtpClient smtpClient = new SmtpClient();&lt;br /&gt;      MailMessage message = new MailMessage();&lt;br /&gt;      try&lt;br /&gt;        {&lt;br /&gt;            MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);&lt;br /&gt;         &lt;br /&gt;          // You can specify the host name or ipaddress of your server&lt;br /&gt;          // Default in IIS will be localhost&lt;br /&gt;          smtpClient.Host = "localhost";&lt;br /&gt;   &lt;br /&gt;         //Default port will be 25&lt;br /&gt;          smtpClient.Port = 25;&lt;br /&gt;&lt;br /&gt;        //From address will be given as a MailAddress Object&lt;br /&gt;        message.From = fromAddress;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;        // To address collection of MailAddress&lt;br /&gt;        message.To.Add("admin1@yoursite.com");&lt;br /&gt;        message.Subject = "Feedback";&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;        // CC and BCC optional&lt;br /&gt;       // MailAddressCollection class is used to send the email to various users&lt;br /&gt;       // You can specify Address as new MailAddress(&lt;/span&gt;&lt;a href="mailto:admin1@yourdomain.com"&gt;&lt;span style="font-size:85%;"&gt;admin1@yourdomain.com&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size:85%;"&gt;)&lt;br /&gt;       message.CC.Add("admin1@yourdomain.com");&lt;br /&gt;       message.CC.Add("&lt;a href="mailto:admin2@yourdomain.com"&gt;admin2@yourdomain.com&lt;/a&gt;");&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;       // You can specify Address directly as string&lt;br /&gt;       message.Bcc.Add(new MailAddress("admin3@yourdomain.com"));&lt;br /&gt;       message.Bcc.Add(new MailAddress("&lt;/span&gt;&lt;a href="mailto:admin4@yoursite.com"&gt;&lt;span style="font-size:85%;"&gt;admin4@yourdomain.com&lt;/span&gt;&lt;/a&gt;"&lt;span style="font-size:85%;"&gt;));&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;       //Body can be Html or text format&lt;br /&gt;       //Specify true if it is html message&lt;br /&gt;       message.IsBodyHtml = false;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;       // Message body content&lt;br /&gt;       message.Body = txtMessage.Text;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;       // Send SMTP mail&lt;br /&gt;       smtpClient.Send(message);&lt;br /&gt;       lblStatus.Text = "Email successfully sent.";&lt;br /&gt;    }&lt;br /&gt;   catch (Exception ex)&lt;br /&gt;   {&lt;br /&gt;           lblStatus.Text = "Send Email Failed.&lt;br /&gt;" + ex.Message;&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;#endregion&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;}&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8272135192268931542-4884803007106048818?l=architectdotnet.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/8-1_nmfEy_Y3wIiUWx2M0XlaR4c/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/8-1_nmfEy_Y3wIiUWx2M0XlaR4c/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/8-1_nmfEy_Y3wIiUWx2M0XlaR4c/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/8-1_nmfEy_Y3wIiUWx2M0XlaR4c/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/SanjeevHasizasBlog-netarchitect/~4/JKkJ9CzJKtM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://architectdotnet.blogspot.com/feeds/4884803007106048818/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8272135192268931542&amp;postID=4884803007106048818&amp;isPopup=true" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/4884803007106048818?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8272135192268931542/posts/default/4884803007106048818?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/SanjeevHasizasBlog-netarchitect/~3/JKkJ9CzJKtM/send-email-using-aspnet-20.html" title="Send Email using ASP.NET 2.0" /><author><name>Sanjeev Hasiza a .NETArchitect</name><uri>http://www.blogger.com/profile/10333682860610766373</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://3.bp.blogspot.com/_vC4V9dUCDH0/S3BlnFDcsNI/AAAAAAAAA9M/tOpEQK5ptvw/S220/sanj.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://architectdotnet.blogspot.com/2007/05/send-email-using-aspnet-20.html</feedburner:origLink></entry></feed>

