2012-06-26 128 views
0

我是新來的,所以請溫和我:)安裝了MS Access 2010的Readig old V1.X MS Access文件

我已經創建了一些C#代碼來讀取MS Access文件的模式定義。它獲取的大多數版本的偉大工程,但是當我嘗試讀取訪問(V1.X)的舊版本,我發現了以下錯誤:

This Property is not supported for external data sources or for databases created with a previous version of Microsoft Jet.

這裏是我的代碼:

private DataTable ReadSchema(string strTable) 
{ 

     DataTable schemaTable = new DataTable(); 
     try 
     { 
      OleDbConnection conn = new OleDbConnection("Provider= Microsoft.JET.OleDB.4.0;data source=R:\\CB Import\\CBS.MDB"); 

      conn.Open(); 
      OleDbCommand cmd = new OleDbCommand(strTable, conn); 
      cmd.CommandType = CommandType.TableDirect; 

      OleDbDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly); 
      schemaTable = reader.GetSchemaTable(); 
      reader.Close(); 
      conn.Close(); 


      DataColumn dcRec = new DataColumn("TableName", typeof(string)); 
      dcRec.DefaultValue = strTable; 
      schemaTable.Columns.Add(dcRec); 


      schemaTable.Columns.Add("Type", typeof(string)); 
      schemaTable.Columns.Add("Length", typeof(string)); 




      foreach (DataRow r in schemaTable.Rows) 
      { 
       Console.WriteLine(r["ColumnName"].ToString() + " " + r["ColumnSize"].ToString() + " " + r["DataType"].ToString() + " " + r["NumericPrecision"].ToString() + " " + r["NumericScale"].ToString()); 
       r["Type"] = r["DataType"]; 
       r["Length"] = r["ColumnSize"]; 

           } 
     } 

     catch (Exception e) 
     { 
     MessageBox.Show(e.Message); 
     } 

     return (schemaTable); 


    } 

它彈了在這條線:

OleDbDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly); 

我有機會獲得2010安裝在我的機器上,即使我嘗試在該版本的Access中打開這些老麻煩的文件,我仍然得到關於打開舊版本錯誤。

此代碼除了嘗試讀取舊版本的Access以外,都可以使用。我可以通過使用Access 2007(安裝在另一臺機器上)並將數據庫文件保存爲2007版本來解決此問題,但是如果可能,我希望能夠自動化(代碼)?如果還有其他編碼方式,或者我搞亂了,請告訴我。

任何幫助將greatfully接收,並以啤酒,巧克力,鮮花或任何你的船浮筒:)

在此先感謝recipricated。

回答

0

好的,這裏的答案 - 自己工作。

public DataTable ReadSchema(string strTable, string strConnectionString, string strAccessFileName) 
    { 
     DataTable schemaTable = new DataTable(); 
     schemaTable.Columns.Add("ColumnName", typeof(string)); 
     schemaTable.Columns.Add("ColumnSize", typeof(Int32)); 
     schemaTable.Columns.Add("NumericPrecision", typeof(Int32)); 
     schemaTable.Columns.Add("NumericScale", typeof(Int32)); 
     schemaTable.Columns.Add("DataType", typeof(string)); 
     schemaTable.Columns.Add("Length", typeof(string)); 

     try 
     { 

      using (var con = new OleDbConnection(strConnectionString)) 
      { 
       con.Open(); 
       using (var cmd = new OleDbCommand("SELECT * FROM [" + strTable + "]", con)) 
       using (var reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly)) 
       { 
        var table = reader.GetSchemaTable(); 

        foreach (DataRow row in table.Rows) 
        { 
         Console.WriteLine(row["ColumnName"].ToString() + " " + row["ColumnSize"].ToString() + " " + row["DataType"].ToString() + " " + row["NumericPrecision"].ToString() + " " + row["NumericScale"].ToString()); 

         DataRow drRecord = schemaTable.NewRow(); 

         drRecord["ColumnName"] = row["ColumnName"]; 
         drRecord["ColumnSize"] = row["ColumnSize"]; 
         drRecord["NumericPrecision"] = row["NumericPrecision"]; 
         drRecord["NumericScale"] = row["NumericScale"]; 
         drRecord["DataType"] = row["DataType"]; 

         schemaTable.Rows.Add(drRecord); 
        } 
       } 
      } 
     } 
     catch (Exception Ex) 
     { 

      string strMessage = "Selecting schema for table " + strTable + ". " + Environment.NewLine + Ex.Message; 
      MessageBox.Show(strMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      tran.strErrors = tran.strErrors + strMessage + "\r\n"; 
      txtErrors.Text = txtErrors.Text + strMessage + "\r\n"; 
     } 



     return (schemaTable); 


    } 

事實證明,壞的「OleDbCommand cmd = new OleDbCommand(strTable,conn);」對於舊的V1.X數據庫文件來說,這行並不好。所以用「SELECT」取代了這個伎倆。

更多啤酒給我! :)