Я работаю над этим кодом для создания CSV-импорта, который может быть выполнен через webapp, теперь то, что я запускаю, - это первая строка файла 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 Может кто-то помочь мне?
У меня все еще есть проблема, теперь ошибка, которую я получаю, это 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 (char)' имеет некоторые недопустимые аргументы c: \ inetpub \ wwwroot \ CSV_insert \ CS.aspx.cs (60,87): ошибка CS1503: Аргумент 1: невозможно преобразовать из 'string' в 'char', у кого есть какие-либо идеи? –
Если у меня есть typeof char, мне кажется, что на самом деле может понадобиться строка типаof в качестве примера item_code будет dmsmh.en.cd. Итак, как мне изменить код для работы таким образом? –