简体   繁体   English

如何在C#中获取Ms Access表的主键

[英]How to get the primary key of an Ms Access table in C#

在需要连接和tableName的情况下,我需要形成Microsoft Access Table主键的字段或字段(只是字段的名称)。

ok, I guess I found it. 好吧,我想我找到了。 It should work for all oledb and is sth. 它应该适用于所有oledb并且是...... like : 喜欢 :

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM