资源简介

读取大文件(此示例用的是nginx日志文件,约4G),并将其 批量写入数据库的方法

因为读取nginx日志大文件并拆分成必要的列 比较耗时,所以插入100万条数据用时约1分钟,如果不计算的话,批量插入效率会更高

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Globalization;
using System.IO;

namespace ConsoleAppBatInsert
{
    class Program
    {
        static readonly string StrConnMsg = “server=.;database=EbgLog;uid=sa;pwd=1@1.com“;
        static readonly long totalRow = 1000000;
        static readonly int getRow = 1000;
        static void Main(string[] args)
        {

            BatInsert();
            Console.WriteLine(“插入数据结束“);
            Console.ReadLine();
        }

        #region 方式二
        static void BatInsert()
        {
            Console.WriteLine(“使用Bulk插入的实现方式“);
            Stopwatch sw = new Stopwatch();
            DataTable dt = GetTableSchema();


 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2019-08-13 04:44  .vs\
     目录           0  2019-08-13 04:44  .vs\ConsoleAppBatInsert\
     目录           0  2019-08-13 04:44  .vs\ConsoleAppBatInsert\DesignTimeBuild\
     文件         230  2019-08-13 04:45  .vs\ConsoleAppBatInsert\DesignTimeBuild\.dtbcache
     目录           0  2019-08-13 04:44  .vs\ConsoleAppBatInsert\v15\
     文件       30720  2019-08-13 22:49  .vs\ConsoleAppBatInsert\v15\.suo
     目录           0  2019-08-13 04:44  .vs\ConsoleAppBatInsert\v15\Server\
     目录           0  2019-08-13 04:44  .vs\ConsoleAppBatInsert\v15\Server\sqlite3\
     文件           0  2019-08-13 02:39  .vs\ConsoleAppBatInsert\v15\Server\sqlite3\db.lock
     文件      438272  2019-08-13 04:38  .vs\ConsoleAppBatInsert\v15\Server\sqlite3\storage.ide
     文件       32768  2019-08-13 22:40  .vs\ConsoleAppBatInsert\v15\Server\sqlite3\storage.ide-shm
     文件     4136512  2019-08-13 22:49  .vs\ConsoleAppBatInsert\v15\Server\sqlite3\storage.ide-wal
     目录           0  2019-08-13 22:48  ConsoleAppBatInsert\
     文件         234  2016-12-08 16:02  ConsoleAppBatInsert\App.config
     文件        2580  2016-12-08 15:08  ConsoleAppBatInsert\ConsoleAppBatInsert.csproj
     文件        5983  2019-08-13 22:48  ConsoleAppBatInsert\Program.cs
     目录           0  2019-08-13 04:44  ConsoleAppBatInsert\Properties\
     文件        1362  2016-12-08 15:08  ConsoleAppBatInsert\Properties\AssemblyInfo.cs
     目录           0  2019-08-13 04:44  ConsoleAppBatInsert\bin\
     目录           0  2019-08-13 22:49  ConsoleAppBatInsert\bin\Debug\
     文件        8704  2019-08-13 22:49  ConsoleAppBatInsert\bin\Debug\ConsoleAppBatInsert.exe
     文件         234  2016-12-08 16:02  ConsoleAppBatInsert\bin\Debug\ConsoleAppBatInsert.exe.config
     文件       15872  2019-08-13 22:49  ConsoleAppBatInsert\bin\Debug\ConsoleAppBatInsert.pdb
     文件       24224  2016-12-08 17:15  ConsoleAppBatInsert\bin\Debug\ConsoleAppBatInsert.vshost.exe
     文件         234  2016-12-08 16:02  ConsoleAppBatInsert\bin\Debug\ConsoleAppBatInsert.vshost.exe.config
     文件         490  2015-10-30 15:19  ConsoleAppBatInsert\bin\Debug\ConsoleAppBatInsert.vshost.exe.manifest
     文件    17623421  2019-08-13 22:46  ConsoleAppBatInsert\bin\Debug\ECS_nginx_225.log
     目录           0  2019-08-13 04:44  ConsoleAppBatInsert\obj\
     目录           0  2019-08-13 22:49  ConsoleAppBatInsert\obj\Debug\
     文件          42  2019-08-13 22:49  ConsoleAppBatInsert\obj\Debug\ConsoleAppBatInsert.csproj.CoreCompileInputs.cache
     文件        1896  2019-08-13 22:49  ConsoleAppBatInsert\obj\Debug\ConsoleAppBatInsert.csproj.FileListAbsolute.txt
............此处省略10个文件信息

评论

共有 条评论