C# MS Access Oledb列属性

C# MS Access Oledb列属性,c#,sql,ms-access,foreign-keys,oledb,C#,Sql,Ms Access,Foreign Keys,Oledb,我用DAO在VBA中解决了这个问题 使用Oledb框架,我创建了一个可以查找记录值的函数。但是,它只获取原始值。我需要找到名为“Row Source”的列属性的值 有人能告诉我如何使用Oledb查找外键值吗 下面是我用于传统查找查询的函数 string IDatabase.LookupRecord(string column, string table, string lookupColumn, string lookUpValue) { OleDbCommand cmdLookupCo

我用DAO在VBA中解决了这个问题

使用Oledb框架,我创建了一个可以查找记录值的函数。但是,它只获取原始值。我需要找到名为“Row Source”的列属性的值

有人能告诉我如何使用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;
}
编辑我发现下面的代码是迄今为止我得到的最接近的代码。它确实获得了列属性,如列描述,但它不适用于行源。有什么想法吗

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();               
    }

我强烈怀疑表列的
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());
            }
        }
    }
}

太好了,我没有意识到DAO可用于C。