Application.DoEvents();
                StreamReader sr = new StreamReader(path);
                System.Collections.ArrayList list = new System.Collections.ArrayList();
                progress.Minimum = 0;
                progress.Maximum = y;//怎么獲取文檔是的行數目                while (sr.Peek()!=-1)
                {
                    progress.Value++;
                    list.Add(sr.ReadLine());
                }
                 sr.Close();這樣讀取好慢..因為文件很大上了600M
怎么用多線程加快點速度???

解决方案 »

  1.   


     comm c = new comm();
            private string path;
            private int line = 0;
            public Dataclass(string path)
            {
                this.path = path;
            }
            private int DoWork()
            {
                StreamReader sr = new StreamReader(path);
                while (sr.ReadLine()!=null)
                {
                    line++;
                }
                sr.Close();
                return line;
            }        /// <summary>
            /// 文檔數據讀入Dataset
            /// </summary>
            /// <param name = "tablename">數據表名</param>
            /// <param name = "n">從文檔多少行開始作為數據</param>
            /// <param name = "col">列數</param>
            /// <param name = "ch">文檔路徑</param>
             public DataSet Tablebase(string tablename, int n, int col,char ch,ToolStripStatusLabel rowconut, ToolStripProgressBar progress)
            {
                try
                {               
                    StreamReader sr = new StreamReader(path);
                    progress.Minimum = 0;
                    int rows = DoWork();
                    progress.Maximum = rows;
                    DataSet ds = myset(tablename);
                    string line = "";
                    for (int i = 0; i < rows; i++)
                    {
                        rowconut.Text = "行數:" + i + "/" + rows.ToString();
                        Application.DoEvents();
                        line = sr.ReadLine();
                        progress.Value++;
                        if (i >= n)
                        {
                            string[] srvalue = line.Split(',');
                            DataRow row = ds.Tables[tablename].NewRow();
                            for (int j = 0; j < ds.Tables[tablename].Columns.Count; j++)
                            {
                                row[j] = srvalue[j];
                            }
                            ds.Tables[tablename].Rows.Add(row);
                        }
                    }
                    sr.Close();
                    //if (list.Count == 0)
                    //{
                    //    MessageBox.Show("文檔中沒有數據行!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    //    return null;
                    //}
                    //if (list.Count < n - 1)
                    //{
                    //    MessageBox.Show("開始數據行超過範圍!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    //    return null;
                    //}
                   
                    return ds;
                }
                catch (Exception err) { throw err; }讀取出來後插入數據庫!~~
    想用多線程來讀!~~
    那個大哥給個實例```
      

  2.   

    就知道是搞转换之类的,你需要调整缓冲区大小,例如你可以试试每读n行提交一次数据库,然后配置这个n看看为多少的时候比较合理(速度比较快、占用的内存还能承受),没必要多线程,在循环中加入Application.DoEvents()就不会卡了。
      

  3.   

    我現在不想一行一行的讀取.因為txt裡有時一條數據換行了(意思是本來是一條完整的數據的,但在txt裡卻用兩行來記錄的)
    現在衹能每次讀取30個逗號長度的字條做為一條數據來插入數據庫(txt中數據間的分隔符是逗號)
      

  4.   

    还有,楼主,你知道数据导入导出么?
    Jet 4.0的驱动支持文本文件的,其中的数据项及字段也是逗号分割的。
      

  5.   

    这种一般是先对文本进行预处理,让它符合格式,如果导入到微软的数据库,一个SQL语句就搞定了