简体   繁体   English

C# 如何从数据库中检索所有记录

[英]C# how can i retrieve all record from the database

I have written the Service but when I run it, it only retrieves the first record in the database table, And also if I view it on SWAGGER the service works but only return one record instead of all我已经编写了服务,但是当我运行它时,它只检索数据库表中的第一条记录,而且如果我在 SWAGGER 上查看它,服务工作但只返回一条记录而不是全部

public async Task<APRSSMSAUDIT_Dto> GetAPRSMessageViewAsync()
{
    try
    {
        using (var conn = new OracleConnection(_configuration.GetConnectionString("Payment")))
        {
            using (var cmd = conn.CreateCommand())
            {
                conn.Open();
                using (var unitOfWork = _unitOfWorkManager.Begin())
                {
                    var sql = "SELECT * FROM EPMS_Messaging";
                    var reference = conn.QueryFirstOrDefault<APRSSMSAUDIT_Dto>(sql);
                    conn.Close();
                    unitOfWork.Complete();
                    await Task.Delay(1);
                    return new APRSSMSAUDIT_Dto
                    {
                        Application_ID = reference.Application_ID,
                        Reference_ID = reference.Reference_ID,
                        Mail_ID = reference.Mail_ID,
                        Message = reference.Message,
                        Template_ID = reference.Template_ID,
                        Msg_Response = reference.Msg_Response,
                        Date_Created = reference.Date_Created,
                        Branch = reference.Branch
                    };

                }
            }
        }
    }
    catch (Exception ex)
    {
        Logger.Error("An error occured in GetLatestMessageAsync Type of error : " + ex.GetType() + ". Error message: " + ex.Message + "Exception data : " + ex.Data + "Exception numerical code : " + ex.HResult + "TargetSite : " + ex.TargetSite + "Exception source " + ex.Source);
        return null;
    }
}
    }
}

QueryFirstOrDefault() only returns you the first item of the collection you're trying to retrieve. QueryFirstOrDefault()仅返回您尝试检索的集合的第一项。

You can read here some documentation of how it works: QueryFirstOrDefault()您可以在此处阅读有关其工作原理的一些文档: QueryFirstOrDefault()

In order to retrieve the whole collection you have to use Query() .为了检索整个集合,您必须使用Query()

If you still don't know how to use it you can check this link .如果您仍然不知道如何使用它,可以查看此链接

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

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