Exemplo n.º 1
0
        /// <summary>
        /// 逐筆Add,一次SaveChange
        /// </summary>
        /// <param name="ItemData"></param>
        private static void TestRun02(IEnumerable <Users> ItemData)
        {
            TSQL2019Entities dbContext = new TSQL2019Entities();

            foreach (var item in ItemData)
            {
                dbContext.Users.Add(item);
            }
            dbContext.SaveChanges();
        }
Exemplo n.º 2
0
        /// <summary>
        /// 批次Add,批次SaveChange
        /// </summary>
        /// <param name="ItemData"></param>
        private static void TestRun03(IEnumerable <Users> ItemData)
        {
            TSQL2019Entities dbContext = new TSQL2019Entities();
            var batchCount             = 100;
            var i = 0;

            foreach (var item in ItemData)
            {
                dbContext.Users.Add(item);

                if (i % batchCount == 0)
                {
                    dbContext.SaveChanges();
                }
            }
            dbContext.SaveChanges();
        }
Exemplo n.º 3
0
        /// <summary>
        /// 批次Add,批次SaveChange,最後關閉DBContext
        /// </summary>
        /// <param name="ItemData"></param>
        private static void TestRun04(IEnumerable <Users> ItemData)
        {
            var batchCount = 100;
            var skipCount  = 0;

            var _ItemData = ItemData.ToArray();

            for (int i = 1; i <= (ItemData.Count() / batchCount); i++)
            {
                using (TSQL2019Entities dbContext = new TSQL2019Entities())
                {
                    for (int j = 0; j < batchCount; j++)
                    {
                        dbContext.Users.Add(_ItemData[j]);
                    }

                    dbContext.SaveChanges();
                }
            }
        }