data error when reading csv file in C# winforms(在 C# winforms 中读取 csv 文件时出现数据错误)
问题描述
我有一个 C# winforms 正在从 csv 文件中读取一列.它正确读取了 4 列中的 3 列.csv 文件中的第 4 列是 S4,但数据集显示的是 4.
I have a C# winforms that is reading a column from a csv file. It reads 3 of the 4 columns correct. The 4th column in the csv file is S4, but the dataset is displaying 4.
代码是:
string conn = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0; Data"
+ "Source={0}; Extended Properties=""text;HDR=YES;FMT=DELIMITED""",
strDirectoryPath);
OleDbConnection oleDBConn = new OleDbConnection(conn);
oleDBConn.Open();
OleDbDataAdapter da = new OleDbDataAdapter("Select * FROM [" + strFileName + "]",
conn);
DataSet ds = new DataSet();
da.Fill(ds);
csv 数据样本为:
AA0013 Incident Incident S4
AA0016 Incident Incident S3
AA0017 Incident Incident S3
AA0023 Incident Incident S3
AA0076 Issue Issue S3
AA0079 Incident Incident S6
AA0082 Issue Issue S6
AA0084 Incident Incident S6
AA0085 Incident Incident S6
这是什么原因造成的,我该如何解决?
What would cause this and how can I resolve it?
推荐答案
这是因为有时 OLEDB 提供程序会自动检测列的数据类型,并尝试将该列中的所有值转换为它检测到的特定数据类型.要解决此问题,您需要指定 schema.ini 文件,该文件将保存有关每列及其数据类型的信息,以便 OLEDB 不会尝试将任何列隐式转换为自己喜欢的数据类型:)...
This is because some times OLEDB provider auto detect the data type of the column and try to convert all the values in that column to a specific data type it detects. to solve this problem you need to specify the schema.ini file that will hold information about each column and its data type so that OLEDB dont try to implicitly convert any column to its own favorite data type :)...
这里是完整的指南..http://www.aspdotnetcodes.com/Importing_CSV_Database_Schema.ini.aspx
问候.
这篇关于在 C# winforms 中读取 csv 文件时出现数据错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C# winforms 中读取 csv 文件时出现数据错误
基础教程推荐
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- WPF 模态进度窗口 2022-01-01
