[英]Skip first line File.ReadAllText for SqlBulkCopy
I have been working on this code to create a CSV Import that can be done through a webapp now what I am running into is the first row of the CSV file has headers and when I do the import I was that first line to not be in the SqlBulkCopy. 我一直在使用此代码来创建CSV导入,现在可以通过webapp完成此操作,我正在运行的是CSV文件的第一行具有标题,当我执行导入时,我就是第一行不在SqlBulkCopy。 Here is the code. 这是代码。
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();
}
}
}
}
Now the error I get is 现在我得到的错误是
Exception Details: System.FormatException: Input string was not in a correct format. 异常详细信息:System.FormatException:输入字符串的格式不正确。
Source Error: 源错误:
Line 46: foreach (string cell in row.Split(',')) Line 47: { Line 48: dt.Rows[dt.Rows.Count - 1][i] = cell; 第46行:foreach(在row.Split(',')中的字符串单元格)第47行:{第48行:dt.Rows [dt.Rows.Count-1] [i] =单元格; Line 49: i++; 第49行:i ++; Line 50: 第50行:
I am also using sql server 2008 Can someone help me out? 我也在使用sql server 2008,有人可以帮我吗?
Use File.ReadAllLines()
instead for File.ReadAllText(csvPath)
. 使用File.ReadAllLines()
代替File.ReadAllText(csvPath)
。 Which helps you to skip the Split operation as well as the First line; 这可以帮助您跳过“拆分”操作以及第一行; See the code below: 请参见下面的代码:
List<string> csvDataList = File.ReadAllLines(csvPath).Skip(1).ToList();
Now the csvDataList
is a list of rows except the first row, you can iterate through those rows and do the same functions: 现在, csvDataList
是除第一行以外的其他行的列表,您可以遍历这些行并执行相同的功能:
Iteration Example: 迭代示例:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.