读取没有标准 .dbf 扩展名的数据库文件

发布于 2024-07-22 04:36:18 字数 928 浏览 3 评论 0原文

我正在尝试读取一个文件,该文件只是一个 dbase 文件,但没有标准扩展名,该文件类似于:

Test.Dat

我正在使用这段代码来尝试读取该文件:

string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Temp;Extended Properties=dBase III";
            OleDbConnection dBaseConnection = new OleDbConnection(ConnectionString);
            dBaseConnection.Open();

            OleDbDataAdapter oDataAdapter = new OleDbDataAdapter("SELECT * FROM Test", ConnectionString);
            DataSet oDataSet = new DataSet();
            oDataAdapter.Fill(oDataSet);//I get the error right here...
            DataTable oDataTable = oDataSet.Tables[0];
            foreach (DataRow dr in oDataTable.Rows)
            {
                Console.WriteLine(dr["Name"]);
            }

当然,这会崩溃,因为它不能找到名为 test 的数据库文件,但如果我将该文件重命名为 Test.dbf ,它就可以正常工作。 我无法一直重命名该文件,因为第三方应用程序使用它作为其文件格式。

有谁知道如何在 C# 中读取没有标准扩展名的 dbase 文件。

谢谢。

I am trying to read a file which is just a dbase file but without the standard extension the file is something like:

Test.Dat

I am using this block of code to try and read the file:

string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Temp;Extended Properties=dBase III";
            OleDbConnection dBaseConnection = new OleDbConnection(ConnectionString);
            dBaseConnection.Open();

            OleDbDataAdapter oDataAdapter = new OleDbDataAdapter("SELECT * FROM Test", ConnectionString);
            DataSet oDataSet = new DataSet();
            oDataAdapter.Fill(oDataSet);//I get the error right here...
            DataTable oDataTable = oDataSet.Tables[0];
            foreach (DataRow dr in oDataTable.Rows)
            {
                Console.WriteLine(dr["Name"]);
            }

Of course this crashes because it can't find the dbase file called test, but if I rename the file to Test.dbf it works fine. I can't really rename the file all the time because a third party application uses it as its file format.

Does anyone know a way to read a dbase file without a standard extension in C#.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携君以终年 2024-07-29 04:36:18

让我对您的代码感到困惑的是您创建 ConnectionString 的方式。 我会这样做:

string databaseFile = "test.dat";
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Temp\" + databaseFile +";Extended Properties=dBase III";

在路径的数据源部分设置文件名时,我从来没有遇到过任何文件类型/扩展名的问题,无论它是什么。

至于您的 SELECT 语句,“SELECT * FROM Test”是从数据库中名为“Test”的表中选择所有数据,而不是从名为“Test”的文件中选择所有数据。

我没有使用过 dBase 文件,但我的猜测是,您的数据源足以让 C# 使用默认的 dBase 扩展名找出您想要的文件,并且当您不填充数据适配器时,它会崩溃t 使用默认扩展名。 尝试添加特定的文件名,看看是否有效。

What confuses me about your code is the way you create your ConnectionString. I would do it like this:

string databaseFile = "test.dat";
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Temp\" + databaseFile +";Extended Properties=dBase III";

When setting the file name in the Data Source part of the path, I've never had a problem with any file type/extension, no matter what it is.

As for your SELECT statement, "SELECT * FROM Test" is selecting all data from the table named "Test" in your database, not your file named "Test".

I've not worked with dBase files, but my guess is what is happening is that your data source is enough for C# to figure out what file you want using the default dBase extension, and it crashes on filling the data adapter when you aren't using the default extension. Try adding the specific file name and see if it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文