2

I've tried using the OleDb interfaces to read a CSV file, but I noticed something weird.

var filePath = @"C:\set2\orders3_1.csv";
var fileDirPath = Path.GetDirectoryName(filePath);
SqlBulkCopy bcp = new SqlBulkCopy("Data Source=.; Initial Catalog=indexer; Integrated Security=SSPI");
OleDbConnection cxn = new OleDbConnection();
cxn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='text;HDR=Yes;FMT=Delimited(,)';Data Source=" + fileDirPath;
OleDbCommand cmd = cxn.CreateCommand();
cmd.CommandText = string.Format("Select [OrderNumber] from [{0}]", Path.GetFileName(filePath));
cxn.Open();
var rdr = cmd.ExecuteReader();
bcp.DestinationTableName = "PersonTable";
bcp.WriteToServer(rdr);
rdr.Close();
cxn.Close();

When I put the file name as orders3_1.csv it reads okay. But when I modify the file name to orders3.1.csv the code runs into an exception saying it cannot find the file...

Is this a known issue with the JET providers?

4

2 回答 2

0

是的。Jet 对文件扩展名非常挑剔:

另一个潜在的问题是,默认情况下,您受限于(至少在 Access 中)可以使用的文件扩展名。开箱即用,您可以使用的唯一文件扩展名是 txt、csv、tab 和 asc。

-- http://www.databasejournal.com/features/msaccess/article.php/3853531/Working-with-external-text-files-in-MS-Access.htm

您可以修改注册表以使其接受其他人。

于 2012-10-25T16:12:50.763 回答
0

您必须将整个文件名用引号括起来,但因为这是一个文字字符串,请使用 \" 来转义它们

cxn.ConnectionString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='text;HDR=Yes;FMT=Delimited(,)';Data Source=\"{0}\"",fileDirPath);

更改代码中的上述行,它应该可以工作。

于 2012-10-25T07:48:19.543 回答