<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users663</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/663" rel="self"/>
  <title>GateKiller</title>
  <updated>Thu Mar 05 15:08:03 -0800 2009</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code777</id>
    <published>2009-03-05T15:08:03-08:00</published>
    <updated>2010-07-02T21:59:27-07:00</updated>
    <title>[C#] C# HTML Encoding</title>
    <content type="html">&lt;p&gt;This is my own attempt at writing a HTML Encoder for ASP.NET.&lt;/p&gt;

&lt;p&gt;C&amp;amp;C Welcome :)&lt;/p&gt;

&lt;pre&gt;public string CleanHtml(object Html) {
	var s = Html.ToString();
	var b = new StringBuilder();
	foreach (char c in s) {
		var l = b.Length;
		
		// Known Replacements
		if (c == '&amp;amp;') {
			b.Append(&amp;quot;&amp;amp;amp;&amp;quot;);
		}
		if (c == '&amp;lt;') {
			b.Append(&amp;quot;&amp;amp;lt;&amp;quot;);
		}
		if (c == '&amp;gt;') {
			b.Append(&amp;quot;&amp;amp;gt;&amp;quot;);
		}
		if (c == '`') {
			b.Append(&amp;quot;'&amp;quot;);
		}
		
		// Always Encode Extended Ascii Codes
		if ((int)(c) &amp;gt; 127) {
			b.Append(&amp;quot;&amp;amp;#&amp;quot; + (int)(c) + &amp;quot;;&amp;quot;);
		}
		
		// If nothing was added, add the original char
		if (l == b.Length) {
			b.Append(c);
		}
	}
	return b.ToString();
}&lt;/pre&gt;</content>
    <author>
      <name>GateKiller</name>
      <email>refactormycode@gatekiller.co.uk</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/777-c-html-encoding" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code600</id>
    <published>2008-11-11T17:10:30-08:00</published>
    <updated>2008-11-11T23:34:42-08:00</updated>
    <title>[C#] DirectoryInfo.CopyTo</title>
    <content type="html">&lt;p&gt;Because the .NET Framework does not contain a function which allows you to copy a directory from one location to another, I thought I would write one:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://gatekiller.co.uk/Post/DirectoryInfo.CopyTo" target="_blank"&gt;http://gatekiller.co.uk/Post/DirectoryInfo.CopyTo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Please let me know what you think. Any refactorings to make the code more elegant would be much appreciated.&lt;/p&gt;

&lt;p&gt;Cheers
&lt;br /&gt;Stephen&lt;/p&gt;

&lt;pre&gt;public static class DirectoryInfoExtensions
{
    public static void CopyTo(this String Source, String Destination, Boolean Overwrite)
    {
                // Hold directory information
                var SourceDirectory = new DirectoryInfo(Source);
                var DestinationDirectory = new DirectoryInfo(Destination);
                
                // Throw an error is the source directory does not exist
                if (SourceDirectory.Exists == false) {
                        throw new DirectoryNotFoundException();
                }
                
                // Create the destination directory
                if (DestinationDirectory.Exists == false) {
                        DestinationDirectory.Create();
                }
                
                // Loop through the files and copy them
                var SubFiles = SourceDirectory.GetFiles();
                for (int i = 0; i &amp;lt; SubFiles.Length; i++) {
                        var NewFile = Path.Combine(
                                DestinationDirectory.FullName,
                                SubFiles[i].Name
                        );
                        SubFiles[i].CopyTo(NewFile, Overwrite);
                }
                
                // Loop through the directories and call this function
                var SubDirectories = SourceDirectory.GetDirectories();
                for (int i = 0; i &amp;lt; SubDirectories.Length; i++) {
                        var NewDirectory = Path.Combine(
                                DestinationDirectory.FullName,
                                SubDirectories[i].Name
                        );
                        SubDirectories[i].CopyTo(NewDirectory, Overwrite);
                }
        }
}

var SourceDirectory = new DirectoryInfo(@&amp;quot;C:\Users\&amp;quot;);
SourceDirectory.CopyTo(@&amp;quot;C:\Users_Backup\&amp;quot;, true);&lt;/pre&gt;</content>
    <author>
      <name>GateKiller</name>
      <email>refactormycode@gatekiller.co.uk</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/600-directoryinfo-copyto" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor53073</id>
    <published>2008-10-07T11:55:54-07:00</published>
    <title>[C#] On Drop all Indexes in SQL Server</title>
    <content type="html">&lt;p&gt;Rik,&lt;/p&gt;

&lt;p&gt;I knew sooner or later someone would ask that question :)&lt;/p&gt;

&lt;p&gt;I'm working on producing some complex reports using a 3rd party database. Using the databases current indexes, which I cannot change, the SQL statement takes about 20hours to complete. If copy the database and replace the indexes with my own, it takes about 2 minutes. So I was looking for a quick way to remove the original index quickly so I could insert my own.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>GateKiller</name>
      <email>refactormycode@gatekiller.co.uk</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/522-drop-all-indexes-in-sql-server/refactors/53073" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code522</id>
    <published>2008-10-07T10:48:07-07:00</published>
    <updated>2010-07-02T22:03:08-07:00</updated>
    <title>[C#] Drop all Indexes in SQL Server</title>
    <content type="html">&lt;p&gt;The below is some code I recently wrote that attempts to delete all indexes in the current database. It's my first attempt and it's not a very elegant solution so I'm open to suggestions on how I can improve the code.&lt;/p&gt;

&lt;p&gt;Thanks&lt;/p&gt;

&lt;pre&gt;## Transact-SQL [sql]

Declare @Index varchar(128)
Declare @Table varchar(128)

Select
SysIndexes.Name As 'Index',
SysObjects.Name As 'Table'
Into
#Indexes
From
SysIndexes
Inner Join SysObjects On
	SysObjects.id = SysIndexes.id
Where
SysIndexes.Name Is Not Null
and SysObjects.XType = 'U'
Order By
SysIndexes.Name,
SysObjects.Name

While (Select Count(*) From #Indexes) &amp;gt; 0
Begin
	Set @Index = (Select Top 1 [Index] From #Indexes)
	Set @Table = (Select Top 1 [Table] From #Indexes)

	--Print 'Drop Index [' + @Index + '] On [' + @Table + ']' + Char(13)
	Exec ('Drop Index [' + @Index + '] On [' + @Table + ']')
	Delete From #Indexes Where [Index] = @Index and [Table] = @Table
End

Drop Table #Indexes
&lt;/pre&gt;</content>
    <author>
      <name>GateKiller</name>
      <email>refactormycode@gatekiller.co.uk</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/522-drop-all-indexes-in-sql-server" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor28448</id>
    <published>2008-09-22T11:59:22-07:00</published>
    <title>[C#] On Backup All MS SQL Server Databases</title>
    <content type="html">&lt;p&gt;@Moodshield: Love it! It's a very simple solution that will suit my server which only has about a dozen databases.&lt;/p&gt;

&lt;p&gt;Many Thanks,
&lt;br /&gt;Stephen&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>GateKiller</name>
      <email>refactormycode@gatekiller.co.uk</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/497-backup-all-ms-sql-server-databases/refactors/28448" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code497</id>
    <published>2008-09-19T15:19:54-07:00</published>
    <updated>2010-07-02T22:03:12-07:00</updated>
    <title>[C#] Backup All MS SQL Server Databases</title>
    <content type="html">&lt;p&gt;I've recently blogged about this script and it seems to work very well. Does anyone has any improvements/suggestions or is it nearly perfect?&lt;/p&gt;

&lt;pre&gt;## SQL Statement [sql]
Use Master

Declare @DatabaseName sysname
Declare @SQLCommand varchar(1024)
Declare curDBName Cursor For
Select [Name] From Master..Sysdatabases
Where [Name] Not In ('tempdb')

Open curDBName
Fetch curDBName Into @DatabaseName

While (@@fetch_status = 0)

Begin
        if databasepropertyex (@DatabaseName,'Status') = 'online'
        Begin
                Select @SQLCommand = 'Backup Database ' + @DatabaseName + 
                ' To Disk = ''D:\Backups\Databases\' + @DatabaseName + '.bak'' With Format'
                execute (@SQLCommand)
        End
        Fetch curDBName Into @DatabaseName
End

Close curDBName
Deallocate curDBName&lt;/pre&gt;</content>
    <author>
      <name>GateKiller</name>
      <email>refactormycode@gatekiller.co.uk</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/497-backup-all-ms-sql-server-databases" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor13783</id>
    <published>2008-07-25T10:50:33-07:00</published>
    <title>[C#] On Cache SQL Server Views</title>
    <content type="html">&lt;p&gt;Just a couple of more changes. Added square brakets around table name and moved the Set @Name to the start of the loop as it was failing on the first table.&lt;/p&gt;

&lt;pre&gt;## SQL Statement [sql]
Declare @Name varchar(128)
Declare @Table varchar(128)

Select Name Into #Views From SysObjects Where XType = 'V'

While (Select Count(*) From #Views) &amp;gt; 0
Begin
	Set @Name = (Select top 1 Name From #Views)
	Set @Table = 'c' + SubString(@Name, 2, 128)
	If Exists(Select Name From SysObjects Where Name = @Table)
	Begin
		Exec('Drop Table [' + @Table + '] Select * Into [##TempViews] From [' + @Name + '] Select * Into [' + @Table + '] From [##TempViews] Drop Table [##TempViews]')
	End
	Else
	Begin
		Exec('Select * Into [##TempViews] From [' + @Name + '] Select * Into [' + @Table + '] From [##TempViews] Drop Table [##TempViews]')
	End
	
	Delete From #Views Where Name = @Name
End

Drop Table #Views&lt;/pre&gt;</content>
    <author>
      <name>GateKiller</name>
      <email>refactormycode@gatekiller.co.uk</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/347-cache-sql-server-views/refactors/13783" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor13782</id>
    <published>2008-07-25T10:20:41-07:00</published>
    <title>[C#] On Cache SQL Server Views</title>
    <content type="html">&lt;p&gt;Jonathan, Thanks very much for that elegant piece of code :) I'm in the process of replacing it with previous code I have.&lt;/p&gt;

&lt;p&gt;This was the only addition I made at the end of the statement you provided.&lt;/p&gt;

&lt;p&gt;I think from now on, I'll write my Cursor like functionality like you have :)&lt;/p&gt;

&lt;p&gt;Cheers
&lt;br /&gt;Stephen&lt;/p&gt;

&lt;pre&gt;## SQL Statement [sql]
Drop Table #Views&lt;/pre&gt;</content>
    <author>
      <name>GateKiller</name>
      <email>refactormycode@gatekiller.co.uk</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/347-cache-sql-server-views/refactors/13782" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor13628</id>
    <published>2008-07-23T08:35:50-07:00</published>
    <title>[JavaScript] On AJAX makeRequest</title>
    <content type="html">&lt;p&gt;Does it work if you remove all the try and catch statements?&lt;/p&gt;

&lt;pre&gt;this.getHttpRequest(identifier).onreadystatechange = function() {
	ajaxChat.handleResponse(identifier);
};&lt;/pre&gt;</content>
    <author>
      <name>GateKiller</name>
      <email>refactormycode@gatekiller.co.uk</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/397-ajax-makerequest/refactors/13628" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor12700</id>
    <published>2008-07-07T11:33:26-07:00</published>
    <title>[C#] On Cache SQL Server Views</title>
    <content type="html">&lt;p&gt;Thanks for the input guys :)&lt;/p&gt;

&lt;p&gt;I could update the view based on the last time one of the rows was edited but I think that would decrease the performance of the view in general.&lt;/p&gt;

&lt;p&gt;I also can't do indexed views because the views are in a different database to the source tables and this is not allowed in sql server 2005.&lt;/p&gt;

&lt;p&gt;I've taken most of the feedback from Mark to make the below which seems a little bit faster :)&lt;/p&gt;

&lt;p&gt;Cheers
&lt;br /&gt;Stephen&lt;/p&gt;

&lt;pre&gt;## Statement [sql]
Declare @Name varchar(128)
Declare cCursor Cursor LOCAL FORWARD_ONLY FAST_FORWARD READ_ONLY For Select Name From SysObjects Where XType = 'V'

Open cCursor

Fetch Next From cCursor Into @Name
While @@Fetch_Status = 0
Begin
	Declare @Table varchar(128)
	Set @Table = 'c' + SubString(@Name, 2, 128)
	If Exists(Select Name From SysObjects Where Name = @Table)
	Begin
		Exec('Drop Table ' + @Table + ' Select * Into ##TempViews From ' + @Name + ' Select * Into ' + @Table + ' From ##TempViews Drop Table ##TempViews')
	End
	Else
	Begin
		Exec('Select * Into ##TempViews From ' + @Name + ' Select * Into ' + @Table + ' From ##TempViews Drop Table ##TempViews')
	End
	Fetch Next From cCursor Into @Name
End

Close cCursor
Deallocate cCursor
&lt;/pre&gt;</content>
    <author>
      <name>GateKiller</name>
      <email>refactormycode@gatekiller.co.uk</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/347-cache-sql-server-views/refactors/12700" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code347</id>
    <published>2008-07-02T15:07:04-07:00</published>
    <updated>2011-04-08T08:09:27-07:00</updated>
    <title>[C#] Cache SQL Server Views</title>
    <content type="html">&lt;p&gt;Question: Is there anyway to make this T-SQL statement faster?&lt;/p&gt;

&lt;p&gt;Background:
&lt;br /&gt;One of our databases has a set of SQL View looking at the main database eg:&lt;/p&gt;

&lt;p&gt;vCustomers
&lt;br /&gt;vPayments&lt;/p&gt;

&lt;p&gt;This transact sql statement ocasionally makes a copy/cache of these views to improve read times in certain area's of the frontend application. (There is also an extra step which creates indexes but this is relatively fast).&lt;/p&gt;

&lt;p&gt;The code will then covert the above views into the tables:&lt;/p&gt;

&lt;p&gt;cCustomers
&lt;br /&gt;cPayments&lt;/p&gt;

&lt;p&gt;Of course, the naming convention could be changed to suit your needs.&lt;/p&gt;

&lt;p&gt;So is there any way to improve performance?&lt;/p&gt;

&lt;p&gt;Many Thanks
&lt;br /&gt;Stephen&lt;/p&gt;

&lt;p&gt;PS: There wasn't a SQL Language option.&lt;/p&gt;

&lt;pre&gt;## Statement [sql]
Declare @Name varchar(128)
Declare @Cursor Cursor

Set @Cursor = Cursor For Select Name From SysObjects Where XType = 'V'

Open @Cursor

Fetch Next From @Cursor Into @Name
While @@Fetch_Status = 0
Begin
	Declare @Table varchar(128)
	Set @Table = 'c' + SubString(@Name, 2, 128)
	If Exists(Select Name From SysObjects Where Name = @Table)
	Begin
		Print 'Drop Table ' + @Table
		Exec('Drop Table ' + @Table)
	End
	Exec('Begin Tran Select * Into ' + @Table + ' From ' + @Name + ' Commit')
	Fetch Next From @Cursor Into @Name
End

Close @Cursor
Deallocate @Cursor&lt;/pre&gt;</content>
    <author>
      <name>GateKiller</name>
      <email>refactormycode@gatekiller.co.uk</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/347-cache-sql-server-views" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code334</id>
    <published>2008-06-20T14:00:30-07:00</published>
    <updated>2008-06-28T06:07:35-07:00</updated>
    <title>[C#] Encryption and Decryption</title>
    <content type="html">&lt;p&gt;This is my first attempt at Encryption and Decryption functions and I would love your feedback. :)&lt;/p&gt;

&lt;p&gt;key and iv are your &amp;quot;passwords&amp;quot; and should be changed.&lt;/p&gt;

&lt;p&gt;Many Thanks
&lt;br /&gt;Stephen&lt;/p&gt;

&lt;pre&gt;// ***** Encryption and Decryption ***** //

byte[] Key(string sText) {
	if (Encoding.Default.GetBytes(sText).Length &amp;gt; 32) {
		throw new ArgumentException(&amp;quot;Key must be less than 32 bytes long.&amp;quot;);
	}
	
	if (Encoding.Default.GetBytes(sText).Length &amp;lt; 1) {
		throw new ArgumentException(&amp;quot;Key must be more than 0 bytes long.&amp;quot;);
	}
	
	while (Encoding.Default.GetBytes(sText).Length &amp;lt; 32) {
		sText = sText + &amp;quot; &amp;quot;;
	}

	return Encoding.Default.GetBytes(sText);
}

string Encrypt(string sText) {
	// Convert Input Text into Bytes Array
	byte[] bText = Encoding.Default.GetBytes(sText.Trim());
	
	// For storing encrypted bytes
	MemoryStream mStream = new MemoryStream();
	
	// get the algorithm to use
	SymmetricAlgorithm alg = SymmetricAlgorithm.Create(&amp;quot;Rijndael&amp;quot;);
	
	// Specify Block and Key Size
	alg.BlockSize = 256;
	alg.KeySize = 256;
	
	// Create our own Key and IV
	
	byte[] key = Key(&amp;quot;34553513543651433545343545789341&amp;quot;);
	byte[] iv = {1,5,6,7,8,1,3,4,5,7,8,9,2,4,5,6,4,5,6,2,4,5,6,4,6,4,2,3,4,8,6,2};
	
	ICryptoTransform encryptor = alg.CreateEncryptor(key, iv); //CreateEncryptor(Byte[] rgbKey, Byte[] rgbIV)
	
	// Tell the crypto stream where and how to encrypt
	CryptoStream crypto = new CryptoStream(mStream, encryptor, CryptoStreamMode.Write);
	
	// Do the actually encryption
	crypto.Write(bText, 0, bText.Length);
	crypto.Close();
	
	// Byte array with encrypted data
	byte[] cipher = mStream.ToArray();
	
	// Return Encrypted Text
	return Convert.ToBase64String(cipher);
}
	
string Decrypt(string sText) {
	// Convert Input Text into Bytes Array
	byte[] bText = Convert.FromBase64String(sText);
	
	// For storing encrypted bytes
	MemoryStream mStream = new MemoryStream(bText);
	
	// get the algorithm to use
	SymmetricAlgorithm alg = SymmetricAlgorithm.Create(&amp;quot;Rijndael&amp;quot;);
	
	// Specify Block and Key Size
	alg.BlockSize = 256;
	alg.KeySize = 256;
	
	// Create our own Key and IV
	byte[] key = Key(&amp;quot;34953913543651433549343595789391&amp;quot;);
	byte[] iv = {1,5,6,7,9,1,3,4,5,7,8,9,2,4,9,6,4,5,6,2,4,5,6,4,6,4,2,9,4,8,6,2};

	ICryptoTransform decryptor = alg.CreateDecryptor(key, iv); //CreateDecryptor(Byte[] rgbKey, Byte[] rgbIV)
	
	// Tell the crypto stream where and how to decrypt
	CryptoStream crypto = new CryptoStream(mStream, decryptor, CryptoStreamMode.Read);
	
	// Do the actually decryption
	byte[] bOutput = new byte[bText.Length - 1 + 1];
	crypto.Read(bOutput, 0, bText.Length);
	crypto.Close();
	
	// Return Decrypted Text
	return Encoding.Default.GetString(bOutput).Replace(&amp;quot;\x000&amp;quot;, String.Empty);
}&lt;/pre&gt;</content>
    <author>
      <name>GateKiller</name>
      <email>refactormycode@gatekiller.co.uk</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/334-encryption-and-decryption" rel="alternate"/>
  </entry>
</feed>

