怎样提取ACCSEE表中的主键,允许空,默认值,字段说明

解决方案 »

  1.   

    用ADO连接数据库,然后用RecordSet.Fields对象,可以获得你想要的资料。
      

  2.   

    Fields(i).Type表示类型
    那什么表示主键,什么表示为空,还有默认值和字段说明
      

  3.   

    '/数据表字段或记录集字段属性.
    Public Type SmFiedArrtr
         FieldName As String        '字段名称
         FieldType As String        '字段类型
         FieldIsNull As String      '是否接受NULL
         FieldDefSize  As Long      '定义的据长度
         FieldActSize As String     '实际数据长度
    End Type
    '
    '取某 数据表 下所有的字段及其属性
    '函数名:GetTabFldAttrib
    '参数:  P_Cnn ADO连接,DateTabName 目标数据表名
    '返回值:SmFiedArrtr 类型数组
    '例:    FiedAtrrib=GetTabFldAttrib(P_CNN,"ACHGOODS")
    Public Function GetTabFldAttrib(ByRef P_Cnn As ADODB.Connection, _
                                    DbTableName As String) As SmFiedArrtr()
        Dim A As Long
        Dim StrSql As String
        Dim Rs As New ADODB.Recordset
        Dim ReturnVal() As SmFiedArrtr
        Dim ReID As Long    On Error Resume Next    StrSql = "Select Top 1 * From [" & DbTableName & "]" '取字段名
        Set Rs = RsOpen(P_Cnn, StrSql)
        Set Rs.ActiveConnection = Nothing
        Erase ReturnVal
        For A = 0 To Rs.Fields.Count - 1
            ReID = ReID + 1
            ReDim Preserve ReturnVal(ReID)
            ReturnVal(ReID - 1).FieldType = Rs.Fields(A).Type                              '数据类型
            ReturnVal(ReID - 1).FieldName = Rs.Fields(A).Name                              '字段名
            ReturnVal(ReID - 1).FieldIsNull = Rs.Fields(A).Attributes And adFldIsNullable  '是否可接受NULL
            ReturnVal(ReID - 1).FieldDefSize = Rs.Fields(A).DefinedSize                    '定义的数据长度
            ReturnVal(ReID - 1).FieldActSize = 0                                           '实际数据长度(因只有字段名),故此值是0
        Next
        Set Rs = Nothing
        GetTabFldAttrib = ReturnVal
    End Function
      

  4.   

    Fields(i).Attributes 如果包含adFldRowID(0x100),就说明是主键。
      

  5.   

    to:cuizm(射天狼)
    不对啊
    Debug.Print rst.Fields(i).Attributes And adFldIsNullable
    我用上面打印出来都是0,数据库里有的是不可为空,有的是可为空to:vbscape() 
    你说的Fields(i).Attributes 怎样用,能说明白一点吗
    谢了
      

  6.   

    Public Sub AttributesX   Dim cnn1 As ADODB.Connection
       Dim rstEmployees As ADODB.Recordset
       Dim fldLoop As ADODB.Field
       Dim proLoop As ADODB.Property
       Dim strCnn As String
       
       ' 打开连接和记录集。
       strCnn = "Provider=sqloledb;" & _
          "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
       Set cnn1 = New ADODB.Connection
       cnn1.Open strCnn
       Set rstEmployees = New ADODB.Recordset
       rstEmployees.Open "employee", cnn1, , , adCmdTable
       
       ' 显示连接的属性。
       Debug.Print "Connection attributes = " & _
          cnn1.Attributes   ' 显示雇员表字段的属性。
       Debug.Print "Field attributes:"
       For Each fldLoop In rstEmployees.Fields
          Debug.Print "   " & fldLoop.Name & " = " & _
             fldLoop.Attributes
       Next fldLoop   ' 显示是 NULLABLE 的雇员表的字段。
       Debug.Print "NULLABLE Fields:"
       For Each fldLoop In rstEmployees.Fields
          If CBool(fldLoop.Attributes And adFldIsNullable) Then
             Debug.Print "   " & fldLoop.Name
          End If
       Next fldLoop   ' 显示雇员表属性的属性。
       Debug.Print "Property attributes:"
       For Each proLoop In rstEmployees.Properties
          Debug.Print "   " & proLoop.Name & " = " & _
             proLoop.Attributes
       Next proLoop   rstEmployees.Close
       cnn1.CloseEnd Sub
      

  7.   

    Attributes 属性 (ADO)
             指示对象的一项或多项特性。设置和返回值设置或返回长整型值。对于 Connection 对象,Attributes 属性为读/写,并且其值可能为以下任意一个或多个 XactAttributeEnum 值的和(默认为零)。常量 说明 
    AdXactCommitRetaining 执行保留的提交,即通过自动调用 CommitTrans 启动新事务。并非所有提供者都支持该常量。 
    AdXactAbortRetaining 执行保留的中止,即通过自动调用 RollbackTrans 启动新事务。并非所有提供者都支持该常量。 
    对于 Parameter 对象,Attributes 属性为读/写,并且其值可能为以下任意一个或多个 ParameterAttributesEnum 值的和。常量 说明 
    AdParamSigned 默认值。指示该参数接受带符号的值。  
    AdParamNullable 指示该参数接受 Null 值。 
    AdParamLong 指示该参数接受长二进制数据。 
    对于 Field 对象,Attributes 属性为只读,其值可能为以下任意一个或多个 FieldAttributeEnum 值的和。常量 说明 
    adFldMayDefer 指示延迟该字段,即不使用整个记录从数据源检索该字段值,只有在显式访问这些字段时才能对其进行检索。 
    adFldUpdatable 指示可以写入该字段。 
    adFldUnknownUpdatable 指示提供者无法确定是否可以写入该字段。 
    adFldFixed 指示该字段包含定长数据。 
    adFldIsNullable 指示该字段接受 Null 值。 
    adFldMayBeNull 指示可以从该字段读取 Null 值。 
    adFldLong 指示该字段为长二进制字段。并指示可以使用 AppendChunk 和 GetChunk 方法。 
    adFldRowID 指示该字段含有无法写入的持久行标识符,且该字段除了对行进行标识(如记录号、唯一标识符等)外不存在有意义的值。 
    adFldRowVersion 指示该字段包含用来跟踪更新的某种时间或日期标记。 
    adFldCacheDeferred 指示提供者缓存了字段值,并已完成随后对缓存的读取。 
    对于 Property 对象,Attributes 属性为只读,并可能是以下任意一个或多个 PropertyAttributesEnum 值的和:常量 说明 
    adPropNotSupported 指示提供者不支持该属性。 
    adPropRequired 指示数据源初始化之前用户必须指定该属性的值。 
    adPropOptional 指示数据源初始化之前用户不必为该属性指定值。 
    adPropRead 指示用户可以读取该属性。 
    adPropWrite 指示用户可以设置该属性。 
    说明使用 Attributes 属性可设置或返回 Connection 对象、Paramete 对象、Field 对象或 Property 对象的特性。设置多个属性时,可以将相应的常量相加。如果将属性值设置为包括不兼容常量的总和,那么将产生错误。Remote Data Service 的用法    该属性在客户端的 Connection 对象上无效。
      

  8.   

    如果是用DAO,则属性都在SC.FIELDS(0).Pr....上.