在写从梅儿小屋的友情链接数据库转换为CuteLink V1.0的时候用到了好些Access的表和字段的操作,好些不会,查阅了资料后使用通过,现整理如下:
建表:
sql="CREATE TABLE webtype ("&_
"id AutoIncrement,"&_
"hits smallint NOT NULL,"&_
"name nvarchar (12) NOT NULL,"&_
"PRIMARY KEY (id))"
Conn.Execute(sql)
其中,AutoIncrement 为自动编号数据类型,PRIMARY KEY 为主键。
删除索引:
Conn.execute("DROP INDEX id on link;")
添加索引:
Conn.execute("Create Unique Index iusername on link(username);")
添加一个username主键
Conn.execute("Alter Table Link Add username nvarchar(12) null PRIMARY KEY")
'删除字段通用函数
Sub DelColumn(TableName,ColumnName)
On Error Resume Next
Conn.Execute("Alter Table "&TableName&" Drop "&ColumnName&"")
If Err Then
Response.Write TableTop & "删除 "&TableName&" 表中字段<font color=blue>错误</font>,请手动将数据库中 <B>"&ColumnName&"</B> 字段删除,原因" & Err.Description & "<BR>" & TableEnd
Err.Clear
Response.Flush
Else
Response.Write TableTop & "删除 "&TableName&" 表中字段 "&ColumnName&" 成功 <BR>" & TableEnd
Response.Flush
End If
End Sub
'添加字段通用函数
Sub AddColumn(TableName,ColumnName,ColumnType)
On Error Resume Next
Conn.Execute("Alter Table "&TableName&" Add "&ColumnName&" "&ColumnType&"")
If Err Then
Response.Write TableTop & "新建 "&TableName&" 表中字段<font color=blue>错误</font>,请手动将数据库中 <B>"&ColumnName&"</B> 字段建立,属性为 <B>"&ColumnType&"</B>,原因" & Err.Description & "<BR>" & TableEnd
Err.Clear
Response.Flush
Else
Response.Write TableTop & "新建 "&TableName&" 表中字段 "&ColumnName&" 成功 <BR>" & TableEnd
Response.Flush
End If
End Sub
'更改字段通用函数
Sub ModColumn(TableName,ColumnType)
On Error Resume Next
Conn.Execute("Alter Table "&TableName&" Alter Column "&ColumnName&" "&ColumnType&"")
If Err Then
Response.Write TableTop & "更改 "&TableName&" 表中字段属性<font color=blue>错误</font>,请手动将数据库中 <B>"&ColumnName&"</B> 字段更改为 <B>"&ColumnType&"</B> 属性,原因" & Err.Description & "<BR>" & TableEnd
Err.Clear
Response.Flush
Else
Response.Write TableTop & "更改 "&TableName&" 表中字段属性 "&ColumnName&" 成功 <BR>" & TableEnd
Response.Flush
End If
End Sub
以下是使用方法:
AddColumn "link","username","nvarchar(12) not null"
DelColumn "link","img"
ModColumn "link","nvarchar(12) not null PRIMARY KEY"