Click here to Skip to main content
15,889,281 members
Articles / Visual Studio
Tip/Trick

Make F5 Execute the Query in the Visual Studio Query Editor Just like in the SSMS Query Editor

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
16 Aug 2015CPOL3 min read 15.3K   1   3
This tip describes how to make the F5 key in Visual Studio execute open SQL queries, instead of starting the project debugging.

Introduction

As if the command mapped to the F5 key in most browsers versus the command mapped to the F5 key in SQL Server Management Studio (SSMS) doesn’t cause me enough grief, Visual Studio brings more annoyance to the party with its two easily confused SQL windows, the Transact-SQL Editor and the Query Designer. I think the database management tools in Visual Studio 2010 are a vast improvement even on Visual Studio 2008, and I often find them more useful than just having SSMS running in a separate application window, but as a seasoned SSMS user, I expect results when I press F5, or Execute in SSMS. Hitting F5 in a data window in Visual Studio has only one result by default, and that is to start a build. Inadvertently triggering a build in Visual Studio doesn’t make for a shiny, pretty, Sunday School type situation, especially with large solutions and complex build processes, so I think my advice to remap the F5 key for data windows in Visual Studio, to rather execute the current SQL query, is pretty good advice.

Using the Code

Let’s start with the Transact-SQL Editor. My favourite addition to the database tools in Visual Studio, this is the closest you get to the good old, free text query window in SSMS. I find the Query designer a schlep to open, as I always get subjected to the Add Tabledialogue, and then still have to hide the noisome Diagram and Criteria panes before I can get hacking away at a cool query.

Let’s make F5 execute the SQL query in the active Transact-SQL Editor pane. Bring up the Options dialogue by clicking Tools->Options on the main Visual Studio menu. This will present you with the unfriendly and anachronistic spectre of a very densely populated dialogue that is both modal and fixed size. Hard to believe in this day and age, but it’s all we have. Here it is, together with numbered steps which I explain below:

Configuring the F5 key in Visual Studio

Let's look at the numbers, as they say:

  1. Type the word execute into the Show Commands containing field. This really narrows down an otherwise punitively long list of commands. I initially tried using the term SQL, but still that filtered list was even longer than anything you want to deal within a retarded dialogue that can’t be resized and only shows about five commands at a time.
  2. There are only three commands in VS 2010, and I don’t know what Data.Execute does, but it doesn’t concern us here. Select the Data.SqlEditorExecuteSqlcommand to proceed.
  3. Remove the existing key binding for this command.
  4. Limit the scope of our binding to the SQL Editor.
  5. Give the Press shortcut keys textbox focus and press F5. Don’t try and type F and5.
  6. Assign the shortcut key to the Data.SqlEditorExecuteSql command.
  7. After repeating this procedure for the QueryDesigner.ExecuteSql command, tidy up by closing the Options dialogue.

Now, you can enjoy the peace of mind that whenever you are editing a SQL query, be it in SSMS or VSA 2010, hitting F5 will execute your query, which will hopefully be quicker than waiting for a build and getting taken against your will into a debugging session.

History

This tip was originally in my blog, but I am revising that blog to be less technical and I am moving all my technical articles here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Erisia Web Development
South Africa South Africa
I am a software developer in Johannesburg, South Africa. I specialise in C# and ASP.NET MVC, with SQL Server, with special fondness for MVC and jQuery. I have been in this business for about eighteen years, and am currently trying to master Angular 4 and .NET Core, and somehow find a way to strengthen my creative faculties.
- Follow me on Twitter at @bradykelly

Comments and Discussions

 
Questionupdate for VS2017 Pin
kbdguy12-Mar-18 5:07
professionalkbdguy12-Mar-18 5:07 
QuestionVisual Studio 2015 is a little different Pin
Rob Bittner12-Apr-17 8:27
Rob Bittner12-Apr-17 8:27 
QuestionThat's it Pin
Nelek16-Aug-15 12:27
protectorNelek16-Aug-15 12:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.