创建数据表/创建列的一些asp函数

作者:亮亮 来源:亮亮blog 时间:2008-06-24 12:21:00 

有时候要通过asp代码在数据库中创建表和列,下面的就是这些操作的函数。

1.检测表是否存在

tbName 检测的表的名称

dbTp 数据库的类型1为sql,0为access

返回1存在,返回0不存在

Function isTable(tbName,dbTp)
    dim isTbSql
    if dbTp="1" then
        isTbSql="select count(*) as num from sysobjects where id=object_id('"&tbName&"')"
        dim rs:set rs=conn.execute(isTbSql)
        if rs("num")>0 then
            isTable="1"
        else
            isTable="0"
        end if
        rs.close
        set rs=nothing
    else
        on error resume next
        dim rs2:set rs2=conn.execute(tbName)
        if err.number=0 then
            isTable="1"
        else
            isTable="0"
        end if
        rs2.close
        set rs2=nothing
    end if
End Function

2.检测字段是否存在

tbName 要检测的字段所在的表

colName 要检测的字段

dbTy 数据库类型 1为sql,0为access

返回1表示字段存在,返回0表示不存在,当然若表不存在也是返回0

Function isCol(tbName,colName,dbTy)
  dim isColSql
  if dbTy="1" then
    isColSql="select count(*) as num from syscolumns where id=object_id('"&tbName&"') and name='"&colName&"'"
    dim rs:set rs=conn.execute(isColSql)
    if rs("num")>0 then
      isCol="1"
     else
       isCol="0"
     end if
     rs.close
     set rs=nothing
  else
    isColSql="select "&colName&" from "&tbName
    on error resume next
    dim rs1:set rs1=conn.execute(isColSql)
    if err.number=0 then
      isCol="1"
    else
      iscol="0"
    end if
    rs1.close
    set rs1=nothing
  end if
End Function

3.添加表函数

tbName 表的名称

pkCol 表的第一个字段,此字段必须为你创建表的主键,并且自动编号

Function addTable(byVal tbName,byVal pkCol)
    Dim TSql
    if isTable(tbName,dbType)="0" then
    TSql="Create Table ["&TbName&"]("&pkCol&" int IDENTITY (1,1) CONSTRAINT PrimaryKey PRIMARY KEY);"
    conn.Execute(TSql)
    response.Write("表创建成功")
    else
    response.Write("要创建的表存在")
    end if
End Function

4.添加列名

tbName 表名称

colName 列名称

colType 列的数据类型;如nvarchar(50)

Function addCol(tbName,colName,colType)
  if isTable(tbName,dbType)="1" and isCol(tbName,colName,dbType)="0" then
    Dim TSql
    TSql="Alter Table ["&tbName&"] Add "&colName&" "&colType
    conn.Execute(TSql)
    response.Write("列:"&colName&" 创建成功")
  else
    response.Write("列:"&colName&" 没有创建,可能表不存在或字段重复")
  end if
End Function

5.删除列

Function delCol(tbName,colName)
  if isTable(tbName,dbType)="1" and isCol(tbName,colName,dbType)="1" then
    Dim TSql
    TSql="Alter Table ["&tbName&"] Drop column "&colName
    conn.Execute(TSql)
    response.Write("列: "&colName&" 删除成功")
  else
    response.Write("表或者列不存在")
  end if
End Function

使用方法相信就很简单了,看下面的示例:

dim connStr
const dbType="1"
if dbType="0" then
  connStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.MapPath("../test.mdb")
else
  connStr="Provider=Sqloledb;User Id=sa;Password=12345a;Initial Catalog=test;Data Source=(local)"
end if
dim conn:set conn=server.CreateObject("adodb.connection")
conn.open connStr
'下面的就是例子
addTable "myTb1","mId"
'addCol "myTb1","mCol_1","nvarchar(50)"
'delCol "myTb1","mCol_1"
'response.Write(isCol("myTb1","mCol_1","0"))
conn.close
set conn=nothing

 

标签:表,字段,数据库,access,函数
0
投稿

猜你喜欢

  • 网页设计的色彩思考

    2007-10-19 13:30:00
  • .NET中书写XML的一种简单方法

    2007-08-23 12:55:00
  • 基于Oracle的面向对象技术入门基础简析开发者网络Oracle

    2010-07-18 12:57:00
  • 一个图片后加载的代码

    2008-09-28 13:03:00
  • Study jQuery in a Simplified Way

    2010-01-30 12:55:00
  • 两个不太常用的 CSS Hack

    2008-06-27 12:49:00
  • 带农历及节日的js日历源码

    2010-08-01 17:38:00
  • 层叠加的五条叠加法则

    2009-05-01 12:07:00
  • 用VB将ASP代码封装成DLL

    2007-09-28 12:46:00
  • 利用索引提高SQL Server数据处理的效率

    2009-01-08 15:32:00
  • 论标志的简洁性

    2009-10-27 16:05:00
  • 最新CSS兼容方案

    2008-08-13 13:20:00
  • ASP运行出错:缺少对象: xmlDoc.documentElement错误解决方法

    2012-11-30 20:40:52
  • 网站中文字的视觉设计

    2008-04-16 13:35:00
  • ASP中从数据库读取二进制文件数据代码

    2010-04-24 15:44:00
  • mysql 5.5.8的几个注意事项

    2011-01-04 19:34:00
  • 自动备份Oracle数据库

    2010-07-31 13:10:00
  • ie的javascript失效问题

    2009-09-21 12:49:00
  • Javascript命名禁区[110407.updated]

    2011-04-28 09:48:00
  • ASP技巧 挂QQ的网页源代码ASP/PHP

    2009-01-05 12:36:00
  • asp之家 网络编程 m.aspxhome.com