如何对长名称 dbf 文件使用 GetOleDbSchemaTable 方法
作为我正在使用 C# 进行的项目的一部分,我需要读取 .dbf 文件。 我要做的第一件事是从文件中获取架构表。 只要文件名(不带扩展名)不超过 8 个字符,我的代码就可以工作。
例如,假设我有一个名为 MyLongFilename.dbf 的文件。 以下代码不起作用; 它抛出以下异常:“Microsoft Jet 数据库引擎找不到对象‘MyLongFilename’。 确保该对象存在,并且其名称和路径名称拼写正确。”
string cxn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyLongFilename;Extended Properties=dBASE 5.0";
OleDbConnection connection = new OleDbConnection(cxn);
要克服此异常,下一步是使用 OldDbConnection 喜欢的名称(“MyLongF~1”而不是“MyLongFilename”),这会导致:
string cxn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyLongF~1;Extended Properties=dBASE 5.0";
OleDbConnection connection = new OleDbConnection(cxn);
这确实成功返回了 OleDbConnection。 现在,为了获取架构表,我尝试以下操作:
connection.Open();
DataTable schemaTable = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,
new object[] { null, null, fileNameNoExt, null });
这将返回一个没有行的 DataTable。 如果我将文件名重命名为 8 个或更少的字符,则此代码将起作用,并且我会为数据库中的每个字段返回一行。
通过长文件名,我知道返回的连接是有效的,因为我可以使用它来填充数据集,如下所示:
string selectQuery = "SELECT * FROM [MyLongF~1#DBF];";
OleDbCommand command = new OleDbCommand(selectQuery, connection);
connection.Open();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
dataAdapter.SelectCommand = command;
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
这给我返回一个数据集,其中包含包含 dbf 文件中所有数据的数据表。
所以问题是如何获取长命名 dbf 文件的模式表? 当然,我可以通过重命名/复制文件来解决这个问题,但这是我不想做的黑客行为。 我也不想用前 1 条记录填充数据集并从列中推断出架构。
As part of a project I'm working on in C# I need to read in a .dbf file. The first thing I want to do is to get the schema table from the file. I have code that works as long as the filename (without the extension) is not longer than 8 characters.
For example, let's say I have a file named MyLongFilename.dbf. The following code does not work; it throws the following exception: “The Microsoft Jet database engine could not find the object 'MyLongFilename'. Make sure the object exists and that you spell its name and the path name correctly.”
string cxn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyLongFilename;Extended Properties=dBASE 5.0";
OleDbConnection connection = new OleDbConnection(cxn);
To get past this exception, the next step is to use a name the OldDbConnection likes ('MyLongF~1' instead of 'MyLongFilename'), which leads to this:
string cxn = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyLongF~1;Extended Properties=dBASE 5.0";
OleDbConnection connection = new OleDbConnection(cxn);
This does successfully return an OleDbConnection. Now to get the schema table I try the following:
connection.Open();
DataTable schemaTable = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,
new object[] { null, null, fileNameNoExt, null });
This returns a DataTable with no rows. If I rename the filename to 8 or less characters then this code works and I get back a row for each field in the database.
With the long filename, I know the returned connection is valid because I can use it to fill a DataSet like so:
string selectQuery = "SELECT * FROM [MyLongF~1#DBF];";
OleDbCommand command = new OleDbCommand(selectQuery, connection);
connection.Open();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
dataAdapter.SelectCommand = command;
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
This gives me back a DataSet containing a DataTable with all of the data from the dbf file.
So the question is how can I get just the schema table for the long named dbf file? Of course I can work around the issue by renaming/copying the file, but that’s a hack I don’t want to have to make. Nor do I want to fill the DataSet with the top 1 record and deduce the schema from columns.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据MSDN,文件夹代表数据库,文件代表表。 您应该使用连接字符串中不包含文件名的目录路径,并将表名称作为 GetOleDbSchemaTable 限制的一部分。
According to MSDN, the folder represents the database and the files represent tables. You should be using the directory path not including the filename in the connection string then, and the name of the table as part of the restrictions to GetOleDbSchemaTable.
好吧,我认为连接应该是
,另一个是,也许你应该尝试使用其他提供商,当我像这样使用时,我之前提升了很多:
但是你应该安装 VFP 7
或 安装 Microsoft OLE DB Provider for Visual FoxPro 9.0这里
Well, i think the connection should be
and the other is, maybe you should try with other provider, I boosted a lot along ago when I used like this:
But you should have VFP 7 installed
or install Microsoft OLE DB Provider for Visual FoxPro 9.0 from here
fileNameNoExt 是否保存短文件名版本? 另外,MyLongF~1 是 9 个字符,而不是 8 个。
Is fileNameNoExt holding the short filename version? Also, MyLongF~1 is 9 characters, not 8.
如果您有一个(可能很小)dbf 文件,您可以通过将 dbf 文件复制到其他位置并打开副本而不是原始文件来解决问题。
If you have a single (and possibly small) dbf file you can solve the problem copying the dbf file elsewhere and open the copy instead of the original file.
我认为数据源应该代表包含 .DBF 文件的目录。 每个 .DBF 文件对应于该目录中的一个表。
我的猜测是 c:\MyLongF~1 是包含与 MyLongF~1#DBF 对应的文件名的目录的短名称,
您可以验证是否是这种情况吗?
I believe that the DataSource should represent the directory that contains the .DBF files. Each .DBF file corresponds to a table in that directory.
My guess is c:\MyLongF~1 is a short name for a directory that contains a filename corresponding to MyLongF~1#DBF
Can you verify whether or not this is the case?