Introduction
An annoying feature I have found in Visual Studio 2005 is how it handles SQL Server 2005 Express database files (.MDF and .LDF) while debugging a project. By default, Visual Studio has, for content files of database type, the option "Copy To Output Directory" set to "Copy Always". This is good if you constantly change the database schema, tables, Stored Procedures, views etc., but if you have a working schema that you want to edit both with your application and with the IDE, you have to set the option to "Do Not Copy". This is because if you edit the database through "Data Connections" in the "Server Explorer" window, the database file modifies date/time changes even if you expand the tables in the database, and you can't use the "Copy if newer" option at all.
Now, since I found that the "Do not copy" option is the easiest way to go around this one, I didn't want to leave my favourite IDE to copy and paste database files between debugging sessions. So I decided to write an add-in to Visual Studio to do this for me - partially for curiosity because I hadn't written add-ins before to Visual Studio, but partially for the exact need. I had just started building an application with lots of Master/Detail forms, and I rather use my application to edit the data instead of using direct table connections in Server Explorer.
The way this add-in works is it creates three buttons to do the actual copying of data. I will explain what the buttons do:
<- Copy DB From Active Output
This button copies database from the active output folder (i.e., Debug/Release under the \bin folder) to the main project folder. This copies the database your application has last used to the project main folder.
-> Copy DB To Active Output
This button copies the database to the active output folder (i.e., Debug/Release under \bin folder) from the main project folder. This copies the database you might have edited in "Server Explorer" to the debugging folder.
-> Copy DB To All Outputs
This button copies database to all possible output folders. Usually, there is only Debug/Release, but there might be more outputs defined to your solution. Otherwise, it works the same way as "Copy DB To Active Output".
As the provided source shows, this is a rather simple piece of software. A few nice features I added were the way it shows the buttons only if the application folder includes database files.
Limitations
- Hasn't been tested with ASP.NET applications, only for WinForms at the moment.
- Does not work for folders under the application folder (i.e., if you have a Databases subfolder, the buttons won't work).
- Action buttons are in the "Standard" toolbar. As the toolbar is by default rather wide, there should be a toolbar for this add-in so they might not be shown in smaller screen resolutions.
- Add-in does not work while source or target database files are in use. Although this is a limitation of Windows, the addin could disconnect the database temporarily with SQL Server management objects for replacing the database file even at application runtime (might be interesting to implement).
- Only copies under the "\bin" folder; the information should be retrieved from project properties.
Future directions
- Fixing the limitations above.
- Keyboard shortcuts should be available for faster access.
- Possibly pre-setting of the "Copy To Output Directory" property of database files when created to "Do not copy" since the "Copy Always" in most cases is not useful.