2017-02-27 46 views
0

我在這裏掙扎。我試圖從dbf文件中獲取數據。爲此使用下面的連接字符串和代碼。C# - dbf外部表格未處於預期格式

DataTable YourResultSet = new DataTable(); 
     const string path = "D:\\Sample\\Database\\client.dbf"; 
     string conStr = String.Format("Provider = Microsoft.Jet.Oledb.4.0; Data Source = {0}; Extended Properties = \"dBase IV\"", Path.GetDirectoryName(path)); 
     var connection = new OleDbConnection(conStr); 
     connection.Open(); 

     var command = new OleDbCommand(string.Format("select id from {0}", Path.GetFileName(path)), connection); 

     using (var reader = command.ExecuteReader()) 
     { 
      while (reader.Read()) 
      { 
       var str = (string)reader["id"]; 
      } 
     } 

     connection.Close(); 

幾乎沒有dbf文件可以讀取,大部分dbf文件都無法讀取。雖然執行代碼ann錯誤發生在「外部表格未處於預期格式。」

即使我使用connextion串像vfopledb,vfoledb1,噴射,王牌等不同..

我的機器是64位的,我使用vs2013。請幫幫我。

不要把負面,因爲我已經嘗試了所有關於這個問題的堆棧流量回答。

+0

您是否嘗試過在select語句中使用表的名稱,比如'select id from client'?你有同樣的錯誤嗎?哪條線路導致錯誤? – Supersnake

+0

是的,我也試過這個查詢。但沒有改進。同樣的錯誤會在那裏。 執行reader語句時會出現錯誤。 –

回答

1

使用VFPOLEDB驅動程序。 Jet或ACE驅動程序無法識別所有dbf表。 VFPOLEDB驅動程序是32位,這意味着您應該在C#項目(屬性/構建目標平臺)中定位x86。

void Main() 
{ 
    DataTable YourResultSet = new DataTable(); 
    string path = @"D:\Sample\Database\client.dbf"; 

    using (OleDbConnection connection = 
    new OleDbConnection("Provider=VFPOLEDB;Data Source=" + Path.GetDirectoryName(path))) 
    using (OleDbCommand command = new OleDbCommand(
    string.Format("select id from {0}", Path.GetFileNameWithoutExtension(path)), connection)) 
    { 
    connection.Open(); 
    YourResultSet.Load(command.ExecuteReader()); 
    connection.Close(); 
    } 

    Form f = new Form(); 
    DataGridView dgv = new DataGridView { Dock = DockStyle.Fill, DataSource=YourResultSet}; 
    f.Controls.Add(dgv); 
    f.Show(); 
} 
+0

嗨,謝謝你的寶貴回答。我已經嘗試過你的示例代碼,我已經將目標CPU版本設置爲x86。但是錯誤是** d:\ sample \ database \ client.dbf不是表格。**。請幫助我 –

+0

對於它的工作,.dbf應該是VFP已知格式的xBase表。也許它不是或標題可能會損壞。您可以通過電子郵件將文件發送給我,以便我可以檢查您是否願意(如果是VFP已知的DBF,則可以使用代碼(VFP代碼)修復它) –

+0

http://fox.wikis.com/wc.dll?Wiki~NotATable~WIN_COM_API。 –