简体   繁体   English

Dapper动态QueryMultiple

[英]Dapper Dynamic QueryMultiple

I'm wanting to query 2 tables and return 2 result sets using dapper. 我想查询2个表,并使用dapper返回2个结果集。

Here is the code i'm trying. 这是我正在尝试的代码。

This code builds but QueryMultiple looks like its not able to get the second result set or something. 这段代码可以构建,但是QueryMultiple看起来好像无法获得第二个结果集或其他内容。 I have tried looping the results and using .Read() which is dynamic. 我试过循环结果并使用动态的.Read()。

Do you think it could not be working because i'm trying to add 2 different sizes of tables to a single dynamic list? 您是否认为因为我试图将2种不同大小的表添加到单个动态列表中而无法正常工作?

    public virtual IEnumerable<dynamic> QueryDatabase(Report report)
    {
        if (report == null) return null;
        using (var conn = new SqlConnection(this.configurationHelper.GetConnectionStringByName(report.ConnectionName)))
        {
            conn.Open();
            var sql = report.Query;
            //var results = conn.QueryMultiple(sql).Read<dynamic>();

            using (var multi = conn.QueryMultiple(sql))
            {
                var result = new List<dynamic>();

                foreach (var item in multi.Read())
                {
                    result.Add(item);
                }
                return result;
            }
        }
    }

Why not create models for each returning table? 为什么不为每个返回表创建模型? example

A class to represent the "Hub" Table 代表“集线器”表的类

public class Hub
    {
        public byte Hub_Id { get; set; }
        public string Hub_Name { get; set; }
        public bool Hub_IsEnabled { get; set; }
    }

A Class to represent "Opco" Table 代表“ Opco”表的类

public class Opco
    {
        public string Opco_Id { get; set; }
        public string Opco_Country { get; set; }
        public byte Opco_Hub_Id { get; set; }
        public bool Opco_IsEnabled { get; set; }
    }

A ViewModel to represent the ViewModel 代表ViewModel的ViewModel

public class ReportViewModel
    {
        public List<Opco> OpcoList { get; set; }
        public List<Hub> HubList { get; set; }

    }

The dapper code to return the view Model dapper代码返回视图模型

 string connString = Utility.Common.GetConnectionString();
        public ReportViewModel GetReportViewModel()
        {
            ReportViewModel reportViewModel = new ReportViewModel();

            using (var conn = new SqlConnection(connString))
            {
                conn.Open();

                using (var multi =
                                conn.QueryMultiple("GetReportViewModel", null, null,
                                commandTimeout: 0,
                                commandType: CommandType.StoredProcedure))
                {
                    reportViewModel.HubList = multi.Read<Hub>().ToList();
                    reportViewModel.OpcoList = multi.Read<Opco>().ToList();
                }
            }
            return reportViewModel;
        }

The SP must be like the following, fields must be in same order as the class and the SELECT must be in the order of the ViewModel, I meant first Select the Hubs and later the Opcos. SP必须类似于以下内容,字段的顺序必须与类相同,而SELECT的顺序必须与ViewModel的顺序相同,我的意思是首先选择集线器,然后选择Opcos。

ALTER PROCEDURE [dbo].[GetReportViewModel]

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT 
        Hub_Id,
        Hub_Name
    FROM [dbo].[Tbl_Hubs]
    WHERE [Hub_IsEnabled] = 1;

    SELECT
        OpCo_Id,
        OpCo_Country
    FROM [dbo].[Tbl_OpCos]
    WHERE [OpCo_IsEnabled] = 1;
END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM