使用 C# 从 MS Access DB 检索表关系

发布于 2024-12-14 19:07:16 字数 179 浏览 1 评论 0原文

我正在使用 C# 和 MS Access 2010。我需要从数据库中检索表关系,以确定实体之间的关系并在我的 C# 代码中使用它们。 我也需要 SQL Server 数据库具有相同的功能。

有没有办法使用 C# 和 .NET 3.0/3.5/4.0 来做到这一点?

珍惜你的时间。

谢谢, 马赫什

I am using C# with MS Access 2010. I need to retrieve the table relationships from the DB in order to determine the relationships between entities and use them in my C# code.
I need the same functionality for SQL Server database also.

Is there a way to do this using C# and .NET 3.0/ 3.5/ 4.0?

Appreciate your time.

Thanks,
Mahesh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

垂暮老矣 2024-12-21 19:07:16

这是我用来检索外键约束(关系,如果您愿意的话)的代码。 TableSchemaForeignKeyForeignKeyColumn 是我自己的类,我在其中存储结果。重点是使用 OleDbConnectionGetOleDbSchemaTable 方法:

private static void RetrieveForeignKeyInfo(OleDbConnection cnn, TableSchema tableSchema, Func<string, string> prepareColumnNameForMapping)
{
    string[] fkRestrictions = new string[] { null, null, null, null, null, tableSchema.TableName };
    using (DataTable dtForeignKeys = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Foreign_Keys, fkRestrictions)) {
        ForeignKey foreignKey = null;
        string constraintName = "";
        foreach (DataRow row in dtForeignKeys.Rows) {
            string newConstraintName = (string)row["FK_NAME"];
            if (newConstraintName != constraintName) {
                constraintName = newConstraintName;
                foreignKey = new ForeignKey();
                foreignKey.MasterTableName = (string)row["PK_TABLE_NAME"];
                tableSchema.ForeignKeys.Add(foreignKey);
            }
            var foreignKeyColumn = new ForeignKeyColumn();
            foreignKeyColumn.DetailColumnName = (string)row["FK_COLUMN_NAME"];
            foreignKeyColumn.MasterColumnName = (string)row["PK_COLUMN_NAME"];
            foreignKeyColumn.DetailColumnNameForMapping = prepareColumnNameForMapping(foreignKeyColumn.DetailColumnName);
            foreignKeyColumn.MasterColumnNameForMapping = prepareColumnNameForMapping(foreignKeyColumn.MasterColumnName);
            foreignKey.Columns.Add(foreignKeyColumn);
        }
    }
}

This is the code I used to retrieve the foreign key constraints (the relationships, if you prefer). TableSchema, ForeignKey and ForeignKeyColumn are my own classes, where I store the result. The point is to use the GetOleDbSchemaTable method of the OleDbConnection:

private static void RetrieveForeignKeyInfo(OleDbConnection cnn, TableSchema tableSchema, Func<string, string> prepareColumnNameForMapping)
{
    string[] fkRestrictions = new string[] { null, null, null, null, null, tableSchema.TableName };
    using (DataTable dtForeignKeys = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Foreign_Keys, fkRestrictions)) {
        ForeignKey foreignKey = null;
        string constraintName = "";
        foreach (DataRow row in dtForeignKeys.Rows) {
            string newConstraintName = (string)row["FK_NAME"];
            if (newConstraintName != constraintName) {
                constraintName = newConstraintName;
                foreignKey = new ForeignKey();
                foreignKey.MasterTableName = (string)row["PK_TABLE_NAME"];
                tableSchema.ForeignKeys.Add(foreignKey);
            }
            var foreignKeyColumn = new ForeignKeyColumn();
            foreignKeyColumn.DetailColumnName = (string)row["FK_COLUMN_NAME"];
            foreignKeyColumn.MasterColumnName = (string)row["PK_COLUMN_NAME"];
            foreignKeyColumn.DetailColumnNameForMapping = prepareColumnNameForMapping(foreignKeyColumn.DetailColumnName);
            foreignKeyColumn.MasterColumnNameForMapping = prepareColumnNameForMapping(foreignKeyColumn.MasterColumnName);
            foreignKey.Columns.Add(foreignKeyColumn);
        }
    }
}
无声无音无过去 2024-12-21 19:07:16

我会使用 DAO,在这种情况下,关系位于一个集合中,您可以从 Database 对象的 Relationships 属性中检索该集合。

在 ADO.NET 中,您可以使用 DataSet 类的 Relations 属性。

I would use DAO, in which case the relationships are in a collection that you can retrieve from the Relationships property of the Database object.

In ADO.NET, you use the Relations property of the DataSet class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文