1

我想使用 SqlKata 创建和编译一个 SQL 查询,然后在其他一些函数中执行这个查询。根据文档,这并不明显,尤其是对于复杂的查询。

我曾尝试将查询转换为字符串然后运行它,但这似乎不是正确的做法,而且该字符串对于复杂的查询不能很好地编译。

    public class TradeLoader : ITradeLoader
    {

        public SqlResult CreateTradeQuery(Target targets, Group groups, DateTime fromTime)
        {
            var compiler = new SqlServerCompiler();
            var query = new Query().FromRaw(@"
                    [trades] AS t WITH (NOLOCK)
                    LEFT JOIN [info] AS i WITH (NOLOCK)
                    ON t.Id = i.Id
                ")
                .Select("i.Name", "t.Price", "t.Volume")
                .WhereTime("t.CreatedDate", ">", fromTime);

            var subQuery = new Query(@"
                    [trades] AS t WITH (NOLOCK)
                    LEFT JOIN [info] AS i WITH (NOLOCK)
                    ON t.Id = i.Id
                ");
            foreach (var target in targets)
            {
                subQuery.OrWhere(q => q.WhereIn("i.Name", groups.Keys).WhereIn("i.GroupName", target.Value));
            }

            query.Where(subQuery);
            SqlResult result = compiler.Compile(query);
            return result; // How to execute this somewhere else?
        }
    }

我不清楚如何使用 SqlKata.Execution 或 System.Data.SqlClient 在其他地方执行此查询。最好我应该能够在两者中运行它。

4

2 回答 2

1

我不明白你想做什么。你可以这样使用它。

public SqlResult CreateTradeQuery(Target targets, Group groups, DateTime fromTime)
{
    var query = GetDefaultQuery();
    query.Select("i.Name", "t.Price", "t.Volume");
    query.Where("t.CreatedDate", ">", fromTime);
    query.Where(GetSubQuery(targets,groups),"subQuery");
    SqlResult result = compiler.Compile(query);
    return result;
}

private Query GetSubQuery(Target targets, Group groups)
{
    var subQuery = GetDefaultQuery();
    foreach (var target in targets)
    {
        subQuery.OrWhere(q => q.WhereIn("i.Name", groups.Keys).WhereIn("i.GroupName", target.Value));
    }
    return subQuery;
}

private Query GetDefaultQuery()
{
    var query = new Query("trades as t");
    query.LeftJoin("info as i","i.Id","t.Id");
    return query;
}
于 2019-11-13T13:06:40.300 回答
0

对于精通C#/.NET的人来说,这可能是基本知识,但我想做的是将参数注入使用SqlKata创建的SQL查询中,然后在不使用SqlKata的情况下运行它。最后,我通过以下方式做到了(可能还有其他方法可以做到这一点):

public class TradeLoader : ITradeLoader
{
    ...

    public async Task<SomeDataType> RunQuery()
    {
        ...
        var sqlCommand = CreateTradeQuery(someTargets, someGroups, someDateTime);
        using (SqlConnection sql = new SqlConnection(_connectionString))
        {
            sql.Open();
            var command = new SqlCommand(sqlCommand.Sql, sql) {CommandTimeout = _timeout};
            foreach (var (name, value) in sqlCommand.NamedBindings)
            {
                var parameter = command.CreateParameter();
                parameter.ParameterName = name;
                parameter.Value = value;
                command.Parameters.Add(parameter);
            }

            using (SqlDataReader dataReader = await command.ExecuteReaderAsync())
            {
                while (await dataReader.ReadAsync())
                {
                    // read data here
                }
            }
        }
        return data;
    }
}

这允许您使用 SqlKata 构建查询,但使用其他库运行查询,例如我使用System.Data.SqlClient.

于 2019-11-14T08:23:01.840 回答