gpt4 book ai didi

c# - 读取 DBF 文件 : System. Data.OleDb.OleDbException

转载 作者:行者123 更新时间:2023-12-02 04:39:18 25 4
gpt4 key购买 nike

我正在尝试在 C# 文件中打开 DBF 并将其上传到 MySQL 数据库。现在我正试图打开 DBF 文件,但出现以下错误:

A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Error: Failed to retrieve the required data from the DataBase.

Unrecognized database format 'C:\Users\Path\..\..\..\SOMEFILE.DBF'.

我的代码如下。

private void button2_Click(object sender, EventArgs e)
{
DirectoryInfo dir = new DirectoryInfo(Regex.Replace(textBox1.Text, @"\\", @"\\"));
foreach (FileInfo file in dir.GetFiles())
{
MessageBox.Show(file.Name);
string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir + file.Name;
string strAccessSelect = "SELECT * FROM "+file.Name.Substring(0,file.Name.Length-4);
DataSet myDataSet = new DataSet();
OleDbConnection myAccessConn = null;

try
{
myAccessConn = new OleDbConnection(strAccessConn);
}
catch (Exception ex)
{
Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
return;
}

try
{
OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect, myAccessConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);

myAccessConn.Open();
myDataAdapter.Fill(myDataSet);
}
catch (Exception ex)
{
Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message);
return;
}
finally
{
myAccessConn.Close();
}
}
}

我只得到第一个带有文件名的 MessageBox 然后它抛出错误。

最佳答案

您的连接字符串应标识 OleDB 数据源是 DBASE 类型:

string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir + ";Extended Properties=dBase III";

另请注意,当通过 OleDB 连接到 DBase 时,您不指定 DBF 文件,而是指定包含它的文件夹。各个文件是表格。

关于c# - 读取 DBF 文件 : System. Data.OleDb.OleDbException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21249404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com