If one were bound and determined to get Dapper SQL strings in a C# project syntax colorized in Visual Studio (and for a bonus, syntax checked or even intellisense-capable!), how would one go about it?

Some wild ideas I had:

  • 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.

Who knows what other options there might be.

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 would it actually take?

有帮助吗?

解决方案 2

A bit late to the party but this extension did the job for me:

Sql Tools

Supports Visual Studio 2017 and 2019 unlike the one in the currently accepted answer. In contrast it's a very simple extension but it worked well for my Postgres/Dapper project.

其他提示

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).

Writing a Visual Studio plugin to do this isn't quite so hard, you can do it all in C#. Here's a sample I used as a reference when doing this once before. I would recommend leaving out the completion bits at first, it makes things easier to work with.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top