Self configuring embedded database in C#

I recently needed to write a C# desktop application which used its own small database. I wanted something that could be easily deployed with xcopy, so I didn’t want to connect to any external database engine. So I used SQL Compact Edition v4. Since I’ve recently been having success with PetaPoco as a micro-ORM type thing, I thought that I’d try that too. I had the idea that the database could configure itself when the program starts, so that’s what I set out to try. I was surprised how easy it was. This is what I did to make a proof-of-concept:

  • Create an empty Console Application in Visual Studio 2012
  • Use NuGet to add references to PetaPoco.Core (v5.0.1) and Microsoft.SqlServer.Compact (v 4.0.8876.1)
  • Have this code as the C# program

That’s all there is to it, easy. If you corrupt the database, then the code will re-create a clean database file next time it runs. You don’t need to include an sdf file in your project, because the database will be created by the code. Obviously this is an extremely trivial example, but it seems to work great.

Creating self-signed test certificates with Bouncy Castle

I've been messing about with Bouncy Castle as a means to create self-signed X509 certificates for testing purposes. Essentially I've taken the information from these three blog entries here and here and here, making them into one piece of code. It seems to be a pretty nifty way to make test certificates using C#.

Here are some instructions: first, create a new Console Application in Visual Studio (I'm doing .Net 4) and use NuGet to add Bouncy Castle (I've got v1.7.0) to the project. Then simply use the attached code example. It demonstrates creating a fictional type of security certificate and saving it to disk.

Obviously, I can't take the credit. The clever stuff is all done by Bouncy Castle, and Roger's Blog has demonstrated how to write the C# code. All that I've done is put it together in one place.

The result is a .net X509Certificate2 object which can be used in the normal way.