前提是有一个数据库 a  里面也有表了  我想在外面代码里 对这个数据库增加新的表 怎么增加 
  谁给我详细点的信息啊 .....    哦 还有主键ID..  还有其他字段的添加

解决方案 »

  1.   


    '引用 COM 中的 Microsoft ActiveX Data Objects 2.8 Library
    '引用 Microsoft ADO Ext. 2.8 for DDL and Security
    Sub CreateAccessTable(ByVal DbName As String,ByVal TblName As String)
            Try
                Dim cat As New ADOX.CatalogClass()
                Dim AdoConn As New ADODB.Connection
                AdoConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DbName & ";Jet OLEDB:Database Password=;"
                AdoConn.Open()
                cat.ActiveConnection = AdoConn
                Dim tbl As New ADOX.TableClass()
                tbl.ParentCatalog = cat
                tbl.Name = TblName            '增加一个自动增长的字段 
                Dim col As New ADOX.ColumnClass()
                col.ParentCatalog = cat
                col.Type = ADOX.DataTypeEnum.adInteger
                ' 必须先设置字段类型 
                col.Name = "id"
                col.Properties("Jet OLEDB:Allow Zero Length").Value = False
                col.Properties("AutoIncrement").Value = True
                tbl.Columns.Append(col, ADOX.DataTypeEnum.adInteger, 0)            '增加一个文本字段 
                Dim col2 As New ADOX.ColumnClass()
                col2.ParentCatalog = cat
                col2.Name = "Description"
                tbl.Columns.Append(col2, ADOX.DataTypeEnum.adVarChar, 50)            '设置主键 
                tbl.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "id", "", "")
                cat.Tables.Append(tbl)
                tbl = Nothing
                cat = Nothing
                MsgBox("数据库表:" + TblName + "已经创建成功!")
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End Sub
      

  2.   

    也可以直接执行sql语句:create table [test] (id integer identity(1,1) primary key,Description text(50))
      

  3.   

    用ADOX可以实现一些sql语句做不到的功能!