无法在MS ACCESS中删除约束

时间:2015-01-28 16:45:26

标签: ms-access-2003

使用SQL命令时:

ALTER TABLE [会话] DROP CONSTRAINT [SessionAttendance]

我收到异常错误消息" 无法找到参考。"

存在约束,并在系统表中显示此用户表的约束。如何让这个约束下降?

数据库采用MS-ACCESS 2003格式。该应用程序使用JET 4.0我有几百个需要架构更新的实例。我有一个实用程序来生成SQL,但在尝试DROP CONSTRAINT操作时会失败。

1 个答案:

答案 0 :(得分:1)

通过Gord Thompson在评论建议中的含义回答。

ALTER语句正在应用于关系中的错误表。

约束最初添加到出勤表中。但是,当使用“GetOleDbSchemaTable”方法列出时,它会显示为Sessions表的属性。

根据以下代码摘录:

Structure Relation
    Public Name As String
    Public PrimaryTableName As String
    Public PrimaryField As String
    Public PrimaryIndex As String
    Public ForeignTable As String
    Public ForeignField As String
    Public OnUpdate As String
    Public OnDelete As String

    Public Overrides Function ToString() As String
        Dim msg As String = String.Format("Name:{0} PT:{1} PF:{2} PI:{3}   FT:{4} FF:{5}", _
                Name, PrimaryTableName, PrimaryField, PrimaryIndex, ForeignTable, ForeignField)
        Return msg
    End Function
End Structure

Private Function ListRelations(tableName As String) As List(Of Relation)
    Dim relations As New List(Of Relation)
    Dim MySchemaTable As DataTable

    Dim dbConn As New OleDbConnection(connectionString)
    dbConn.Open()

    MySchemaTable = dbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Foreign_Keys, _
               New Object() {Nothing, Nothing, tableName})

    Dim result As Boolean = False

    'List the table name from each row in the schema table.
    For Each row As DataRow In MySchemaTable.Rows
        Dim r As New Relation
        r.Name = row("FK_NAME")

        r.PrimaryTableName = row("PK_TABLE_NAME")
        r.PrimaryField = row("PK_COLUMN_NAME")
        r.PrimaryIndex = row("PK_NAME")
        r.ForeignTable = row("FK_TABLE_NAME")
        r.ForeignField = row("FK_COLUMN_NAME")
        r.OnUpdate = row("UPDATE_RULE")
        r.OnDelete = row("DELETE_RULE")
        Console.WriteLine(r.ToString)
        relations.Add(r)
    Next

    MySchemaTable.Dispose()
    dbConn.Close()
    dbConn.Dispose()

    Return relations
End Function