怎样用asp代码在已有数据库中插入一个新表?数据库是Access的.
新表的字段类型不一样...而且有一个字段是"自动编号"类型的(或者定义主键)...
可以实现吗?请求高手帮忙。谢谢...
[QUOTE]<%
CreateTableSql = "create table [System_Option]([Option] varchar(100), [Value] varchar(2000))"
CreateTableName = "System_Option"
CreateTableMsg = "创建系统信息表"
dim ReturnMsg
If Not CreateTable(CreateTableSql, CreateTableName) Then
ErrFlag = False
ReturnMsg = CreateTableMsg & "失败!"
Else
ErrFlag = True
ReturnMsg = CreateTableMsg & "成功!"
End If
response.Write(ReturnMsg)
'函数作用:创建数据表
Function CreateTable(CreateSQL,TableName)
Dim getTheTable
getTheTable = True
On Error Resume Next
Err.Number = 0
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set rst = server.createobject("ADODB.Recordset")
''判断表是否已经存在,存在则不用创建
rst.Open "select name from sysobjects where type='u' and name='"& TableName &"'", Conn, 3, 3
If not rst.Eof then
getTheTable = False
End if
rst.Close
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If getTheTable Then
Conn.Execute "" & CreateSQL & ""
End If
''清除表中记录,可根据需要删除
Conn.Execute "delete from [" & TableName & "]"
''判断是否创建成功
If Err.Number <> 0 Then
CreateTable = False
Exit Function
End If
CreateTable = True
End Function
%>[/QUOTE]
[此贴子已经被作者于2007-6-27 17:01:04编辑过]
[CODE]<%
if request("act")="new" then
banji=request.form("class")
set cnn=server.createobject("adodb.connection")
str="driver={microsoft access driver (*.mdb)};dbq=" &server.mappath("database\db.mdb")
cnn.open str
newtable="create table "&banji&" (id AUTOINCREMENT,姓名 varchar(8),学号 varchar(10))"
cnn.execute newtable
kecs=session("kcs")
for i=1 to kecs
subject=request.form("kechengming("&i&")")
altable="alter table "&banji&" add column "&subject&" int null"
cnn.execute altable
next
%>
<%
else
%>
<html>
<body>
<%
kecs=session("kcs")'此session在以前的表单定义过/
%>
<form method="POST" name="form2" action="test.asp" ><input type="hidden" name="act" value="new" size="20">
<center>请输入班级: <input type="text" name="class"></br>
<% for i= 1 to kecs %>
课 程 <%=i%> <input type="text" name="kechengming(<%=i%>)" value="" size="20"></br>
<%next%>
<p><input type="submit" value="提交"> <input type="reset" value="重写" name="B2"></center><p></form>
</body>
</html>
<%end if%>[/CODE]
<!--AUTOINCREMENT 自动编号
CHAR 文本,默认255字符(不能为NULL),使用CHAR(10),10个字符
MEMO 备注
INT 长整型
PRIMARY KEY (字段名) 设为主建 -->