gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:27:22 24 4
gpt4 key购买 nike

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

最佳答案

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

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

关于c# - 如何在C#中获取Ms Access表的主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/862749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com