2010-01-14 79 views
1

我正在嘗試使用.NET和Winforms來讀取dBase III .dbf文件,而我試過的東西似乎都不起作用。我嘗試了四種不同的連接方法,並且它們中的每一個都掛在Open方法上。沒有例外,沒有超時,沒有事件消息,什麼都沒有。表格就在那裏。關於什麼可能是錯誤的任何想法?與dBase .dbf文件的.NET連接

這是我試過的方法。 .dbf文件是在d:\ DB:

private void read1() 
     { 
      string c = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\db\\;Extended Properties=dBASE III"; 
      OleDbConnection conn = new OleDbConnection(c); 
      conn.Open(); 
      MessageBox.Show("ok"); 
      conn.Close(); 

     } 

     private void read2() 
     { 
      System.Data.Odbc.OdbcConnection oConn = new System.Data.Odbc.OdbcConnection(); 
      oConn.ConnectionString = @"Driver={Microsoft dBase Driver (*.dbf)};SourceType=DBF;SourceDB=D:\db;Exclusive=No; Collate=Machine;NULL=NO;DELETED=NO;BACKGROUNDFETCH=NO;"; 
      oConn.Open(); 
      System.Data.Odbc.OdbcCommand oCmd = oConn.CreateCommand(); 
      oCmd.CommandText = @"SELECT * FROM D:\db\Poi.dbf"; 
      DataTable dt = new DataTable(); 
      dt.Load(oCmd.ExecuteReader()); 
      MessageBox.Show(dt.Rows.Count.ToString()); 
      oConn.Close(); 
     } 

     private void read3() 
     { 
      System.Data.Odbc.OdbcConnection oConn = new System.Data.Odbc.OdbcConnection(); 
      oConn.ConnectionString = @"Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=d:\db;"; 
      oConn.Open(); 
      MessageBox.Show("ok"); 
      oConn.Close(); 
     } 

     private void read4() 
     { 
      System.Data.Odbc.OdbcConnection oConn = new System.Data.Odbc.OdbcConnection(); 
      oConn.ConnectionString = @"Driver={Microsoft dBase Driver (*.dbf)};datasource=d:\db\"; 
      oConn.Open(); 
      System.Data.Odbc.OdbcCommand oCmd = oConn.CreateCommand(); 
      oCmd.CommandText = @"SELECT * FROM D:\db\Poi.dbf"; 
      DataTable dt = new DataTable(); 
      dt.Load(oCmd.ExecuteReader()); 
      MessageBox.Show(dt.Rows.Count.ToString()); 
      oConn.Close(); 
     } 
+0

我想幫忙,但在那裏我找到了DBF檔案這幾天? =) –

回答

4

試試這個:

using (OleDbConnection cn = new OleDbConnection(
    @"Provider=Microsoft.Jet.OLEDB.4.0;" + 
    @"Data Source=C:\full-path-without-filename;" + 
    @"Extended Properties=dBASE III;")) 
using (OleDbCommand cm = cn.CreateCommand()) 
{ 
    cn.Open(); 
    cm.CommandText = "SELECT * FROM fox_samp"; // fox_samp is your filename 
    using (OleDbDataReader dr = cm.ExecuteReader()) 
    { 
     while (dr.Read()) 
     { 
      Console.WriteLine(dr[0]); 
     } 
    } 
} 

我只是跑這沒有錯誤。而這個網站是非常有用的:ConnectionStrings.com

+0

謝謝,但這也沒有工作。它掛在Open()上。我認爲文件或我的電腦有問題.. – dstr

+0

我建議你建立一個控制檯應用程序,並作出一個非常簡單的例子,如上所述;或者你也可以使用OdbcConnection和相關的類 –

1

只要它不能工作,因爲64個系統不再支持ODBC DBF驅動程序的64位,也沒有噴氣存在了..