使用 oledb 导入 Csv 文件 - 不读取“1.2.3.4”值

Importing Csv file using oledb - not reading "1.2.3.4" value

我想使用 oledb 连接将 csv 文件导入到 C# 中的数据库。我的 csv 文件有 9 列,这一列是 "Version Number",其值为“1.2.3.4”,当我将 csv 文件数据加载到数据表中时,它将此值转换为“1.234”。我想将“1.2.3.4”值保存到数据库中。我试图将列数据类型更改为文本到 csv,但它仍然转换。需要这方面的建议,我该如何解决这个问题。

 OleDbConnectionStringBuilder connectionString = new OleDbConnectionStringBuilder();
        connectionString.Provider = "Microsoft.ACE.OLEDB.12.0";
        if (extension == ".csv")
        {
            connectionString["Extended Properties"] = "text;HDR=Yes;";
            connectionString.DataSource = Path.GetDirectoryName(fileName);
            tableName = Path.GetFileName(fileName);
        }
        else
            if (extension == ".xls")
            {
                connectionString["Extended Properties"] = "Excel 8.0;HDR=Yes;IMEX=1";
                connectionString.DataSource = fileName;
            }
            else
                if (extension == ".xlsx")
                {
                    connectionString["Extended Properties"] = "Excel 12.0 Xml;HDR=YES";
                    connectionString.DataSource = fileName;
                }
OleDbDataAdapter oleda = new OleDbDataAdapter("select * from [{1}]", fileName), 
                   connectionString.ToString());
DataTable dtbCSV = new DataTable();
oleda.Fill(dtbCSV);

Csv 文件数据:

Name                    Policy Category VersionName        ReleaseDate  Description                      DownloadType   Version     FileUrl
What's_New_Release_8.1  Trial       What's_New_Release_8.1  9/1/2016    This is the Description for Downloads   Sku      1.1        https://google.com/image/temp.png
Release 7 0 What's New  Trial       Release 7 0 What's New  10/5/2016   This is the Description for Downloads   Sku      1.23.41.2  http://google.com/image/temp.png

Schema.ini 文件:

[6770aedf-e6b7-44de-afbf-8380f5c450ca.csv] 
 ColNameHeader=True 
 Format=CSVDelimited 
 Col1=Name Text Width 500 
 Col2=Policy Text Width 1000
 Col3=Category Text Width 1000 
 Col4=VersionName Text Width 1000
 Col5=ReleaseDate DateTime
 Col6=Description Text Width 1000
 Col7=DownloadType Text Width 1000
 Col8=Version DateTime
 Col9=FileUrl Text Width 1000

这是我的 schema.ini,它位于我保存 csv 文件的临时文件夹中

提前致谢,
桑迪

嗯,首先我假设你的 csv 文件是用制表符分隔的。 您需要为 OleDb 指定列类型以正确解析它。我认为最好的方法是使用 schema.ini 文件,该文件必须与您的 csv 文件位于同一路径中。这是它的外观示例:

[test.csv]
Format=TabDelimited
ColNameHeader=false
MaxScanRows=0
Col1=Name Text
Col2=Policy Text
Col3=VersionName Text
Col4=ReleaseDate Text
Col5=Description Text
Col6=DownloadType Text
Col7=Version Text
Col8=FileUrl Text

如您所见,您必须在第一行指定您的 csv 文件名。然后我将分隔符设置为制表符。之后,我停用第一行是 headers 行(这将导致您删除 csv 中的第一行)。之后,您必须使用预期类型定义所有列。

希望对您有所帮助。

@Pikoh 已经解释了如何使用 schema.ini 文件来控制驱动程序处理每个字段的对象。

另一种选择是使用像 CsvHelper 这样的库,它读取 将 CSV 数据直接映射到对象,例如:

var csv = new CsvReader( textReader );
IEnumerable<MyClass> records = csv.GetRecords<MyClass>();

GetRecords returns 一个 IEnumerable,这意味着延迟加载行 并且 您可以使用 LINQ 过滤它们或仅加载特定字段:

var records = from record in csv.GetRecords<MyClass>()
              where record.ReleseDate > new DateTime(2016,09,01)
              select new {record.Name};

字段可以自动映射,或者您可以使用流畅的界面指定您自己的映射。映射 class 还允许您指定本地化选项,例如处理 MM/DD/YYYYDD/MM/YYYY 格式:

public sealed class MyClassMap : CsvClassMap<MyClass>
{
    public MyClassMap()
    {
        Map( m => m.Description ).Index( 0 )
                           .TypeConverterOption( CultureInfo.InvariantCulture );
        Map( m => m.TimeStamp ).Index( 1 )
                          .TypeConverterOption( DateTimeStyles.AdjustToUniversal );
        Map( m => m.Cost ).Index( 2 )
                          .TypeConverterOption( NumberStyles.Currency );
        Map( m => m.CurrencyFormat ).Index( 3 )
                          .TypeConverterOption( "C" );
        Map( m => m.BooleanValue ).Index( 4 )
                          .TypeConverterOption( true, "sure" )
                          .TypeConverterOption( false, "nope" );
    }
}

最后,您避免了安装正确的 OLEDB 驱动程序(x86 或 x64)的麻烦。