简体   繁体   English

CSV 文件无法上传 - C#、Sqlbulkcopy

[英]CSV file doesn't upload - C# , Sqlbulkcopy

I have an issue where I am unable to upload a CSV file using C# sqlbulkcopy.我遇到了无法使用 C# sqlbulkcopy 上传 CSV 文件的问题。 C# code is correct, files are getting uploaded when in correct format. C# 代码正确,文件以正确的格式上传。 Tested it.测试了一下。

Reason: These csv files, may be, are not in valid format.原因:这些 csv 文件的格式可能无效。 Please see pic below, here you can see that the highlighted part is my column row and where the highlighting ends there starts a new data row which is not in new line.请参见下图,在这里您可以看到突出显示的部分是我的列行,突出显示结束的地方开始了一个不在新行中的新数据行。 So, it is a comma separated value sheet but values are in a single line, which throws me error "The given ColumnName does not match up with any column in data source" while uploading .因此,它是一个逗号分隔的值表,但值在一行中,这会在上传 .

Please click on image for larger view.请点击图片查看大图。

在此处输入图片说明

If I open it in excel and save it by pressing Ctrl+S, a popup opens and says that it is not valid CSV and when I click yes and close the sheet it asks to save and replace the file in destination.如果我在 excel 中打开它并按 Ctrl+S 保存它,则会打开一个弹出窗口并说它不是有效的 CSV,当我单击是并关闭工作表时,它会要求保存并替换目标中的文件。 After saving it and then opening in notepad there I could see that the content is in now correct format which uploads successfully using C#.保存它然后在记事本中打开后,我可以看到内容现在是正确的格式,可以使用 C# 成功上传。 Like below image:如下图所示:

在此处输入图片说明

在此处输入图片说明

When I put cursor between Over40hrs and 200 (image 1) and press backspace once nothing happens, seems like there is some character.当我将光标放在 Over40hrs 和 200(图 1)之间并在没有任何反应后按退格键时,似乎有一些字符。 This is happening for all rows.所有行都会发生这种情况。 Can we play with that character and save it in valid format ?我们可以玩那个角色并将其保存为有效格式吗?

Is there any way by which I can save the contents in valid format when uploading in C# asp.net ?在 C# asp.net 中上传时,有什么方法可以以有效格式保存内容? I couldn't find such question on internet.我在互联网上找不到这样的问题。 Please help.请帮忙。

看起来记事本无法显示该字符,您可以将其替换为String.Replace 将所有文件读入变量,将这些字符替换为 Environment.NewLine 以新名称保存此文件并上传新文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM