3

I have written a program that uses an Azure API to run queries against the Azure log analytics repository.

The language used to write this query is called Kusto.

I have a lot of Kusto queries that I used directly in my C# code.

Something like this :

Client.ExecuteQuery(@"Heartbeat 
| summarize LastCall = max(TimeGenerated) by Computer 
| where LastCall < ago(15m)
| take 20");

Some queries are very long and make the code very ugly.

Is there any better and more appropriate way to store these queries say for example in a database that will help to remove them from code.

SendETHToThisAddress
  • 2,756
  • 7
  • 29
  • 54
  • T4 runtime text templates? – Daniel Mann Feb 17 '19 at 19:01
  • Unless you need to change these at runtime, I would avoid putting them in a database. I would suggest using resource files (RESX) or simply store it in separate files you compile with the code. Perhaps that `Client.ExecuteQuery` even supports specifying a file? – Xerillio Feb 17 '19 at 19:01

2 Answers2

2

You can save the queries as text files along with your source code, and then package them with your application as "Embedded Resources".

The accepted answer in this post explains how: How to read embedded resource text file

You can also find plenty of examples and tutorials through your favorite search engine; try "c# embedded resource"

xander
  • 1,689
  • 10
  • 18
  • The link provided here has a some good answers on the topic. I was able to use one of them to implement a solution using a text file as an embedded resource. – SendETHToThisAddress Sep 03 '21 at 03:12
2

For EF migration I use sql which stored in Data layer library in the resources and after that call like this:

private string ReadSqlFile(string relativePath)
{
    var path = Path.Combine(AppContext.BaseDirectory, relativePath);
    return File.ReadAllText(path);
}

protected override void Up(MigrationBuilder migrationBuilder)
{
    //some migrations methods
    var script = ReadSqlFile("Migrations/DataMigration/AddNewCountryFieldToStudent.sql");
    migrationBuilder.Sql(script);
}

It's very useful to store in separate files. And don't forget to change property to copy locally.

Nick Sinitsin
  • 247
  • 1
  • 4