Monday, March 24, 2008

Programmatically Executing SQL Scripts

I didn't realize GO is not a SQL keyword. When I was trying to execute a script that worked in SQL Server Management Studio I was getting SQLException Incorrect syntax near 'Go'. The solution is to split the input script on GOs and execute those scripts.

const char splitChar = '☻'; //this character should never appear in command files
if (createDatabaseScript.Contains(splitChar.ToString()) == true)
throw new Exception("Aborting. Splitting this file may break the script because the script contains the split character");
using (dbConnnection)
SqlCommand createDbCommand = new SqlCommand(string.Empty, dbConnnection);

//split the input script into multiple scripts based on GOs
foreach (string command in createDatabaseScript.Replace("GO", splitChar.ToString()).Split(new Char[] { splitChar }))
createDbCommand.CommandText = command;
catch (Exception ex)
//log the error for later review.
results += ex.Message + Environment.NewLine;

Monday, March 17, 2008

FxCop bug - CA1709 & CA 1707

CA 1707 IdentifiersShouldNotContainUnderscores
CA 1709 IdentifiersShouldBeCasedCorrectly

I ran into a surprise last week. FxCop is throwing warnings for properties on 2 out of 5 interfaces in a particular project. Interface A implements interface B.

I'm assuming the bug described in this article is causing it: and that it will be fixed in Visual Studio 2008 Service Pack 1.

Monday, March 10, 2008

Unit Testing Graphics Operations

We decided to verify our graphic operations here by doing a binary comparison.
Here is a simple implementation using Linq.

Setting up the expected value:

Bitmap aBitmap = new Bitmap(181,46); //exact size of the bitmap to reduce the number of bits
Graphics aGraphic = Graphics.FromImage(aBitmap);
target.DrawGrid(aGraphic); //the operation you want to verify
MemoryStream actual = new MemoryStream();
string aBunch = string.Join(",", actual.ToArray().Select(b => b.ToString()).ToArray());

Copy the string aBunch. We will use this to create the expected byte array.
create the byte array like this but use your pasted values instead of my values
byte[] expected = new byte[] {66,77,78... };

Finishing the unit test:

Bitmap aBitmap = new Bitmap(181,46);
Graphics aGraphic = Graphics.FromImage(aBitmap);
MemoryStream actual = new MemoryStream();
aBitmap.Save(actual,ImageFormat.Bmp); // get our actual result.
byte[] expected = getExpected("DrawGridTest");

Assert.IsTrue(actual.ToArray().SequenceEqual(expected),"Binary comparison failed.");

This method is getting expected from a function because Visual Studio responds slowly when there are long lines of text. I created a partial class containing the instantiation of expected (I'm using a switch statement). I thought about putting the results in a resource or a file, but I'm preferring code for now.