Google analytics script

Latest jQuery CDN with code tiggling.

Thursday 4 November 2010

Running batch files (*.bat) inside Visual Studio

This is something very simple, but very useful. Something Microsoft should've put in Visual Studio in the first place long ago. When I develop data-aware applications I usually end up writing database scripts to make it easy to (re)deploy and version control my database model. I usually write several files just to make things controllable and manageable. But deploying them is a different task altogether. And a tedious one as well. I have to fire up SQL Management Studio and then run all those scripts in correct order. Since this is done rather frequently during application development it seems like a very good candidate for automation or simplification. So let's do just that.

Managing database scripts

Some development teams split database scripts into several files to make it easier to version control and handle file merging. They may put every DB object drop/creation into a separate file or they may split the whole script into functional parts like dropping, model creation etc. This greatly depends on several factors as well as on developer preference. I tend to use the latter approach, so I don't end up with a huge amount of files (which may become very unmanageable on their own) and each file has a manageable content length. I usually split my script into these files:

  1. Drop Objects.sql
  2. Create Model.sql
  3. User Functions.sql
  4. Stored Procedures.sql
  5. Static Data.sql
  6. Test Data.sql - this one is only used during development time to provide enough data to develop against
This is the starting list, that also needs to be executed in the same order as listed here. If there's a particular lookup table with lots of insert statements I may put those into a separate file so it makes Static/Test Data scripts more manageable.

Automate/simplify execution

Running these scripts in correct orders is prone to human errors. We may mess up the order of execution or forget to run (or even double run) a certain script. So let's make it possible to execute these scripts directly from within Visual Studio. The first thing to do is to add a batch file (i.e. Deploy DB.bat) to the project that would probably look similar to this: @sqlcmd -S servername -d dbname -U username -P password -i "Drop Objects.sql","Create Model.sql","User Functions.sql","Stored Procedures.sql","Static Data.sql","Test Data.sql" This script will deploy all scripts to our desired database in a single run in correct order and all of the scripts so none will be left out. Test Data.sql script has to be omitted in final deployment scenario. Of course. The @ sign at the beginning only suppresses the command itself to be displayed in the command window.

Ok so we created a batch file that makes it simple to deploy all database scripts at once. But this still means we have to fire up command window, navigate to our file and run it. So let's now make it possible to run these from within Visual Studio then. Follow this list of steps to accomplish just that:

  1. Open Windows explorer and navigate to %UserProfile%\Documents\Visual Studio 20xx\
  2. Add a new file here and name it ExecuteBatch.cmd.
  3. Edit this file and put these two lines in @cmd /c %1
    @pause
  4. Go to Visual Studio and right click previously added batch file (Deploy DB.bat) and select Open With... context menu option.
  5. This dialog will open where you have to click on the Add... button.
  6. When you click the Add... button this next window will appear:
  7. Browse for the ExecuteBatch.cmd that you've created previously and give it a nicer friendly name as shown above. Confirm by clicking the OK button.
  8. The dialog will now display our new new "editor" which will execute batch files. If you want to, you can also set this one as the default "editor" for batch files (since we started off by right clicking on a batch file). That will mean that by double clicking on a batch file, it will be immediately executed.
  9. Confirm everything by clicking on the OK button.

Voila! That's it really. Until you reinstall your machine, you'll be able to run batch files from within Visual Studio. Deploying database scripts will become bliss. One thing less to worry about.

6 comments:

  1. I like this, but I'd like to take it a step further. How can you use the new ExecuteBatch.cmd to add a dialog in the User Interface for installing your script at the end of your project installation?
    Thanks.

    ReplyDelete
  2. @KW: Are you asking how to run a DB creation script at the end of an application installer program? This doesn't really have much to do with Visual Studio...

    ReplyDelete
  3. Hi, Can I use a button click event to call a batch file?

    ReplyDelete
    Replies
    1. Which button would that be? Some button in Visual Studio or anywhere else?

      Delete
    2. yes i want to call this batch file as i have made this batch file to print something so, i want to create a button and whenever i click on that button it start printin by calling that batch file

      and 1 more thing i want to give quantity that how may print i want to take at that time. so, pl guide for this query also

      Delete
    3. I think your best bet would be to write a macro that would do the trick. But you will have to resort to other resources than my blog.

      Delete

This is a fully moderated comments section. All spam comments are marked as spam without exception. Don't even try.

Note: only a member of this blog may post a comment.