2016-06-15 88 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 –