阅读在C#中的WinForms CSV文件时数据出错 [英] data error when reading csv file in C# winforms

查看:140
本文介绍了阅读在C#中的WinForms CSV文件时数据出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从CSV文件中读取一个列是一个C#的WinForms。它读取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数据样本:

csv data sample is:

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

here is the complete guide.. http://www.aspdotnetcodes.com/Importing_CSV_Database_Schema.ini.aspx

问候。

这篇关于阅读在C#中的WinForms CSV文件时数据出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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