如何用VB操作某Access数据库文件增加一个表、表中增加一个字段?
我用VB做的一个程序,调用了一个Access数据库文件。
现在要修改这个程序,多出了一些内容仍然要存储到这个数据库文件中,那么就需要修改这个Access文件,多建一个表并建立表中的字段,此数据库文件中原来有的表也要增加几个字段。
当然直接打开Access修改这个文件很容易,我想问的是能不能不用Access而用VB实现我的要求呢?目的是当别人使用我的这个程序时,程序修改升级了,但是仍然要调用原来已经存了数据的数据库文件,不能要求使用者都去用Access修改吧
[解决办法]
Private Sub Command2_Click() '添加一个空表
Dim Link As New ADODB.Connection
Dim Rs As New ADODB.Recordset
Link.CursorLocation = adUseClient
Link.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb;Persist Security Info=False "
Link.Open
Rs.Open "CREATE TABLE MyTb(姓名 TEXT,学号 INT,婚否 TEXT,编号 CHAR(12),注册日期 DATETIME) ", Link, adOpenDynamic, adLockOptimistic
End Sub
Private Sub Command3_Click() '添加字段
Dim Link As New ADODB.Connection
Dim Rs As New ADODB.Recordset
Link.CursorLocation = adUseClient
Link.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb;Persist Security Info=False "
Link.Open
Rs.Open "ALTER TABLE mytb2 ADD MYNAME TEXT NULL,MYCODE LOGICAL NULL ", Link, adOpenDynamic, adLockPessimistic
'mytb2是表,MYNAME是字段,TEXT是字段类型,NULL为必须的,允许为空
End Sub
[解决办法]
Private Sub Command2_Click() '添加一个空表
Dim Link As New ADODB.Connection
dim strsql as string
Link.CursorLocation = adUseClient
Link.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb;Persist Security Info=False "
Link.Open
strsql= "CREATE TABLE MyTb(姓名 TEXT,学号 INT,婚否 TEXT,编号 CHAR(12),注册日期 DATETIME) "
link.execute(strsql)
End Sub
Private Sub Command3_Click() '添加字段
Dim Link As New ADODB.Connection
dim strsql as string
Link.CursorLocation = adUseClient
Link.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb;Persist Security Info=False "
Link.Open
strsql= "ALTER TABLE mytb2 ADD MYNAME TEXT NULL,MYCODE LOGICAL NULL "
link.execute(strsql)
End Sub
[解决办法]
'By VBAdvisor : CSDN上问此类问题的太多,总结一下:
Public Sub ADOCreateTable()
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 tbl
Set cat = Nothing
'DAO data type ADO data type
'dbBinary adBinary
'dbBoolean adBoolean
'dbByte adUnsignedTinyInt
'dbCurrency adCurrency
'dbDate adDate
'dbDecimal adNumeric
'dbDouble adDouble
'dbGUID adGUID
'dbInteger adSmallInt
'dbLong adInteger
'dbLongBinary adLongVarBinary
'dbMemo adLongVarWChar
'dbSingle adSingle
'dbText adVarWChar
'Though not shown in this example, there are a number of other attributes of a table or column that you can set when creating the table or column, using the DAO Attributes property. The table below shows how these attributes map to ADO and Microsoft Jet Providerspecific properties.
'DAO TableDef Property Value 'ADOX Table Property Value
'Attributes dbAttachExclusive Jet OLEDB:Exclusive Link True
'Attributes dbAttachSavePWD Jet OLEDB:Cache Link Name/Password True
'Attributes dbAttachedTable Type "LINK "
'Attributes dbAttachedODBC Type "PASS-THROUGH "
'DAO Field Property Value ADOX Column Property Value
'Attributes dbAutoIncrField AutoIncrement True
'Attributes dbFixedField ColumnAttributes adColFixed
'Attributes dbHyperlinkField Jet OLEDB:Hyperlink True
'Attributes dbSystemField No equivalent n/a
'Attributes dbUpdatableField Attributes (Field Object) adFldUpdatable
'Attributes dbVariableField ColumnAttributes Not adColFixed
End Sub