Category Archives: .NET

TIP: Quickly purge SQL Server Data

I was working on writing some integration tests, and needed to quickly get the database to a clean state between them. Dropping and recreating the database would take too long, but, if I could easily delete all the data, then I could populate with what I needed for each test…with MS SQL Server, you can use the stored proc sp_MSForEachTable to run SQL against each table. The following code uses the proc, disabling triggers, and constraints so it can delete all data, then enables them again

public static void PurgeData()
{
	using (var conn = new SqlConnection(ConnectionString))
	{
		conn.Open();
 
		var cmdText = new StringBuilder();
		cmdText.AppendLine("EXEC sp_MSForEachTable 'DISABLE TRIGGER ALL ON ?'");
		cmdText.AppendLine("EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'");
		cmdText.AppendLine("EXEC sp_MSForEachTable 'DELETE FROM ?'");
		cmdText.AppendLine("EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'");
		cmdText.AppendLine("EXEC sp_MSForEachTable 'ENABLE TRIGGER ALL ON ?'");
 
		using (var sqlCmd = new SqlCommand(cmdText.ToString(), conn))
		{
			sqlCmd.ExecuteNonQuery();
		}
 
		conn.Close();
	}
}

Hope this helps you out.

TIP: Always run Visual Studio as an Administrator

If you’re like me, you always want Visual Studio opening as Administrator, allowing it to install services, and do whatever you need it to do. But you often forget to right click and “Run as Administrator”. Or you just want to be able to use the pinned shortcuts on the task bar. It’s really amazingly simple to make it always run as an administrator, just follow these simple steps:

  1. Right Click on “devenv.exe” in Explorer
  2. ClickTroubleshoot compatibility
  3. ClickTroubleshoot program
  4. CheckThe program requires additional permissions
  5. ClickNext
  6. ClickTest the program…“. It should launch Visual Studio as Administrator
  7. ClickNext
  8. ClickYes, save these settings for this program
  9. ClickClose the troubleshooter

You can revert by following the same steps, but unchecking “The program requires additional permissions”

It’s a great day to be a .Net developer

What more is there to say?  Microsoft opensourced Roslyn (.NET Compiler Platform), source is avaiable on CodePlex!!!

It’s fantastic news, to quote Eric Lippert

What astonished me was that its not just a “reference” license, but a full on liberal Apache 2.0 license. And then to have Miguel announce that Xamarin had already got Roslyn working on linux was gobsmacking.

It’s a huge step, and something I hope Microsoft does more of.  I can’t wait to find some time to dig into this code, and see how it all works.