Daily Archives: October 27, 2011

SQL Server: See your print statements as they happen using raiserror

When writing long running SQL scripts the fact that a PRINT SQL statement is not immediately output to the Messages window can cause frustration and I can never remember the syntax for RAISERROR, so here it is:
RAISERROR('Hello there', 0, 255) WITH NOWAIT
See MSDN RAISERROR

Advertisements

Asynchronous code execution in .NET 2.0 – BackgroundWorker

When TPL (.NET 4.0) is not available and you want to asynchronously do some work you can resort to .NET 2.0:

BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate(object s, DoWorkEventArgs args)
{
  // Do something here, any local variables you use will be cloned
};
worker.RunWorkerAsync();

See MSDN BackgroundWorker Class

Call private or internal constructors using reflection

Sometimes you want to get past the object oriented barriers that C# throws up.
A while ago now I wanted to instantiate a class that only had internal constructors, here is a useful snippet of how I achieved this:
// Use reflection to call private or internal constructors
Type ty = Type.GetType("assembly.name.space.name.myclass, assembly.name.space.name, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
ConstructorInfo ctor = ty.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic)[0];
var inst = ctor.Invoke(new object[] { param1, param2 });

Excel String concatenation

String Concatenation in Excel is just like VB, just ampersand the bits together e.g. A1 & “, ” & A2

Excel Replace with Substitute

In Excel if you want to replace a portion of a string with another string you might think Replace is the function to use, but it’s the Substitute function you need.
See Microsoft Online documentation

I love half term, I love half term, I just love half term so very, very, very much

Half term is a lovely time for cycle commuting.
The busy roads are (nearly) free flowing, cars are not running so late because there’s not as much traffic, so they tend to drive better too.

Normally the main road is jam packed with traffic well before I turn off, so I normally take the hatched area in the middle until my turning. But this morning I took the main road route rather than cutting through the backroads as I normally do because there wasn’t queuing traffic or anything slowing me up. I cruised down the smooth tarmac all the way without being held up by cars at all, it was a really good commute today, I wish every week were half term week.