Legend Legend - 11 months ago 55
C# Question

Is it possible to syntax-highlight in-line SQL?

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
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?

Answer Source

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