Thursday, March 13, 2008

Supporting a global data menu option under 2005 and 2008

It's no secret that that SQL Server integrates better into Visual Studio than any other database.  We are trying to change that.  Two of the things that I wanted to implement in the new version of our integration code is to replicate the Add New submenu and use the the built-in table browsers that Sql Server uses.  Here is a screen shot of the global data menu we wanted to reproduce.

This menu should also work in Visual Studio 2005 though the top four items (Schema Compare, Data Compare, Refactor, and T-SQL Editor) won't be there.  The problem is that adding items to the global data menu is not directly supported in Visual Studio 2005 but it is supported in 2008.  This is further complicated due to the fact that Microsoft moved from using CTC files to VSCT files to define package resources.  I didn't want to ship two binaries (one for 2005 and one for 2008) so I have continued to use the CTC format.  Here is what I had to do to get it working.
First, I defined a constant for the appropriate menu group in VS 2005.  I put this in my file named guids.h
#define guidVS2005Data          { 0x501822e5, 0xb5af, 0x11d0, { 0xb4, 0xdc, 0x00, 0xa0, 0xc9, 0x15, 0x06, 0xef } }
Then I defined two menus and two groups (one for 2005 and one for 2008).
    guidMySqlProviderCmdSet:menuAddNew2005, guidVS2005Data:8706, 0x1000,, "MySQLAdd", "&Add New";
    guidMySqlProviderCmdSet:menuAddNew2008, guidVSData:IDG_DV_GLOBAL1, 0x1000,, "MySQLAdd", "&Add New";

    guidMySqlProviderCmdSet:groupAddNew2005, guidMySqlProviderCmdSet:menuAddNew2005, 0x0000;
    guidMySqlProviderCmdSet:groupAddNew2008, guidMySqlProviderCmdSet:menuAddNew2008, 0x0000;

Note here that 8706 is the id of the global data menu in VS 2005 and IDG_DV_GLOBAL1 is the id under VS 2008.  menuAddNew2005, menuAddNew2008, groupAddNew2005, and groupAddNew2008 are just simple ids I assigned. The reason you need to define the menu twice is because no matter which version of VS you use it will balk at adding the same menu to two different top level groups.
Then I define the commands I want to appear on the menus but I don't put them on the menus here since I need to put them on two different menus.
/* global data menu items */
guidMySqlProviderCmdSet:cmdidAddNewTableGlobal, Group_Undefined:0, 0x0000, OI_NOID, BUTTON, DIS_DEF, "&Table";
guidMySqlProviderCmdSet:cmdidAddNewViewGlobal, Group_Undefined:0, 0x0001, OI_NOID, BUTTON, DIS_DEF, "Vie&w";
guidMySqlProviderCmdSet:cmdidAddNewProcedureGlobal, Group_Undefined:0, 0x0002, OI_NOID, BUTTON, DIS_DEF, "Stored &Procedure";
guidMySqlProviderCmdSet:cmdidAddNewFunctionGlobal, Group_Undefined:0, 0x0003, OI_NOID, BUTTON, DIS_DEF, "Stored &Function";
guidMySqlProviderCmdSet:cmdidAddNewUDFGlobal, Group_Undefined:0, 0x0004, OI_NOID, BUTTON, DIS_DEF, "&UDF";

Now I add the commands to both menus
guidMySqlProviderCmdSet:cmdidAddNewTableGlobal, guidMySqlProviderCmdSet:groupAddNew2005, 1;
guidMySqlProviderCmdSet:cmdidAddNewTableGlobal, guidMySqlProviderCmdSet:groupAddNew2008, 1;

/* others omitted for brevity  */
Now we need to add our commands to our data view XML file.  Here is what one command binding looks like.
<CommandBinding name="AddNewTable" guid="B87CB51F-8A01-4c5e-BF3E-5D0565D5397D"
                cmdid="500" handler="MySql.Data.VisualStudio.MySqlDataViewCommandHandler">
  <Parameter value="HighLevel"/>

Notice the command parameter named "HighLevel".  This is important because without it the menus will not function correctly in VS 2005.  You also need to add all the commands into the command bindings of every node.  Yes, this is very repetitious but is necessary due to a bug in VS 2005.  Without doing that the menu will appear when the connection node is selected but will disappear when on the other nodes.
I would also like to thank a developer at Microsoft who answered tons of my questions and helped me sort this out.  His name is Stephen Provine and I really appreciate all his hard work.  With all this done, here is the same shot working with MySQL.


  1. Descargar música gratisMarch 25, 2008 at 12:12 PM

    Nice explanation, thank you ;-)

  2. thanks for the article..

  3. nice info, thank you

  4. what a great blog, i really like it.

  5. stroller travel systemJuly 10, 2009 at 11:19 AM

    MS makes some good tools, and the integration is always top notch

  6. You are so true on that!

  7. I felt bad because I was always worried about what the teacher would say, and now I use the services of a masterpaper com, which does any written work on order for everyone who wants to get good grades.