网络编程
位置:首页>> 网络编程>> Asp编程>> 创建数据表/创建列的一些asp函数

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

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

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

有时候要通过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

 

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com