col(6).Properties("Jet OLEDB:Allow Zero Length") = True
在用AdoX动态创建数据库时可以设置缺省值吗?

解决方案 »

  1.   

    允许零长度值和允许 NULL 值完全不同。
    允许 NULL 是允许将指针值设置为 0,它一般表示尚未赋值;而允许零长度是允许指针(不为零)所指向的缓冲区的数据长度可以为 0,也就是一个空字符串。当 Required 属性为 True,添加新记录时就必须为该字段赋值。可以赋一个空串,但不可以赋 NULL。实际上是允许改写指针所指缓冲区,不允许改写指针值。如果系统允许为字段赋值 NULL,字段的 Attributes 属性就必须包含 adFldIsNullable。并非所有的数据库都支持它,而且它在创建后就是只读的。
      

  2.   

    例子:
    Dim cat As New ADOX.Catalog
    Dim tbl As New ADOX.Table
    ' Open the catalog
    cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=.\\NorthWind.mdb;"' Create a new Table object.
    With tbl
    .Name = "Contacts"
    ' Create fields and append them to the new Table
    ' object. This must be done before appending the
    ' Table object to the Tables collection of the
    ' Catalog.
    .Columns.Append "ContactName", adVarWChar
    .Columns.Append "ContactTitle", adVarWChar
    .Columns.Append "Phone", adVarWChar
    .Columns.Append "Notes", adLongVarWChar
    .Columns("Notes").Attributes = adColNullable
    End With' Add the new table to the database.
    cat.Tables.Append tblSet cat = Nothing
      

  3.   

    原本动态创建空表是可以完成的,当我加入
    col(i).Attributes = adColNullable
    之后,就在
    cat.tables.append tbl
    时弹出“在项目中未找到的”错误
    为什么?Attributes属性是否只对adLongVarWchar有效?
    Attributes是否时应该在append tbl之后设置呢?
      

  4.   

    adColNullable 仅适用于字符串类型字段,所以不可对所有字段如此设置。数字型字段是没有指针的,直接存储。