我正在尝试使用DataTable来获取SQL Server数据库的架构 但是,当尝试检测ForeignKeys时,约束集合仅带来UNIQUE约束。
Private Sub ShowConstraints(ByVal tableName As String)
Dim table As DataTable = New DataTable(tableName)
Using connection As SqlConnection = New SqlConnection(GetConnectionString)
Dim adapter As SqlDataAdapter = New SqlDataAdapter("Select top 1 * from " + _
tableName, connection)
connection.Open()
adapter.FillSchema(table, SchemaType.Mapped)
For Each c As Constraint In table.Constraints
If TypeOf c Is ForeignKeyConstraint Then
Dim fk As ForeignKeyConstraint = CType(c, ForeignKeyConstraint)
Console.WriteLine("** FK ** relatedTable: {0}; RelatedColumns: {1}", _
fk.RelatedTable, fk.RelatedColumns)
Else
Console.WriteLine("** Whatever ** Name: {0}; Type: {1}", _
c.ConstraintName, c.GetType.ToString)
End If
Next
End Using
End Sub
如何获取ForeignKey约束?
答案 0 :(得分:4)
嗯,这是出乎意料的。事实证明,毕竟FillSchema方法不会返回外键信息。 我有一些看起来应该这样做的示例代码,但它没有。
如果您真正想要的是以编程方式查询数据库中的所有FK,那么请从您的代码中尝试此系统存储过程。
msdb.dbo.sp_Help'tableName'
它返回一个数据集。第7个表具有表的所有约束,包括FK。
答案 1 :(得分:2)
我最终使用了对架构的直接查询。感觉不对,但完成了工作:
Private Sub ShowFKs()
Dim sqlstmt As New StringBuilder
sqlstmt.Append(" SELECT ")
sqlstmt.Append(" rc.CONSTRAINT_NAME, ")
sqlstmt.Append(" rcu.TABLE_NAME 'Referencing Table', ")
sqlstmt.Append(" rcu.COLUMN_NAME 'Referencing Column',")
sqlstmt.Append(" rcu1.TABLE_NAME 'Referenced Table',")
sqlstmt.Append(" rcu1.COLUMN_NAME 'Referenced Column'")
sqlstmt.Append(" FROM")
sqlstmt.Append(" INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc")
sqlstmt.Append(" INNER JOIN ")
sqlstmt.Append(" INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE rcu ")
sqlstmt.Append(" ON rc.CONSTRAINT_CATALOG = rcu.CONSTRAINT_CATALOG ")
sqlstmt.Append(" AND rc.CONSTRAINT_NAME = rcu.CONSTRAINT_NAME")
sqlstmt.Append(" INNER JOIN ")
sqlstmt.Append(" INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE rcu1 ")
sqlstmt.Append(" ON rc.UNIQUE_CONSTRAINT_CATALOG = rcu1.CONSTRAINT_CATALOG ")
sqlstmt.Append(" AND rc.UNIQUE_CONSTRAINT_NAME = rcu1.CONSTRAINT_NAME")
Using connection As SqlConnection = New SqlConnection(GetConnectionString)
Dim cmd As New SqlCommand(sqlstmt.ToString, connection)
Dim reader As SqlDataReader
cmd.CommandType = CommandType.Text
connection.Open()
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Do While reader.Read
For i As Integer = 0 To reader.FieldCount - 1
Console.WriteLine("** {0} = {1}", reader.GetName(i), reader.GetValue(i).ToString)
Next
Console.WriteLine("---------------------")
Loop
End Using
End Sub
此问题的信息:Query to get all foreign key constraints in SQL Server 2000
答案 2 :(得分:1)
嗯,如果DataRelations被禁用,或者没有强制执行或级联约束,则可能无法填充ForeignKeyConstraint对象。
但这可能有用:
Private Sub ShowConstraints(ByVal tableName As String)
Dim table As DataTable = New DataTable(tableName)
Using connection As SqlConnection = New SqlConnection(GetConnectionString)
Dim adapter As SqlDataAdapter = New SqlDataAdapter("Select top 1 * from " + _
tableName, connection)
connection.Open()
adapter.FillSchema(table, SchemaType.Mapped)
Console.WriteLine(" ** Parent Relations ** ")
For Each dr As DataRelation In table.ParentRelations
Console.Write("name: {0}: ", dr.RelationName)
Dim fk As ForeignKeyConstraint = dr.ChildKeyConstraint
If Not (fk Is Nothing) Then
Console.WriteLine(" RelatedTable {0}; RelatedColums {1}", _
fk.RelatedTable, fk.RelatedColumns)
Else
Console.WriteLine(" no constraint.")
End If
Next
Console.WriteLine(" ** child Relations ** ")
For Each dr As DataRelation In table.ChildRelations
Console.Write("name: {0}: ", dr.RelationName)
Dim fk As ForeignKeyConstraint = dr.ChildKeyConstraint
If Not (fk Is Nothing) Then
Console.WriteLine(" RelatedTable {0}; RelatedColums {1}", _
fk.RelatedTable, fk.RelatedColumns)
Else
Console.WriteLine(" no constraint.")
End If
Next
End Using
End Sub
答案 3 :(得分:0)
查询 information_schema 表( user_tables / all_tables 表示oracle) - 它们包含有关数据库的元数据。
答案 4 :(得分:0)
最好的方法是直接查询 information_schema 表:
SELECT
cu.TABLE_SCHEMA + '.' + cu.TABLE_NAME AS TABLE_NAME
, cu.COLUMN_NAME
, rc.CONSTRAINT_NAME AS FK_NAME
, rc.UNIQUE_CONSTRAINT_NAME AS REFERENCE
FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE cu INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
ON cu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
答案 5 :(得分:0)
此查询对我有用。
SELECT rc.[CONSTRAINT_NAME]
,fk.[TABLE_NAME]
,fk.[COLUMN_NAME]
,pk.[TABLE_NAME]
,pk.[COLUMN_NAME]
FROM [LocalV4].[INFORMATION_SCHEMA].[REFERENTIAL_CONSTRAINTS] rc
inner join [LocalV4].[INFORMATION_SCHEMA].[CONSTRAINT_COLUMN_USAGE] fk on fk.[CONSTRAINT_NAME] = rc.[CONSTRAINT_NAME]
inner join [LocalV4].[INFORMATION_SCHEMA].[CONSTRAINT_COLUMN_USAGE] pk on pk.[CONSTRAINT_NAME] = rc.[UNIQUE_CONSTRAINT_NAME]