Ejemplo n.º 1
0
        /// <summary>
        /// 以DataRelation實作DataTable中的Sum運算
        /// </summary>
        /// <param name="ds"></param>
        private static void ComputeSum(DataSet ds)
        {
            DataTable orderDt = ds.Tables["OrderDetail"];

            DataTable sumDt = new DataTable();

            sumDt.TableName = "GruopSum";
            sumDt.Columns.Add("ProductId", typeof(int));
            sumDt.Columns.Add("Discount", typeof(decimal));
            sumDt.Columns.Add("Sum", typeof(int));

            //寫入group by的兩個欄位
            foreach (DataRow dr in orderDt.Rows)
            {
                if (sumDt.Select(string.Format("productId = {0} and discount = {1}", dr["ProductId"], dr["Discount"])).Length == 0)
                {
                    sumDt.Rows.Add(new object[] { dr["ProductId"], dr["Discount"] });
                }
            }

            ds.Tables.Add(sumDt);

            //指定group by欄位對應, 加入relation
            ds.Relations.Add("GroupSumRelation",
                             new DataColumn[] { sumDt.Columns["ProductId"], sumDt.Columns["Discount"] },
                             new DataColumn[] { orderDt.Columns["ProductId"], orderDt.Columns["Discount"] });

            //指定彙總函數運算使用的relation及欄位
            sumDt.Columns["Sum"].Expression = "Sum(Child(GroupSumRelation).Quantity)";

            PrintData.PrintDataSet(ds);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            DataSet ds;

            //ds = GetDataByAdapter();
            ds = GetDataByReader();

            //資料來源須配合GetDataByAdapter()
            //UpdateProductName(ds);

            PrintData.PrintDataSet(ds);

            //資料來源須配合GetDataByAdapter()
            //BulkCopy(ds);

            Console.ReadKey();
        }