MS访问OLEDB列属性(MS Access Oledb column properties)

2019-10-18 13:58发布

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

使用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;
}

编辑我发现下面的代码在这里它最接近的香港专业教育学院得到了这么远。 它得到像列说明列属性,但它不为行源工作。 有任何想法吗?

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

Answer 1:

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


文章来源: MS Access Oledb column properties