kamal Aliyu
kamal Aliyu

Reputation: 77

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

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;
    }
}
    }
}

Upvotes: 0

Views: 345

Answers (1)

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 328

QueryFirstOrDefault() only returns you the first item of the collection you're trying to retrieve.

You can read here some documentation of how it works: QueryFirstOrDefault()

In order to retrieve the whole collection you have to use Query().

If you still don't know how to use it you can check this link.

Upvotes: 1

Related Questions