当读取使用一个DataReader和OLEDB射流的数据提供一个CSV文件,我怎么能控制列数据类型?射流、提供一个、数据类型、文件

2023-09-02 01:33:08 作者:蒹葭烫酒

在我的C#应用​​程序我正在使用Microsoft Jet OLEDB数据提供程序读取CSV文件。连接字符串看起来是这样的:

In my C# application I am using the Microsoft Jet OLEDB data provider to read a CSV file. The connection string looks like this:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:Data;Extended Properties="text;HDR=Yes;FMT=Delimited

我打开使用连接字符串,然后选择所有行从CSV文件的命令在ADO.NET OleDbConnection:

I open an ADO.NET OleDbConnection using that connection string and select all the rows from the CSV file with the command:

select * from Data.csv

当我打开一个OleDbDataReader并检查它返回的列的数据类型,我觉得这东西在堆栈中试图猜测基于数据文件中的第一行中的数据类型。例如,假设CSV文件包含:

When I open an OleDbDataReader and examine the data types of the columns it returns, I find that something in the stack has tried to guess at the data types based on the first row of data in the file. For example, suppose the CSV file contains:

House,Street,Town
123,Fake Street,Springfield
12a,Evergreen Terrace,Springfield

调用OleDbDataReader.GetDataTypeName方法众议院列将显示,列已给出的数据类型DBTYPE_I4,所以从它读取的所有值都PTED为整数除$ P $。我的问题是房子应该是一个字符串 - 当我尝试从第二行读取内务值时,OleDbDataReader返回null

Calling the OleDbDataReader.GetDataTypeName method for the House column will reveal that the column has been given the data type "DBTYPE_I4", so all values read from it are interpreted as integers. My problem is that House should be a string - when I try to read the House value from the second row, the OleDbDataReader returns null.

如何,我可以告诉任何Jet数据库提供商或OleDbDataReader间preT一列字符串,而不是数字?

How can I tell either the Jet database provider or the OleDbDataReader to interpret a column as strings instead of numbers?

推荐答案

还有,你可以创建一个会告诉ADO.NET如何跨preT的CSV模式文件 - 实际上给它的结构。

There's a schema file you can create that would tell ADO.NET how to interpret the CSV - in effect giving it a structure.

试试这个:http://www.aspdotnet$c$cs.com/Importing_CSV_Database_Schema.ini.aspx

 
精彩推荐
图片推荐