2011-08-29 146 views
2

我嘗試使用C#連接到DBF數據庫(我嘗試3種連接的)如何連接到DBF數據庫

string connectionString = @"Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=c:\employees.dbf;"; 
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\employees.dbf;Extended Properties=dBASE IV;"; 
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\employees.dbf;Extended Properties=dBASE IV;User ID=Admin;Password=;"; 

using (OdbcConnection connection = new OdbcConnection(connectionString)) 
{ 
    connection.Open(); 
} 

,我得到了錯誤

ERROR1:

ERROR [HY024] [Microsoft][ODBC dBase Driver] '(unknown)' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.
ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr failed
ERROR [HY024] [Microsoft][ODBC dBase Driver] '(unknown)' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.

或錯誤2:

ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

what c一個是問題?

在此先感謝

回答

7
Dim Conn As New OLEDBConnection 
Conn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\folder;Extended Properties=dBASE IV;User ID=Admin;Password=;" 

要從數據庫表中選擇,你必須做到以下幾點(例如):

"SELECT * FROM tblCustomers.DBF" 

(注意,表名之後的DBF)

+1

得到此錯誤:錯誤[IM002] [微軟] [ODBC驅動程序管理器]數據源找不到名稱並且沒有指定默認驅動程序 – Gali

+1

如果您使用的是64位服務器,則需要使用「Provider = Microsoft.Jet.OLEDB.12.0」。我知道這是一個古老的線程,但只是有人偶然發現這個..... –

+0

謝謝Crisim II Numenoreano –

2

兩件事情:

首先,請嘗試以下連接字符串:

string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\;Extended Properties=dBASE IV;"; 

注意你沒有在連接字符串中指定的文件名(這將是你的SELECT或其他語句的一部分),只是文件的路徑。

一旦你打開連接,這就是你使用文件名的地方。例如:

OleDbCommand cmd = new OleDbCommand("SELECT * FROM Employees"); 

注意不要添加「.DBF」擴展(它的假設 - 因爲事實上,該文件必須具有擴展名爲.dbf,否則將不予以確認,至少按照我的經驗,閱讀)。

編輯

我有過的(每次幾百到幾千)比我更想與DBF文件的工作的「特權」。如果你仍然有問題留下評論,明天我會在工作時看一看 - 上面的內容大多數都是在我的頭頂上,有一點點使用Google。

+0

感謝您的幫助,您能爲此發送一個小小的C#示例嗎? – Gali

+0

@Gali - 什麼樣的樣本?也就是說,如何去做什麼(除了上面的內容,我的意思是)。 – Tim

+0

您好先生,我在閱讀dbf文件時遇到不同類型的錯誤。請檢查http://stackoverflow.com/questions/33687000/the-microsoft-jet-database-engine-could-not-find-object –

-1

試試這個:

System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection ("Driver={Microsoft Visual FoxPro Driver}; 
SourceType = DBF; 
SourceDB = " + System.IO.Path.GetFullPath(strFileName).Replace(System.IO.Path.GetFileName(strFileName), "") + ";Exclusive=No");