从 Acumatica 的数据库中获取 table 的列名

Get column names of a table from database in Acumatica

如何从像 APTran 这样的数据库 table 中获取列?

我试图通过这种方式从 akumatika 缓存中获取,但问题是缓存中的内容和数据库中的内容并不总是匹配

 Type objType = System.Web.Compilation.PXBuildManager.GetType(e.Row.ObjectName, true);
 PXCache objCache = Base.Caches[objType];
 var fields = objCache.Fields;

我只需要获取数据库中列的名称。我该怎么做?

我认为你应该为此创建一个存储过程并用

执行它
 var results = PXDatabase.Execute()

看到这个问题How to execute stored procedure

实际上我最近不得不为我需要实现的通用服务执行此操作,您可以通过反射来完成此操作。

PXCache cache = Base.Caches[objectType];
BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance;

// An array containing every property on the table in the cache.
PropertyInfo[] fields = cache.GetItemType().GetProperties(bindingFlags);

foreach (PropertyInfo field in fields)
{
    object fieldValue = cache.GetValue(record, field.Name);
    // Do something with fieldValue
    // Check what attributes the property has by using field.GetCustomAttributes()
    // If you only want SQL persisted fields you would examine the attributes
    // And see if it contains a PXDBDatatype attribute
}

在这种情况下,bindingFlags 变量不是必需的,但我想演示如何使用它,因为在检索特性或属性时您会经常需要它,因此知道它的存在很重要。希望这对您有所帮助,我建议您查找 System.Reflection 库以及 PropertyInfo 对象类型上可用的内容,以查看下一步您可以执行的所有操作,但这应该为您提供一个良好的起点。

嗯..不是吗?来自 DAC 的字段可能与来自 DB 的字段完全不同(可能更多、更少或匹配。匹配实际上相当罕见……) DB - 在 table 上执行 sp_help。 DAC - 这是我用来显示字段和数据的代码:

StringBuilder retval = new StringBuilder();

if (obj != null)
{
    foreach (System.ComponentModel.PropertyDescriptor descriptor in System.ComponentModel.TypeDescriptor.GetProperties(obj))
    {
        if (retval.Length > 0) retval.Append(",");
        retval.Append(string.Format(System.Globalization.CultureInfo.InvariantCulture, " {0}={1}", descriptor.Name, descriptor.GetValue(obj)));
    }
}
return retval.ToString();