我一直在這個代碼創建一個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 有人能幫助我嗎?
我仍然有問題,現在我得到的錯誤是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:不能從'字符串'轉換爲'字符'任何人有任何想法? –
我在哪裏有typeof字符我想我實際上可能需要typeof字符串作爲例子item_code將dmsmh.en.cd。那麼如何修改代碼以這種方式工作呢? –