[Dapper] 利用 Dapper select 資料到 List

摘要:[Dapper] 利用 Dapper select 資料到 List

product.cs


   public class Product
    {
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        public int SupplierID { get; set; }
        public int CategoryID { get; set; }
        public string QuantityPerUnit { get; set; }
        public decimal UnitPrice { get; set; }
        public short? UnitsInStock { get; set; }
        public short? UnitsOnOrder { get; set; }
        public short? ReorderLevel { get; set; }
        public bool Discontinued { get; set; }
        // reference 
        public Supplier Supplier { get; set; }
    }

Program.cs


using (var conn = new SqlConnection(mNorthwindConnStr))
{
	conn.Open();
	IEnumerable products = conn.Query("Select * from Products where 
                                          ProductId = @ProductId", new { ProductID = 4 });

	foreach (Product product in products)
	{
		Console.Write(string.Format("ProductID:{0}, ProductName:{1}, SupplierID:{2}, 
                             QuantityPerUnit:{3}, UnitPrice:{4}",product.ProductID,
                             product.ProductName, product.SupplierID, product.QuantityPerUnit,
                             product.UnitPrice));
	}

	conn.Close();
}