在Windows Server 2008中解析CSV文件64 C# [英] Parsing CSV file in Windows Server 2008 64 C#
问题描述
我在Windows Server 2008 64位版本中解析* .CSV文件时遇到了一些问题。问题是Jet OLEDB 4.0不会读取CSV中显示的标题行。
这意味着,当我尝试访问其中一个列:
DataTable tbl = GetCsvData();
string sd = tbl.Rows [0] [id]。ToString();
程序引发异常,表示该列不属于datable。
我使用下面的代码:
public DataTable GetCsvData b $ b FileInfo file = new FileInfo(this.fileName);
string connectionString =Provider = Microsoft.Jet.OLEDB.4.0; +
Data Source =+ file.DirectoryName +\\; +
Extended Properties = \text; HDR = Yes; FMT = Delimited(;)\;;
OleDbConnection objConn = new OleDbConnection(connectionString);
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand(string.Format(SELECT * FROM [{0}],file.Name),objConn);
OleDbDataAdapter adp = new OleDbDataAdapter(objCmdSelect);
DataTable tbl = new DataTable(CSVData);
adp.Fill(tbl);
objConn.Close();
objConn.Dispose();
return tbl;
}
可以看到,扩展属性是正确的:HDR = ,这迫使Jet引擎读取CSV文件上的标题行。
问题真的很奇怪,因为我可以在开发机器上读取相同的CSV文件XP SP3),完全没有问题。
我认为这是一个问题导出到64位版本的Windows服务器2008年。
我检查了服务器和本地机器上msjet40.dll文件的版本:
Windows XP SP3 => 4.0.9551
Windows Server 2008 64位=> 4.0.9755
问题不在于CSV文件,它在由Microsoft至少是我的想法),因为我可以在我的机器上完美地读取CSV文件。
有没有任何想法一种方法来解决这个问题?
您可能需要查看快速CSV阅读器 a>或 FileHelpers库,而不使用JET OLEDB。
I'm having some trouble parsing a *.CSV file in windows server 2008 64bit version. The problem it that Jet OLEDB 4.0 doesn't read the header row, presented in the CSV.
That means, that when I try to access one of the columns like this:
DataTable tbl = GetCsvData();
string sd = tbl.Rows[0]["id"].ToString();
The program throws an exception, saying that the column does not belong in the datable.
I'm using the following code:
public DataTable GetCsvData() {
FileInfo file = new FileInfo(this.fileName);
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + file.DirectoryName + "\\;" +
"Extended Properties=\"text;HDR=Yes;FMT=Delimited(;)\";";
OleDbConnection objConn = new OleDbConnection(connectionString);
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand(string.Format("SELECT * FROM [{0}]", file.Name), objConn);
OleDbDataAdapter adp = new OleDbDataAdapter(objCmdSelect);
DataTable tbl = new DataTable("CSVData");
adp.Fill(tbl);
objConn.Close();
objConn.Dispose();
return tbl;
}
As you can see, the Extended Properties are correct: "HDR=Yes", this forces the Jet engine to read the header row on the CSV file.
The problem is really strange because I can read the same CSV file on my development machine ( Windows XP SP3 ), with absolutely no problem.
I think this is a problem derived to the 64 bit version of Windows server 2008.
I checked the versions of the msjet40.dll file on both the server and local machine:
Windows XP SP3 => 4.0.9551
Windows Server 2008 64 bit => 4.0.9755
The problem isn't on the CSV file, it's in the driver provided by Microsoft ( at least is what I think ), since I can read the CSV file perfectly on my machine.
Does one has any idea a way to solve this problem? I googled a lot, but I couldn't find anything.
Thanks..
You might want to have a look at "Fast CSV Reader" or "FileHelpers library" instead of using JET OLEDB.
这篇关于在Windows Server 2008中解析CSV文件64 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!