2
我想在C#文件中打開DBF並將其上載到MySQL數據庫。現在我只是試圖打開DBF文件,但我收到以下錯誤:閱讀DBF文件:System.Data.OleDb.OleDbException
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
的文件名,然後將它拋出的錯誤。
請發佈您的堆棧跟蹤。 – JNYRanger
hmm是你嘗試連接的Dbase/Foxpro數據庫嗎?在foxpro dbf我認爲數據源應該是文件夾名稱檢查https://www.connectionstrings.com/dbf-foxpro/ – bansi
user908759:你有什麼問題?請刪除try ... catch塊以查看哪個語句,您現在獲得了哪種錯誤類型和msg。 – 2014-01-21 04:56:13