ASP创建ACCESS表或添加字段
2009-04-22 10:38:54 来源:WEB开发网创建一张空表:
Sql="Create TABLE [表名]"
创建一张有字段的表:
Sql="Create TABLE [表名]([字段名1] MEMO NOT NULL, [字段名2] MEMO, [字段名3] COUNTER NOT NULL, [字段名4] DATETIME, [字段名5] TEXT(200), [字段名6] TEXT(200))
字段类型:
2 : "SmallInt", // 整型
3 : "Int", // 长整型
4 : "Real", // 单精度型
5 : "Float", // 双精度型
6 : "Money", // 货币
7 : "DateTime", // 日期时间
11 : "Bit", // 是否
13 : "TimeStamp",
17 : "TinyInt", // 字节
72 : "UniqueIdentifier", // 同步复制 ID
128 : "Binary",
129 : "Char",
130 : "NChar",
131 : "Decimal", // 小数
133 : "DateTime",
135 : "SmallDateTime",
200 : "VarChar",
201 : "Text",
202 : "VarChar", // 文本
203 : "Text", // 备注
204 : "Binary", // 二进制
205 : "Image" // OLE 对象
以下字段为无编码字段(NChar、NVarchar、NText型)
8,128,130,202,203,204,205
以下字段为按当前系统内码编码的字段(asp中可用CodePage=936纠正为gb2312内码)
129,200,201
在现有的表中增加字段:
Sql="alter table [表名] add column [字段名] varchar(200)"
修改字段类型:
Sql="alter table [表名] Alter COLUMN 字段名] varchar(50)"
删除表:
Sql="Drop table [表名]"
删除字段:
sql="alter table [表名] drop [字段名]"
修改字段:Alter TABLE [表名] Alter COLUMN [字段名] 类型(大小) NULL
新建约束:Alter TABLE [表名] ADD CONSTRAINT 约束名 CHECK ([约束字段] <= '2007-1-1')
删除约束:Alter TABLE [表名] Drop CONSTRAINT 约束名
新建默认值:Alter TABLE [表名] ADD CONSTRAINT 默认值名 DEFAULT 'Gziu.CoM' FOR [字段名]
删除默认值:Alter TABLE [表名] Drop CONSTRAINT 默认值名
=======================================
conn.open connstr
sql="alter table [tablename] add hehe char(20)"
conn.execute(sql)
response.write("添加成功")
access新建数据库和表还不简单,有了表字段初始化就更简单
=======================================
<%
session("tablen")="news"
'news是已存在的表名
session("fieldsn")="c"
'要添加的字段名
connectionstring="PRovider=microsoft.jet.oledb.4.0;data source="&server.MapPath("data/QQ.mdb")
set conn=server.createobject("adodb.connection")
conn.OPEN connectionstring
jhsql = " Alter Table "&session("tablen")&" add column "&session("fieldsn")&" real "
conn.execute(jhsql)
%>
================================
生成数据表,添加字段,其中id字段为自动增加,测试通过~~
Sub GenAutoIncrementFld()
set cn=server.CreateObject("ADODB.Connection")
set clx=server.CreateObject("ADOX.Column")
set cat=server.CreateObject("ADOX.Catalog")
set tblnam=server.CreateObject("ADOX.Table")
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\网站制作\asp操作mdb\db\test.mdb"
Set cat.ActiveConnection = cn
tblnam.Name = "Test"
clx.ParentCatalog = cat
clx.Type = 3
clx.Name = "Id"
clx.Properties("AutoIncrement") = true
tblnam.Columns.Append clx
tblnam.Columns.Append "DataField",130,20
cat.Tables.Append tblnam
Set clx = Nothing
Set cat = Nothing
cn.Close
Set cn = Nothing
End Sub
call GenAutoIncrementFld
===================================
字段类型对应数值
''---- DataTypeEnum Values ----
Const adEmpty = 0
Const adTinyInt = 16
Const adSmallInt = 2
Const adInteger = 3
Const adBigInt = 20
Const adUnsignedTinyInt = 17
Const adUnsignedSmallInt = 18
Const adUnsignedInt = 19
Const adUnsignedBigInt = 21
Const adSingle = 4
Const adDouble = 5
Const adCurrency = 6
Const adDecimal = 14
Const adNumeric = 131
Const adBoolean = 11
Const adError = 10
Const adUserDefined = 132
Const adVariant = 12
Const adIDispatch = 9
Const adIUnknown = 13
Const adGUID = 72
Const adDate = 7
Const adDBDate = 133
Const adDBTime = 134
Const adDBTimeStamp = 135
Const adBSTR = 8
Const adChar = 129
Const adVarChar = 200
Const adLongVarChar = 201
Const adWChar = 130
Const adVarWChar = 202
Const adLongVarWChar = 203
Const adBinary = 128
Const adVarBinary = 204
Const adLongVarBinary = 205
Const adChapter = 136
Const adFileTime = 64
Const adPropVariant = 138
Const adVarNumeric = 139
Const adArray = &H2000
赞助商链接