Reputation: 116790
Often times I write some SQL like this:
string sql = @"
-- Multi-line SQL
";
Without getting into a debate on whether this approach is good or bad, can someone tell me what is the best way to get this SQL to be highlighted inside Visual Studio?
One approach I can think of is to create separate .sql
files and then consume that as a string here so that when editing the original SQL, Visual Studio recognizes that this is a SQL file and syntax-highlights it.
This seems to be a tedious approach to solve a simple problem. Is there a simpler solution?
Upvotes: 10
Views: 5564
Reputation: 22836
JetBrains just released ReSharper 2016.2 which has support for inline syntax highlight and intellisense for regular expression and html with code comments like /*language=html*/
.
I find that an amazing feature, which (I believe) could quite easily be extended to support SQL and almost any other language.
I've added a feature request here: https://youtrack.jetbrains.com/issue/RSRP-460656
This doesn't answer your question, but I'm hoping they'll consider adding such a feature, as inline SQL is quite common in code (ie. working with Dapper).
Upvotes: 8
Reputation: 1097
There is a Visual Studio extension for this (for VS 2010 and 2012). It does have the problems you would imagine, such as highlighting SQ: keywords in strings that don't contain SQL. Depending on the type of work you do, you might still prefer this to not having highlighting in SQL.
http://visualstudiogallery.msdn.microsoft.com/a3a662c6-28eb-4de9-9a29-d328b1ac3f6b
One thing I can recommend is changing the SQL keyword colors in Visual Studio to be variations on the normal string colors. This way, the highlighting is helpful when correct, but isn't distracting in cases where it is highlighting a word in a string that doesn't actually contain SQL.
Upvotes: 6
Reputation: 4730
It is not going to work as others are telling you. How would VS know which strings are sql and which are not. If you dont want to use sql files, you can make your own file extension like ".sqlx" or something and then in VS properties assign SQL editor for this extension, so VS knows to apply correct colour coding to it.
Then you can do something like this:
string sql = System.IO.File.ReadAllText("your path to sql file");
It may even be better because at least you will keep SQL code separate. However, I would strongly recommend looking at Entity Framework or Linq-to-SQL
Upvotes: 0