vb中如何方便的得到一个表中的所有主键字段
在线等待

解决方案 »

  1.   

    csdnHelp,我原本就是和你说的方法一样做的,但我判断不出来,我的语句:
    If ((Fld.Attributes And adFldKeyColumn) = adFldKeyColumn) Then
    来判断此字段是否为主键的一部分,但无论是什么类型的字段,Fld.Attributes的值始终都是106,不知何故?
      

  2.   

    引用Microsoft ADO Ext. 2.x for DDL and Security  Dim cnn As ADODB.Connection
      Dim strCnn As String
      Dim Ctl As ADOX.Catalog  strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;"
      strCnn = strCnn & "Data Source=" & App.Path & "\gz.mdb;"
      strCnn = strCnn & "Jet OLEDB:Engine Type=5;"  Set cnn = New ADODB.Connection
      cnn.Open strCnn  Set Ctl = New ADOX.Catalog
      Ctl.ActiveConnection = cnn''''''''''''''''''''''''''''''''''''''''''''''
      Dim s As Integer
      s = Ctl.Tables("固定表").Indexes.Count
      
      Dim TF As Boolean
      TF = Ctl.Tables("固定表").Indexes(0).PrimaryKey
      
      Dim ps As Integer
      ps = Ctl.Tables("固定表").Indexes(0).Columns.Count  If TF Then
        MsgBox "有主键"
        If s > 0 Then
          MsgBox "主键个数: " & ps      Dim i As Integer
          
          For i = 0 To ps - 1
            MsgBox "主键字段名 " & i + 1 & " : " & Ctl.Tables("固定表").Indexes(0).Columns.Item(i).Name
          Next i
          
        End If
       Else
        MsgBox "无主键"
      End If