当前位置:编程学习 > C#/ASP.NET >>

导入数据好慢啊、求解释啊


50多万条数据导了半个多小时将近1个一小时
 坑爹  吗的 怎么这么慢。。。。
是TXT文本文件导入数据库  数据还有处理
数据有1000W条数据 要导入
估计一个晚上都不行 只会把服务器崩溃掉。
求大神解释 解决下这个速度
我把源码都贴上来
/// <summary>
        /// 多线程 导入txt 数据
        /// </summary>
        public void DataImport()
        { 
             //得到txt文件的数目
            int iCount = listBox1.Items.Count;

            DataTable dt = new DataTable();

            for (int j = 0; j < iCount; j++)
            {
                //   创建第一个线程  处理第一个txt文件
                //取到第一个txt文件
                string sFile = listBox1.Items[j].ToString();

                DateTime dtLast;
                
                //放进ArrayList中
                ArrayList al = Common.GetFileArray(sFile, out dtLast);

                IndexGong += al.Count;

                Thread Th1 = new Thread(delegate()
                    {
                        DataDispose(al);
                    });
                //启动线程Th1
                Th1.Start();
                //   创建第二个线程  处理第一个 + 1 txt文件
                //   判断是否还有这个文件
                if (++j < iCount)
                {
                    //取到下一个txt文件
                    string sFile1 = listBox1.Items[j].ToString();

                    DateTime dtLast1;

                    //放进ArrayList中
                    ArrayList al1 = Common.GetFileArray(sFile1, out dtLast1);

                    IndexGong += al1.Count;

                    Thread Th2 = new Thread(delegate()
                        {
                            DataDispose(al1);
                        });
                    Th2.Start();
                }
                //   创建第三个线程  处理第一个 + 1 +1 txt文件
                //   判断是否还有这个文件
                if (++j < iCount)
                {
                    //取到下一个txt文件
                    string sFile1 = listBox1.Items[j].ToString();

                    DateTime dtLast1;

                    //放进ArrayList中
                    ArrayList al1 = Common.GetFileArray(sFile1, out dtLast1);


                    IndexGong += al1.Count;

                    Thread Th3 = new Thread(delegate()
                    {
                        DataDispose(al1);
                    });

                    Th3.Start();
                }
                //   创建第四个线程  处理第一个 + 1 + 1 + 1 txt文件
                //   判断是否还有这个文件
                if (++j < iCount)
                {
                    //取到下一个txt文件
                    string sFile1 = listBox1.Items[j].ToString();

                    DateTime dtLast1;

                    //放进ArrayList中
                    ArrayList al1 = Common.GetFileArray(sFile1, out dtLast1);


                    IndexGong += al1.Count;

                    Thread Th4 = new Thread(delegate()
                    {
                        DataDispose(al1);
                    });

                    Th4.Start();
                }
                //   创建第五个线程  处理第一个 + 1 + 1 + 1 + 1 txt文件
                //   判断是否还有这个文件
                if (++j < iCount)
                {
                    //取到下一个txt文件
                    string sFile1 = listBox1.Items[j].ToString();

                    DateTime dtLast1;

                    //放进ArrayList中
                    ArrayList al1 = Common.GetFileArray(sFile1, out dtLast1);

                    IndexGong += al1.Count;

                    Thread Th5 = new Thread(delegate()
                    {
                        DataDispose(al1);
                    });

                    Th5.Start();
                }
            }
            MessageBox.Show("数据导入完成  一共有"+IndexGong+"条 成功导入数据 " + IndexCheng + " 条  失败 "+indexConut+" 条");
        }

        /// <summary>
        /// 获取txt文本数据 并且转换格式
        /// </summary>
        /// <param name="al"></param>
        public void DataDispose(ArrayList al)
        {
            for (int i = 0; i < al.Count; i++)
            {
                //读取第一行的txt文件数据
                string sTemp = al[i].ToString();
                //以空格 截取
                string[] sArr = sTemp.Split(new string[] { "   " }, StringSplitOptions.None);
                if (sArr.Length < 11)
                {
                    sArr = sTemp.Split(new string[] { "\t" }, StringSplitOptions.None);
                }

                CountFrom(sArr);


                
            }
        }


//导入数据库方法
public void CountFrom(string[] sArr)
        {
            try
            {
                string[] SFM = sArr[2].Split('.');
                //时间
                string Time = null;
                if (SFM.Length == 2)
                {
                    if (SFM[1].Length > 3)
                    {
                        SFM[1] = SFM[1].Substring(0, 3);
                    }
                    Time =  SFM[0] + "." + SFM[1];
                }
                else
                {
                    Time =  SFM[0];
                }
                string[] times = null;
                times = Time.Split(':');
                if (times.Length == 3)
                {

                }
                else if(times.Length==2)
                {
                    Time = "00:" + times[0] + ":" + times[1];
                }
                Time = sArr[1] +" "+ Time;
                //纬度
                string[] WD = sArr[3].Split('=');
                string WDu = WD[1];
                //经度
                string[] JD = sArr[4].Split('=');
                string JDu = JD[1];
                //强度
                string[] QD = sArr[5].Split('=');
                string QDu = QD[1];
                //陡度
                string[] DD = sArr[6].Split('=');
                string DDu = DD[1];
                //误差
                string[] WC = sArr[7].Split('=');
                string WCu = WC[1];
                //定位方式
                string[] DW = sArr[8].Split(':');
                string DWu = DW[1];
                //省
                string[] Sheng = sArr[9].Split(':');
                string ShengY = Sheng[1];
                //市
                string[] Shi = sArr[10].Split(':');
                string ShiY = Shi[1];
                //县
                string[] Xian = sArr[11].Split(':');
                string XianY = Xian[1];

                //获取省邮编
                string sqlSheng = string.Format("select top 1 zipcode.zip from provinces " +
                            "join  cities on provinces.provinceid=cities.provinceid " +
                            "join areas on cities.cityid=areas.cityid " +
                            "join zipcode on areas.areaid=zipcode.areaid  " +
                            "where province='{0}'", ShengY);
                //获取市邮编
                string sqlShi = string.Format("select top 1 zipcode.zip from provinces " +
                             "join  cities on provinces.provinceid=cities.provinceid " +
                             "join areas on cities.cityid=areas.cityid " +
                             "join zipcode on areas.areaid=zipcode.areaid  " +
                             "where province='{0}' and cities.city='{1}'", ShengY, ShiY);
                //获取县邮编
                string sqlXian = string.Format("select top 1 zipcode.zip from provinces " +
                             "join  cities on provinces.provinceid=cities.provinceid " +
                             "join areas on cities.cityid=areas.cityid " +
                             "join zipcode on areas.areaid=zipcode.areaid  " +
                             "where province='{0}' and cities.city='{1}' and areas.area='{2}'", ShengY, ShiY, XianY);
                //判断是否是国外 国外邮编为空
                if (!ShengY.Equals("国外"))
                {
                    ShengY = ReturnZipcode(sqlSheng);
                }
                else
                {
                    ShengY = "null";
                }
                //判断是否是国外 国外邮编为空
                if (!ShiY.Equals("国外"))
                {
                    ShiY = ReturnZipcode(sqlShi);
                }
                else
                {
                    ShiY = "null";
                }
                //判断是否是国外 国外邮编为空
                if (!XianY.Equals("国外"))
                {
                    XianY = ReturnZipcode(sqlXian);
                }
                else
                {
                    XianY = "null";
                }
                //导入数据库sql语句
                string sql = string.Format("INSERT INTO [CLRAS].[dbo].[T_LIGHTNING]([OCCURTIME],[JINDU],[WEIDU] ,[INTENSITY],[GRADIENT]  ,[CHARGE]" +
                                            ",[ENERGY],[OFFSET],[LOCATEMODE],[OCCURNS] ,[TODAYID],[PROVINCE],[COUNTY],[CITY],[GRIDKEY])" +
                                            "VALUES('{0}',{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14})",
                                            Time, WDu, JDu, QDu, DDu, WCu, DWu, 0, 0, 0, 0, 0, ShengY == null ? "null" : ShengY, ShiY == null ? "null" : ShiY, XianY == null ? "null" : XianY);
                try
                {
                    Database db = DatabaseFactory.CreateDatabase();
                    SqlCommand cmd = new SqlCommand(sql);
                    if (db.ExecuteNonQuery(cmd) > 0)
                    {
                        IndexCheng++;
                    }
                }
                catch (Exception ex)
                {

                    indexConut++;
                }
            }
            catch (Exception ex)
            {
                indexConut++;
            }
        }

--------------------编程问答-------------------- 大数据量用SqlBulkCopy 
http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlbulkcopy%28v=vs.80%29.aspx
  
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,