在 C# winforms 中读取 csv 文件时出现数据错误 [英] data error when reading csv file in C# winforms

查看:23
本文介绍了在 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 文件时出现数据错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆