Search code examples
c#sqlsql-server-cebuild-process

Building a database from SQL files


This is for a project in a databases course, so I'm trying to avoid things like EF Code First that let me pretend that databases don't actually exist and data just magically persists.

Basically I want to be able to tear-down and re-build a local SQL Server CE database from SQL scripts as a build step in the project. I'm using VS2012 express and it's a WPF project. Ideally there would be a single SQL folder visible in the project from the IDE, that contains scripts which are run in order.

All I've been able to find are tools that help you avoid writing SQL. Any suggestions for how I might do this?


Solution

  • You could write some C# code that reads the contents of a target folder and executes it against the database (using pure, uncomplicated SqlCommands), then put this code into a MSBuild Task, and add it to the AfterBuild target of your csproj.

    EDIT: even better, use a pre-existing MSBuild pack (like http://www.natthompson.com/?p=183 this one) to execute the sql for you!