2012-02-08 24 views
1

我們有一個包含2500列和2000行的數據表。當我們嘗試使用OLEDB導出時,出現「定義的字段太多」的錯誤。我不能使用Excel Inter操作,因爲它消耗更多時間。有沒有其他辦法可以解決這個問題使用OLEDB將數據表導出到Excel會拋出太多定義的字段錯誤

using (OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileName + ";" + "Extended Properties=" + "\"" + "Excel 8.0;HDR=NO;" + "\"")) 
       { 
        con.Open(); 
        OleDbCommand cmdInsert; 
        if (createTable || !File.Exists(fileName)) 
        { 
         sql = "CREATE TABLE " + tableName + " ("; 
         for (int i = 0; i < tbl.Columns.Count; i++) 
         { 
          sql += "[" + tbl.Columns[i].ColumnName + "]"; 
          if (i + 1 == tbl.Columns.Count) //Here we decide should we close insert command or appebd another create column command 
           sql += " " + GetColumnType(tbl.Columns[i]) + ")"; //Close insert 
          else 
           sql += " " + GetColumnType(tbl.Columns[i]) + ","; //there is more columns to add 
         } 
        } 
        if (!String.IsNullOrEmpty(sql)) 
        { 
         cmdInsert = new OleDbCommand(sql, con); 
         cmdInsert.ExecuteNonQuery(); 
        } 
        foreach (DataRow row in tbl.Rows) 
        { 
         //Dodati parametre na comandu 
         string values = "("; 
         for (int i = 0; i < tbl.Columns.Count; i++) 
         { 
          if (i + 1 == tbl.Columns.Count) 
          { 
           if (tbl.Columns[i].DataType == System.Type.GetType("System.Decimal") || 
            tbl.Columns[i].DataType == System.Type.GetType("System.Int64") || 
            tbl.Columns[i].DataType == System.Type.GetType("System.Double")) 
            values += String.IsNullOrEmpty(row[i].ToString()) ? "0)" : Convert.ToDecimal(row[i]).ToString("#0.00", _infoEn) + ")"; 
           else 
            values += "'" + System.Security.SecurityElement.Escape(row[i].ToString()) + "')"; 

           //values += "'" + "test" + "')"; 
          } 
          else 
          { 
           if (tbl.Columns[i].DataType == System.Type.GetType("System.Decimal") || 
            tbl.Columns[i].DataType == System.Type.GetType("System.Int64") || 
            tbl.Columns[i].DataType == System.Type.GetType("System.Double")) 
            values += String.IsNullOrEmpty(row[i].ToString()) ? "0," : Convert.ToDecimal(row[i]).ToString("#0.00", _infoEn) + ","; 
           else 
            values += "'" + System.Security.SecurityElement.Escape(row[i].ToString()) + "',"; 

           //values += "'" + "test" + "',"; 
          } 
         } 
         string sqlInsert = String.Format("Insert into [{0}$] VALUES {1}", tableName, values); 
         cmdInsert = new OleDbCommand(sqlInsert, con); 

         cmdInsert.ExecuteNonQuery(); 
        } 
       } 

回答

1

我發現使用oledb的限制是255列。因此,我正在轉換數據表到CSV使用下面的代碼,並將其寫入到HTTP頁面

public void CreateCSVFile(System.Data.DataTable dt, out StringWriter stw) 
     { 
      stw = new StringWriter(); 
      int iColCount = dt.Columns.Count; 
      for (int i = 0; i < iColCount; i++) 
      { 
       stw.Write(dt.Columns[i]); 
       if (i < iColCount - 1) 
       { 
        stw.Write(","); 
       } 
      } 
      stw.Write(stw.NewLine); 
      foreach (DataRow dr in dt.Rows) 
      { 
       for (int i = 0; i < iColCount; i++) 
       { 
        if (!Convert.IsDBNull(dr[i])) 
        { 
         stw.Write(dr[i].ToString()); 
        } 
        if (i < iColCount - 1) 
        { 
         stw.Write(","); 
        } 
       } 
       stw.Write(stw.NewLine); 
      } 
     } 

現在我能夠解決出口龐大的數據

+0

你知道問題在那裏記錄的響應OleDb的最大列數是255?我遇到了一個類似的問題,從Excel工作表中填充DataTable,該Excel工作表中有一堆空的但已初始化的列。 – Jim 2012-07-26 17:32:55

0

Excel(你也連接)是否有限制它通過「表」定義支持的列?那可能是你的瓶頸。

我會建議的是,當你建立你的表,你做它的所有列。我會建議在批處理週期中運行它。調用函數並一次執行100列,並只導出2或3行。因此,可能需要20個循環才能構建出所有2000列,但您可能會在那裏找到突破。如果你得到2000個完整的列,然後展開正在導出的行並找出ROW插入失敗的行。它可能會有一些NULL值在殺死進程時浮動。