2016-06-15 55 views
1

我一直在這個代碼創建一個CSV導入,可以通過什麼現在我遇到了一個web應用程序做的是CSV文件的第一行標題,當我做導入我是第一行不在SqlBulkCopy中。這是代碼。跳過第一行File.ReadAllText爲SqlBulkCopy的

public partial class CS : System.Web.UI.Page 
{ 
    protected void Upload(object sender, EventArgs e) 
    { 
     //Upload and save the file 
     string csvPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName); 
     FileUpload1.SaveAs(csvPath); 

     DataTable dt = new DataTable(); 
     dt.Columns.AddRange(new DataColumn[15] { new DataColumn("ORGANIZATION_ID", typeof(int)), 
      new DataColumn("INVENTORY_ITEM_ID", typeof(int)), 
      new DataColumn("ITEM_CODE", typeof(char)), 
      new DataColumn("SUBINVENTORY_CODE", typeof(char)), 
      new DataColumn("LOCATOR_ID", typeof(int)), 
      new DataColumn("LOCATOR_CODE", typeof(char)), 
      new DataColumn("QTY", typeof(int)), 
      new DataColumn("FLOWRACK", typeof(char)), 
      new DataColumn("LOCATOR_POSITION", typeof(char)), 
      new DataColumn("LOCATOR_BIN_LEVEL", typeof(char)), 
      new DataColumn("PICKING_ORDER", typeof(int)), 
      new DataColumn("ITEM_BOX_QUANTITY", typeof(int)), 
      new DataColumn("AVAILABLE_BOX_QTY", typeof(int)), 
      new DataColumn("AVAILABLE_MOD_QTY", typeof(int)), 
      new DataColumn("CreateTime", typeof(DateTime)) }); 
     string csvData = File.ReadAllText(csvPath); 
     foreach (string row in csvData.Split('\n')) 
     { 
      if (!string.IsNullOrEmpty(row)) 
      { 
       dt.Rows.Add(); 
       int i = 0; 
        foreach (string cell in row.Split(',')) 
       { 
         dt.Rows[dt.Rows.Count - 1][i] = cell; 
         i++; 

       } 
      } 
     } 

     string consString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; 
     using (SqlConnection con = new SqlConnection(consString)) 
     { 
      using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con)) 
      { 
       //Set the database table name 
       sqlBulkCopy.DestinationTableName = "dbo.Inventory"; 
       con.Open(); 
       sqlBulkCopy.WriteToServer(dt); 
       con.Close(); 
      } 
     } 
    } 
} 

現在我得到的錯誤是

Exception Details: System.FormatException: Input string was not in a correct format.

源錯誤:

Line 46: foreach (string cell in row.Split(',')) Line 47: { Line 48: dt.Rows[dt.Rows.Count - 1][i] = cell; Line 49: i++; Line 50:

我也使用SQL Server 2008 有人能幫助我嗎?

+0

我仍然有問題,現在我得到的錯誤是Microsoft(R)Visual C#2010編譯器版本4.0.30319.1版權所有(C)Microsoft Corporation。版權所有。 C:\的Inetpub \ wwwroot的\ CSV_insert \ CS.aspx.cs(60,66):錯誤CS1502:關於 'char.GetNumericValue(炭)' 最好的重載的方法匹配具有一些無效參數C:\的Inetpub \ wwwroot的\ CSV_insert \ CS.aspx.cs(60,87):錯誤CS1503:參數1:不能從'字符串'轉換爲'字符'任何人有任何想法? –

+0

我在哪裏有typeof字符我想我實際上可能需要typeof字符串作爲例子item_code將dmsmh.en.cd。那麼如何修改代碼以這種方式工作呢? –

回答

2

使用File.ReadAllLines()而不是爲File.ReadAllText(csvPath)。這有助於您跳過拆分操作以及第一行;請參見下面的代碼:

List<string> csvDataList = File.ReadAllLines(csvPath).Skip(1).ToList(); 

現在csvDataList是,除了第一行的行的列表,你可以通過這些行重複做同樣的功能:

迭代舉例:

foreach (string Csv in csvDataList) 
{ 
    DataRow dRow = dt.NewRow(); 
    int i = 0; 
    foreach (string cell in Csv.Split(',')) 
    { 
      if (dRow[i].GetType() == typeof(int)) 
      { 
       int cellValue = 0; 
       if (Int32.TryParse(cell, out cellValue)) 
       { 
        dRow[i++] = cellValue; 
       } 
      } 
      else if (dRow[i].GetType() == typeof(char) && cell.Length == 1) 
      { 
       dRow[i++] = cell[0]; 
      } 
      else // Which means that the cell is of type int 
       dRow[i++] = (int)Char.GetNumericValue(cell[0]); 
    } 
    dt.Rows.Add(dRow); 
} 
+0

當我這樣做,我得到:**編譯器錯誤消息:CS1061:「System.Collections.Generic.List 」不包含「拆分」的定義,並沒有擴展方法「分裂」接受一個類型的第一個參數「系統.Collections.Generic.List 「可以找到(是否缺少using指令或程序集引用?)** –

+0

我已經更新了樣品迭代的代碼,請通過該 –

+0

好了,我們取得了一些!現在我有: '異常詳細信息:System.FormatException:輸入字符串的格式不正確。 源錯誤: 線45:的foreach(在Csv.Split串小區( '')) 46行:{ 線47:卓爾[0] =細胞; 第48行:} 第49行:dt.Rows.Add(dRow);' Error on Line:47 –