【问题标题】:How to get the primary key of an Ms Access table in C#如何在 C# 中获取 Ms Access 表的主键
【发布时间】:2023-03-17 02:47:01
【问题描述】:

在给定连接和表名的情况下,我需要构成 Microsoft Access 表的主键的一个或多个字段(只需字段名称即可)。

【问题讨论】:

    标签: c# ms-access primary-key


    【解决方案1】:

    好的,我想我找到了。它应该适用于所有 oledb 并且是 sth。喜欢:

    public static List<string> getKeyNames(String tableName, DbConnection conn)
        {
            var returnList = new List<string>();
    
    
            DataTable mySchema = (conn as OleDbConnection).
                GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys,
                                    new Object[] {null, null, tableName});
    
    
            // following is a lengthy form of the number '3' :-)
            int columnOrdinalForName = mySchema.Columns["COLUMN_NAME"].Ordinal;
    
            foreach (DataRow r in mySchema.Rows)
            {
                returnList.Add(r.ItemArray[columnOrdinalForName].ToString());
            }
    
            return returnList;
        }
    

    【讨论】:

    • 你救了我这么多。一个如此有用的解决方案没有我能找到的任何记录用法,这超出了我的理解范围。
    猜你喜欢
    • 2010-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多