使用以下代码可以将Excel读入DataSet,怎样才可以将Txt度入DataSet,使用者的资料大多是Txt的,而且经常超过650000行,转成Excel反而比较麻烦,在Extended Properties后面加上Txt也不可以。string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=D:\\a.xls;" + "Extended Properties=Excel 8.0;";
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, "ExcelInfo");

解决方案 »

  1.   

    先读入DataTable吧。没有上面那么简单了,需要自己写代码处理以下。
      

  2.   

    其實很簡單namespace TxtTablebase
    {
        class Dataclass
        {
            comm c = new comm();
            /// <summary>
            /// 文檔數據讀入Dataset
            /// </summary>
            /// <param name = "tb">數據庫名</param>
            /// <param name = "tablename">數據表名</param>
            /// <param name = "path">文檔路徑</param>
            /// <param name = "n">從文檔多少行開始作為數據</param>
            public DataSet Tablebase(string tablename, string path, int n, ToolStripStatusLabel rowconut, ToolStripProgressBar progress)
            {
                try
                {
                    StreamReader sr = new StreamReader(path);
                    System.Collections.ArrayList list = new System.Collections.ArrayList();
                    while (sr.Peek() > -1)
                    {
                        list.Add(sr.ReadLine());
                    }
                    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;
                    }
                    rowconut.Text = "行數:" + (list.Count - n).ToString();
                    Application.DoEvents();
                    progress.Minimum = 0;
                    progress.Maximum = list.Count - n;
                    DataSet ds = myset(tablename);
                    for (int i = 0; i < list.Count - n; i++)
                    {
                        string[] srvalue = list[n + i].ToString().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);
                        progress.Value = i;
                    }
                    int cd = ds.Tables[0].Rows.Count;
                    return ds;
                }
                catch (Exception err) { throw err; }
                finally
                {
                    c.sqlConn.Close();
                }        }
            public DataSet myset(string tablename)
            {
                try
                {
                    SqlDataAdapter da = new SqlDataAdapter("select * from " + tablename, c.sqlConn);
                    DataSet ds = new DataSet();
                    da.Fill(ds, tablename);
                    return ds;
                }
                catch (Exception err) { throw err; }
            }
        }
      

  3.   

    http://www.connectionstrings.com/?carrier=textfile
    在这个里面找到了,现在可以了明天在研究wwwiii520,谢谢
      

  4.   

    建议读到DataTable后立即存库,然后再操作,