用过虚拟主机的朋友都清楚,如果使用ACCESS数据库建站一段时间后,数据库会变的非常的大。如果这是我们需要增加或修改字段、表,则必须用ftp把数据库下载下来,非常的耗时间。
因此我们需要制作一个在线修改数据库的程序。下面给大家收集了这类程序需要的子函数,大家可以自己编写界面,调用这些子函数。
<%
'变量定义赋值
Dim objADOXDatabase '更改表名对象
Dim ConnStr '数据库连接
ConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath("/data/k88_data.mdb")
'建表
Sub AddTable(TableName)
On Error Resume Next
objConn.Execute("CREATE Table "& TableName &"")
If Err Then
Response.Write "添加 <font color=#4455aa>"&TableName&"</font> 表<font color=blue>错误</font>,请手动添加 <font color=red><B>"&TableName&"</B></font> 表,原因" & Err.Description & "<BR>"
Err.Clear
Response.Flush
Else
Response.Write "添加 <font color=#4455aa>"&TableName&"</font> 表成功 <BR>"
Response.Flush
End If
End Sub
'添加索引
Sub AddIndex(TableName,ColumnName,IndexType)
On Error Resume Next
SQL= "CREATE INDEX "& ColumnName &" ON "& TableName &"("& ColumnName &")"
If IndexType = 1 Then
SQL=SQL& " WITH PRIMARY "
End If
objConn.Execute(SQL)
If Err Then
Response.Write "添加 "&TableName&" 表中索引<font color=blue>错误</font>,请手动添加,原因" & Err.Description & "<BR>"
Err.Clear
Response.Flush
Else
Response.Write "添加 <font color=#4455aa>"& TableName &"</font> 表中索引 <font color=red>"& ColumnName &"</font> 成功 <BR>"
Response.Flush
End If
End Sub
'删除字段通用函数
Sub DelColumn(TableName,ColumnName)
On Error Resume Next
objConn.Execute("Alter Table "&TableName&" Drop "&ColumnName&"")
If Err Then
Response.Write "删除 "&TableName&" 表中字段<font color=blue>错误</font>,请手动将数据库中 <font color=#4455aa><B>"&ColumnName&"</B></font> 字段删除,原因" & Err.Description & "<BR>"
Err.Clear
Response.Flush
Else
Response.Write "删除 <font color=#4455aa>"&TableName&"</font> 表中字段 <font color=red>"&ColumnName&"</font> 成功 <BR>"
Response.Flush
End If
End Sub
'添加字段通用函数
Sub AddColumn(TableName,ColumnName,ColumnType)
On Error Resume Next
objConn.Execute("Alter Table "&TableName&" Add "&ColumnName&" "&ColumnType&"")
If Err Then
Response.Write "新建 <font color=red>"&TableName&"</font> 表中字段<font color=blue>错误</font>,请手动在 <font color=blue>"& TableName &"</font> 数据表中建立 <font color=red><B>"&ColumnName&"</B></font> 字段,属性为 <B>"&ColumnType&"</B>,原因" & Err.Description & "<BR>"
Err.Clear
Response.Flush
Else
Response.Write "新建 <font color=#4455aa>"&TableName&"</font> 表中字段 <font color=red>"&ColumnName&"</font> 成功 <BR>"
Response.Flush
End If
End Sub
'更改字段通用函数
Sub ModColumn(TableName,ColumnName,ColumnType)
On Error Resume Next
SQL="Alter Table ["&TableName&"] Alter Column ["&ColumnName&"] "&ColumnType&""
'Response.Write SQL&"<br>"
objConn.Execute(SQL)
If Err Then
Response.Write "更改 <font color=red>"&TableName&"</font> 表中字段属性<font color=blue>错误</font>,请手动将 <font color=blue>"&TableName&"</font> 表中的 <font color=#4455aa><B>"&ColumnName&"</B></font> 字段更改为 <font color=red><B>"&ColumnType&"</B></font> 属性,原因" & Err.Description & "<BR>"
Err.Clear
Response.Flush
Else
Response.Write "更改 <font color=#4455aa>"&TableName&"</font> 表中字段属性 <font color=red>"&ColumnName&"</font> 成功 <BR>"
Response.Flush
End If
End Sub
Sub ReNameTableConn()
On Error Resume Next
Set objADOXDatabase = Server.CreateObject("ADOX.Catalog")
objADOXDatabase.ActiveConnection = ConnStr
If Err Then
Response.Write "建立更改表名对象出错,您所要升级的空间不支持此对象,您很可能需要手动更改表名,原因" & Err.Description & "<BR>"
Err.Clear
Response.Flush
End If
End Sub
'更改字段名,
Sub RenameColumn(TableName,oldName,newName)
On Error Resume Next
objADOXDatabase.Tables(TableName).Columns(oldName).Name = newName
If Err Then
Response.Write "更改字段名<font color=blue>错误</font>,请手动将数据库 <font color=blue><B>"&TableName&"</B></font> 中 <font color=#4455aa><B>"&oldName&"</B></font> 字段名更改为 <font color=red><B>"&newName&"</B></font>,原因" & Err.Description & "<BR>"
Err.Clear
Response.Flush
Else
Response.Write "更改字段名 <font color=#4455aa>"&OldName&"</font> To <font color=red>"&newName&"</font> 成功 <BR>"
Response.Flush
End If
End Sub
'更改数据库表名,入口参数:老表名、新表名
Sub RenameTable(oldName, newName)
On Error Resume Next
objADOXDatabase.Tables(oldName).Name = newName
If Err Then
Response.Write "更改表名<font color=blue>错误</font>,请手动将数据库中 <B><font color=blue>"&oldName&"</B></font> 表名更改为 <font color=red><B>"&newName&"</B></font>,原因" & Err.Description & "<BR>"
Err.Clear
Response.Flush
Else
Response.Write "更改表名 <font color=#4455aa>"&OldName&"</font> To <font color=red>"&newName&"</font> 成功 <BR>"
Response.Flush
End If
End Sub
'删除表通用函数
Sub DelTable(TableName)
On Error Resume Next
objConn.Execute("Drop Table "&TableName&"")
If Err Then
Response.Write "删除 "&TableName&" 表<font color=blue>错误</font>,请手动将数据库中 <B>"&TableName&"</B> 表删除,原因" & Err.Description & "<BR>"
Err.Clear
Response.Flush
Else
Response.Write "删除 <font color=#4455aa>"&TableName&"</font> 表成功 <BR>"
Response.Flush
End If
End Sub
'判断字段是否存在通用函数
Function IsColumn(TableName,ColumnName)
On Error Resume Next
IsColumn = False
objConn.Execute("select " & ColumnName & " from " & TableName & " where 1 = 2")
If Err.Number <> 0 Then
Else
IsColumn = True
End If
End Function
%>