MS Access Oledb column properties

本文关键字:properties column Oledb Access MS | 更新日期: 2023-09-27 18:29:43

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

使用Oledb框架,我创建了一个可以查找记录值的函数。然而,它只获得原始值。我需要找到名为"行源"的列属性的值

有人能向我解释一下如何使用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;
}

EDIT我在这里找到了以下代码,这是迄今为止我得到的最接近的代码。它确实获得了类似列Description的列属性,但它不适用于行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();               
    }

MS Access Oledb column properties

我强烈怀疑表列的RowSource属性是Access特有的,因此您必须使用DAO来检索它

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