public static void Main(string[] args)
        {
            string strPath = "F:\\Download\\600.txt";
            int lines = 1;
            Console.WriteLine("正在读取行数...."+DateTime.Now.ToString());
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", System.Type.GetType("System.Int32"));
            dt.Columns.Add("UserName",System.Type.GetType("System.String"));            
            dt.Columns.Add("Password", System.Type.GetType("System.String"));
            dt.Columns.Add("Email", System.Type.GetType("System.String"));

            using (var sr = new StreamReader(strPath))
            {
                var ls = "";
                while ((ls = sr.ReadLine()) != null)
                {
                    if (ls.Contains("#"))
                    {
                        string[] templine = ls.Split('#');
                        if (templine.Length == 3)
                        {
                            DataRow item = dt.NewRow();
                            item["ID"] = lines;
                            item["UserName"] = templine[0].Trim();
                            item["Password"] = templine[1].Trim();
                            item["Email"] = templine[2].Trim();
                            dt.Rows.Add(item);
                            lines++;
                        }
                    }
                }
            }

            if (dt.Rows.Count > 0)
            {
                string strConnection = "server=localhost;database=Data;uid=sa;pwd=123456;Connect Timeout=50000";
                SqlBulkCopyInsert(strConnection, "tb_csdn_data", dt);
            }

            Console.WriteLine("结束读取行数" + dt.Rows.Count + "...." + DateTime.Now.ToString());
            Console.ReadLine();
        }

        public static void SqlBulkCopyInsert(string strConnection, string strTableName, DataTable dt)
        {
            try
            {
                using (SqlBulkCopy sqlRevdBulkCopy = new SqlBulkCopy(strConnection))
                {
                    sqlRevdBulkCopy.BulkCopyTimeout = 50000;
                    sqlRevdBulkCopy.DestinationTableName = strTableName; 
                    sqlRevdBulkCopy.NotifyAfter = dt.Rows.Count;
                    sqlRevdBulkCopy.WriteToServer(dt);
                    sqlRevdBulkCopy.Close(); 
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

  

posted on 2019-08-19 16:03  风浪  阅读(571)  评论(0编辑  收藏  举报