using Microsoft.Data.SqlClient; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; namespace ConsoleApp1 { static class Program { public static void Main(string[] args) { Payment payment = new Payment(); List payments = payment.FetchAll(); } } public class Payment { [Key] public int Id { get; set; } [Required(ErrorMessage = "Required")] public string BorT { get; set; } [Required(ErrorMessage = "Required")] public string Supplier { get; set; } [Required(ErrorMessage = "Required")] public string Type { get; set; } [Required(ErrorMessage = "Required")] public string Name { get; set; } [Required(ErrorMessage = "Required")] public string AccountNo { get; set; } [Required(ErrorMessage = "Required")] [MaxLength(1)] public string Frequency { get; set; } [Required(ErrorMessage = "Required")] public DateTime DueDate { get; set; } [Required(ErrorMessage = "Required")] public decimal Amount { get; set; } public int UserId { get; set; } public DateTime TransDate { get; set; } public DateTime EffectiveDate { get; set; } [MaxLength(6)] public string BSBNo { get; set; } public decimal abu { get; set; } public decimal bu { get; set; } [MaxLength(6)] public string Status { get; set; } public DateTime ProcessDate { get; set; } [MaxLength(6)] public string ProcessBy { get; set; } public decimal Calcbu() { bu = (decimal)(4.2 * Convert.ToDouble(Amount) / 12); return bu; } public List FetchAll() { List returnList = new List(); string ConnectionString = @"Server=****.database.windows.net; Authentication=Active Directory Default; Database=mytestdb1;"; using (SqlConnection conn = new SqlConnection(ConnectionString)) { conn.Open(); } using (SqlConnection con = new SqlConnection(ConnectionString)) { SqlCommand command = new SqlCommand("SELECT * FROM dbo.Payment", con); con.Open(); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { Payment newpayment = new Payment(); newpayment.Id = reader.GetInt32(0); newpayment.BorT = reader.GetString(1); newpayment.Supplier = reader.GetString(2); newpayment.Type = reader.GetString(3); returnList.Add(newpayment); } } return returnList; } } } }