2013-08-30 51 views
1

我使用DAO here的MS Access OLEDB列屬性

使用OLEDB框架解決了這個問題,在VBA,我創建了一個功能,可以查找記錄值。但它只能獲得原始價值。我需要找到所謂的「行來源」列屬性的值

有人能向我解釋瞭如何使用OLEDB

下面是我的一個傳統的查找查詢功能查找外鍵值。

string IDatabase.LookupRecord(string column, string table, string lookupColumn, string lookUpValue) 
{ 
    OleDbCommand cmdLookupColumnValue = new OleDbCommand(); 

    string sqlQuery = "SELECT [" + table + "].[" + column + "] " + 
         "FROM [" + table + "] " + 
         "WHERE [" + table + "].[" + lookupColumn + "] = '" + lookUpValue + "'"; 

    cmdLookupColumnValue.CommandText = sqlQuery; 
    cmdLookupColumnValue.CommandType = CommandType.Text; 
    cmdLookupColumnValue.Connection = connection; 

    string result = ""; 

    try 
    { 
     result = cmdLookupColumnValue.ExecuteScalar().ToString(); 
    } 
    catch(Exception ex) 
    { 
     MessageBox.Show("Query is not valid :" + ex.ToString()); 
    } 

    return result; 
} 

編輯我發現下面的代碼here其最接近的香港專業教育學院迄今得到。它確實獲得像列描述那樣的列屬性,但它不適用於行Source。有任何想法嗎?

public void Test() 
    { 
     string columnName = "Main Space Category"; 

     ADOX.Catalog cat = new ADOX.Catalog(); 
     ADODB.Connection conn = new ADODB.Connection(); 
     conn.Open(connectionString, null, null, 0); 
     cat.ActiveConnection = conn; 
     ADOX.Table mhs = cat.Tables["FI - ROOM"]; 

     var columnDescription = mhs.Columns[columnName].Properties["Description"].Value.ToString(); 

     MessageBox.Show(columnDescription); 

     conn.Close();    
    } 

回答

3

我強烈懷疑,表列的RowSource特性如此具體的訪問,你將不得不使用DAO進行檢索。下面的C#代碼是你可能如何做到這一點的例子:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace daoConsoleApp 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string TableName = "Cars"; 
      string FieldName = "CarType"; 

      // This code requires the following COM reference in your project: 
      // 
      // Microsoft Office 14.0 Access Database Engine Object Library 
      // 
      var dbe = new Microsoft.Office.Interop.Access.Dao.DBEngine(); 
      Microsoft.Office.Interop.Access.Dao.Database db = dbe.OpenDatabase(@"Z:\_xfer\Database1.accdb"); 
      try 
      { 
       Microsoft.Office.Interop.Access.Dao.Field fld = db.TableDefs[TableName].Fields[FieldName]; 
       string RowSource = ""; 
       try 
       { 
        RowSource = fld.Properties["RowSource"].Value; 
       } 
       catch 
       { 
        // do nothing - RowSource will remain an empty string 
       } 

       if (RowSource.Length == 0) 
       { 
        Console.WriteLine("The field is not a lookup field."); 
       } 
       else 
       { 
        Console.WriteLine(RowSource); 
       } 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.ToString()); 
      } 
     } 
    } 
} 
+0

完美,我沒有意識到,DAO是可用於C#。 – MichaelTaylor3D

相關問題