Search code examples
c#visual-studiosyntax-highlightingcolorize

Colorize SQL strings in Visual Studio


How can one get code colorization of SQL strings in a C# project within Visual Studio? And for a bonus, add syntax checking and intellisense?

Despite some time spent searching the web, I couldn't find any way to do this and am not sure where to go next.

Note: this question is not looking for "preferred" or "best" or seeking people's opinions on how they like to do it, it's seeking any way at all to achieve the above concrete goal(s).

My wild ideas were:

  • Create a new language that is in reality C#, but has just enough differences to support inline SQL somehow (not inside double quotes).
  • Make a Visual Studio plugin (or modify the attendant VS code some way) that at some point manages to get code colorization in.

I get that achieving my objective could be very costly in terms of effort, time, resources, etc. I'm asking from the perspective of, if you had those resources, what is any possible way to get code colorization?

The use case is Dapper, if that helps.


Solution

  • There is already a Visual Studio Add In for doing this. Check it out!

    From their site:

    This Visual Studio 2010 extension adds basic SQL syntax highlighting (keywords, functions and variables) to string literals.

    In addition, you can get the source code and customize it anyway you feel fit to (including adding specific Dapper keywords).