【问题标题】:MS Access Oledb column propertiesMS Access Oledb 列属性
【发布时间】:2023-03-05 17:22:01
【问题描述】:

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

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

编辑我找到了以下代码here 这是迄今为止我得到的最接近的代码。它确实获得了列属性,如列描述,但它不适用于行源。有什么想法吗?

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

【问题讨论】:

    标签: c# sql ms-access foreign-keys oledb


    【解决方案1】:

    我强烈怀疑表列的RowSource 属性对于 Access 来说是如此特定,以至于您必须使用 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#。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    相关资源
    最近更新 更多