Parameters 如何在KQL查询中指定参数?

Parameters 如何在KQL查询中指定参数?,parameters,kql,Parameters,Kql,我正在尝试使用查询参数和.NET Kusto SDK对Azure Data Explorer群集执行KQL查询 我尝试将参数放在大括号{}内,而不放在大括号内 我已经阅读了有关向查询传递参数的文档,但是我找不到任何示例说明通过.NET SDK传递到Azure Data Explorer时查询的外观 当我在Kusto.Explorer工具中设置参数时,我的查询可以在该工具中工作,但在使用SDK时我运气不佳 var queryParameters = new Dictionary<string

我正在尝试使用查询参数和.NET Kusto SDK对Azure Data Explorer群集执行KQL查询

我尝试将参数放在大括号{}内,而不放在大括号内

我已经阅读了有关向查询传递参数的文档,但是我找不到任何示例说明通过.NET SDK传递到Azure Data Explorer时查询的外观

当我在Kusto.Explorer工具中设置参数时,我的查询可以在该工具中工作,但在使用SDK时我运气不佳

var queryParameters = new Dictionary<string, string>()
            {
                { "myscope", "scope001" },
                { "startdate", "2019-01-01" },
                { "enddate", "2019-01-30" },
                { "author", "Bob Jammo" }
            };

var query = @"declare query_parameters (myscope:string, startdate:string, enddate:string, author:string);
                            Events 
                            | where Scope == ""{myscope}"" 
                                and EventTime between (datetime({startdate}) .. datetime({enddate}))
                                and EventType == ""product""
                                and User.Email <> """"
                            | mv-expand Payload.products
                            | where Payload_products.authors contains ""{author}""
                            | distinct DeviceId
                            | count";

using (var client = KustoClientFactory.CreateCslQueryProvider(ConfigurationManager.AppSettings["AdxConnectionString"]))
{
    var clientRequestProperties = new Kusto.Data.Common.ClientRequestProperties(
        options: null,
        parameters: queryParameters);

    clientRequestProperties.ClientRequestId = StepsBase.ScenarioScope;

    using (var reader = client.ExecuteQuery(query, clientRequestProperties))
    {
        reader.Read();
        return Convert.ToInt32(reader[0]);
    }
}
var queryParameters=newdictionary()
{
{“myscope”,“scope001”},
{“开始日期”,“2019-01-01”},
{“结束日期”,“2019-01-30”},
{“作者”、“Bob Jammo”}
};
var query=@“声明查询参数(myscope:string,startdate:string,enddate:string,author:string);
事件
|其中Scope==“{myscope}”
和EventTime之间(datetime({startdate})…datetime({enddate}))
和EventType==“”产品“”
和用户。电子邮件“”
|mv扩展有效载荷产品
|其中Payload_products.authors包含“{author}”
|不同设备ID
|计数”;
使用(var client=KustoClientFactory.CreateCslQueryProvider(ConfigurationManager.AppSettings[“AdxConnectionString”]))
{
var clientRequestProperties=new Kusto.Data.Common.clientRequestProperties(
选项:null,
参数:查询参数);
clientRequestProperties.ClientRequestId=StepsBase.ScenarioScope;
使用(var reader=client.ExecuteQuery(查询、clientRequestProperties))
{
reader.Read();
返回Convert.ToInt32(读取器[0]);
}
}

我得到一个错误,它向我表明参数值尚未设置:“语法错误:无法解析查询:无法解析datetime文本:'datetime(startdate)'”

根据错误消息判断,这是因为您使用的是
datetime({startdate})
而不是
todatetime({startdate})


无论如何,您也可以考虑更改<代码>起始日期> /代码>和<代码>结束日期<代码> >代码>日期时间>代码>而不是<代码>字符串 >开始(并且在.NET辞典中调整它们的定义)

根据错误消息判断,这是因为您使用的是代码>日期时间({StaseDe}})而不是
todatetime({startdate})

无论如何,您也可以考虑更改<代码>起始日期> /代码>和<代码>结束日期<代码> >代码>日期时间>代码>而不是<代码>字符串< /代码>开始(并在.NET词典中调整它们的定义)