Retrieve table relationships from MS Access DB using C#(使用C#从MS Access数据库中检索表关系)
本文介绍了使用C#从MS Access数据库中检索表关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在MS Access 2010中使用C#。我需要从数据库中检索表关系,以便确定实体之间的关系并在我的C#代码中使用它们。 我还需要用于SQL Server数据库的相同功能。
有没有办法使用C#和.NET 3.0/3.5/4.0来实现这一点?
感谢您抽出时间。
谢谢, Mahesh
推荐答案
这是我用来检索外键约束(关系,如果您愿意)的代码。TableSchema
、ForeignKey
和ForeignKeyColumn
是我自己的类,我在其中存储结果。重点是使用OleDbConnection
的GetOleDbSchemaTable
方法:
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);
}
}
}
这篇关于使用C#从MS Access数据库中检索表关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:使用C#从MS Access数据库中检索表关系


基础教程推荐
猜你喜欢
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01