Csharp 讀取大文本文件數據到DataTable中,大批量插入到數據庫中

文本文件中的內容格式為:(其中問題,誰能否解決大於15M的文本文件能快速讀到DataTable中)

俚    rustic
俚语    slang
哩    mile
哩    onomat
哩数    milage
逦    winding
理    logic
理    reason
理    science
理八郎    Rihachiro
理兵卫    Rihee
理兵卫    Rihei
理财    financing
理财    portfolio
理查    Richard
理查德    Richard
理查德冰川    Richardsbreen
理查顿    Richardton
理查多    Richardo
理查多维奇    Richardovich
理查姆斯    Richarms
理查森    Richardson
理查特    Richart
理查特    Richarte
理查兹    Richards
理察    Richard
理次郎    Masajiro
理发    airdressing
理发    coiffure
理发    hairdressing
理发店    barbers
理发师    barber
理法    logos
理蕃    barbarians
理工    sciences
理光    Ricoh
理化    chemistry
理化学研究所    RIKEN
理会    notice
理吉郎    Rikichiro
理解    comprehend
理解    comprehension
理解    understand
理解    understandable
理解    understanding
理解力    comprehension
理解能力    savvy
理解人    understanding
理经济学    Matekon
理开    sleave
理理恩龙    Liliensternus
理疗医师    physiatrician
理论    theories
理论    theory
理论地    theoretically
理论化    theorise
理论家    doctrinarian
理论家    theoretician
理论家    theorist
理论上    academically
理论上    theoretically
理念    concept
理念    concepts
理念    ideal
理念    ideals
理念    ideas
理赔    compensate
理七郎    Rishichiro
理三郎    Risaburo
理十郎    Rijuro
理市郎    Riichiro
理事    director
理事    syndic
理事长    director
理事长    frata
理事会    council
理事会    meeting
理事之职    directorate
理四郎    Rishiro
理松庵    Rishoan
理所当然    behoove
理所当然    naturally
理所当然    proper
理太郎    Ritaro
理塘    Litang
理夏德    Ryszard
理想    ideal
理想    Ideal
理想    idealism
理想    idealists
理想    ideals
涂聚文  GeovinDu
涂斯博  SiboDu
理想报    Ideal
理想报    Kalpana
理想标准公司    Ideal-standard
理想地    ideally
理想国    utopia
理想国    Utopia
理想化    idealisation
理想化    idealization
理想化    idealize
理想美    beauideal
理想目标    dreamboat
理想坡莫合金    idealoy
理想人物    dreamboat
理想上    ideally
理想主义    idealism
理想主义地    idealistically
理想主义者    idealist
理性    rational
理性    rationality
理性    reason
理性论    rationalism
理性论者    rationalist
理性主义    rationalism
理性主义者    rationalist
塗聚文  GeovinDu

        /// <summary>
        /// 從文本文件中讀取數據到DatatTable中
        /// 塗聚文 2011-11-22
        /// 締友計算機信息技術有限公司 捷為工作室
        /// </summary>
        /// <returns></returns>
        private DataTable CreateDataTableFromFile()
        {
            DataTable dt = new DataTable();
            DataColumn dc;
            

            dc = new DataColumn();
            dc.DataType = System.Type.GetType("System.Int32");
            dc.ColumnName = "序号";
            dc.Unique = false;
            dt.Columns.Add(dc);
            dc = new DataColumn();
            dc.DataType = System.Type.GetType("System.String"); 
            dc.ColumnName = "中文";
            dc.Unique = false;
            dt.Columns.Add(dc);
            dc = new DataColumn();
            dc.DataType = System.Type.GetType("System.String");
            dc.ColumnName = "英文";
            dc.Unique = false;
            dt.Columns.Add(dc);

            StreamReader sr = new StreamReader(file);//text文件地址 大的文件,當讀出到DatatTable中,可以批理插入數據庫中
            string input;
            string cn = string.Empty;
            string en = string.Empty;
            int index = 0;
            while ((input = sr.ReadLine()) != null)
            {
                this.richTextBox1.AppendText(input + "\r\n"); 
                string[] s = input.Split(new char[] { '\t',' ' });
                //dr = dt.NewRow();
                //dr["序号"] = index;
                for (int i = 0; i < input.Length; i++)
                {
                    if (i == 0)
                    {
                        cn = s[i].ToString();
                        //dr["中文"] = s[i].ToString();
                    }
                    if (i == 1)
                    {
                        en = s[i].ToString();
                        //dr["英文"] = s[i].ToString();
                    }
                }               
                
                //dr["c4"] = s[3];
                //dt.Rows.Add(dr);
                dt.Rows.Add(index, cn, en);
                index++;
                Application.DoEvents();       
            }
            sr.Close();
            return dt;
        }

 

        /// <summary>
        /// DatatTable數據批理插入數據庫中
        /// 塗聚文 2011-11-22
        /// 締友計算機信息技術有限公司 捷為工作室
        /// </summary>
        private void StartImport()
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.TableLock);
            bulkCopy.DestinationTableName = "GeovinDuDictionary";
            bulkCopy.BatchSize = 1000000;  //每一批次產生的行數
            bulkCopy.NotifyAfter = 1000;
            bulkCopy.ColumnMappings.Add("中文", "WordChina"); //
            bulkCopy.ColumnMappings.Add("英文", "WordEnglish");
            bulkCopy.WriteToServer(CreateDataTableFromFile());
            sw.Stop();
            richTextBox1.Text = (sw.ElapsedMilliseconds / 1000.00).ToString();
        }

 

你可能感兴趣的:(Datatable)