分享
 
 
 

数据库结构操作

王朝other·作者佚名  2006-12-16
窄屏简体版  字體: |||超大  

数据库结构操作。适应于access,sql server等常见的数据库。

1。建立连接。

可以通过ODBC或OLEDB连接。

Set gObjDC = Server.CreateObject("ADODB.Connection")

dim strconn,myDSN

myDSN="test"

strconn="DSN="&myDSN&";uid=sa;pwd="

'strconn ="Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Initial Catalog=MeiSha;Data Source=tonny"

gObjDC.ConnectionString=strconn

gObjDC.Open

2。显示所有表

set gObjRS = gObjDC.OpenSchema(adSchemaTables)

Do While Not gObjRS.EOF

If gObjRS.Fields("TABLE_TYPE") = "TABLE" AND Left(gObjRS.Fields("TABLE_NAME"), 4) <> "MSys" Then

'不必把系统表显示出来

Response.Write "<TR>"

Response.Write "<TD>" & gObjRS.Fields("TABLE_NAME") & "</TD>"

myPLink = "?DSN_NAME=" & myDSN & "&Table_Name=" & gObjRS.Fields("TABLE_NAME")

Response.Write "<TD> <A HREF=tablestruct.asp" & myPLink & ">Structure</A> </TD>"

Response.Write "<TD> <A HREF=tablecontent.asp" & myPLink & ">Content</A> </TD>"

Response.Write "</TR>" & vbCrLf

End If

gObjRS.MoveNext

Loop

gObjRS.Close

3。新建表

<FORM METHOD=POST ACTION="definetable.asp?DSN_Name=<% =myDSN %>">

Table Name :<BR>

<INPUT TYPE="text" NAME="Table_Name"><BR>

Field Count :<BR>

<INPUT TYPE="text" NAME="Field_Count"><BR>

<BR>

<INPUT TYPE="submit" VALUE="Create">

</FORM>

definetable.asp中主要源码

myFieldCount = Request.Form("Field_Count")

<FORM METHOD=POST ACTION="createtable.asp?DSN_Name=<% =Request.Form("DSN_Name") %>&Table_Name=<% =Request.Form("Table_Name") %>&Field_Count=<%=myFieldCount %>" ID="Form1">

<TABLE ID="Table2">

<TR>

<TD>Name</TD>

<TD>Type</TD>

<TD>Length</TD>

<TD>Null</TD>

<TD>Primary Key</TD>

<TD>Unique Index</TD>

</TR>

<% For i=1 to myFieldCount%>

<TR>

<TD><INPUT TYPE="text" NAME=<% ="FieldName_" & i %> ></TD>

<TD><SELECT SIZE=1 NAME=<% ="FieldType_" & i %> >

<!-请注意:此处根据不同数据库填写字段类型->

<OPTION>BINARY

<OPTION>BIT

<OPTION>BYTE

<OPTION>COUNTER

<OPTION>CURRENCY

<OPTION>DATETIME

<OPTION>SINGLE

<OPTION>DOUBLE

<OPTION>SHORT

<OPTION>LONG

<OPTION>LONGTEXT

<OPTION>LONGBINARY

<OPTION>TEXT

</SELECT>

</TD>

<TD><INPUT TYPE="text" SIZE=5 NAME=<% ="FieldLength_" & i %> ID="Text2"></TD>

<TD>

<SELECT SIZE=1 NAME=<% ="FieldNull_" & i %> ID="Select2">

<OPTION>not null

<OPTION>null

</SELECT>

</TD>

<TD><INPUT TYPE="checkbox" NAME=<% ="FieldPrimary_" & i %> ID="Checkbox1"></TD>

<TD><INPUT TYPE="checkbox" NAME=<% ="FieldUnique_" & i %> ID="Checkbox2"></TD>

</TR>

<%Next%>

</TABLE>

<INPUT TYPE="reset" VALUE="Clear" ID="Reset1" NAME="Reset1"> <INPUT TYPE="submit" VALUE="Create" ID="Submit1" NAME="Submit1">

</FORM>

createtable.asp中主要源码

myPrimary = ""

mySQLQueryString = "CREATE TABLE " & myTable &" ("

myFieldCount = CInt(Request.QueryString("Field_Count"))

For i = 1 to myFieldCount

myFieldName = Request.Form("FieldName_"&i)

mySQLQueryString = mySQLQueryString & Chr(34) & _

myFieldName & Chr(34) & " " &_

Request.Form("FieldType_"&i)

myLength = Request.Form("FieldLength_"&i)

If isNumeric(myLength) Then

mySQLQueryString = mySQLQueryString & " (" & myLength & ") "

End If

mySQLQueryString = mySQLQueryString & " " & Request.Form("FieldNull_"&i)

If Request.Form("FieldUnique_"&i) <> "" Then

mySQLQueryString = mySQLQueryString & " CONSTRAINT pk" & myFieldName & " UNIQUE"

End If

mySQLQueryString = mySQLQueryString & ", "

If Request.Form("FieldPrimary_"&i) <> "" Then

myPrimary = myPrimary & Chr(34) & myFieldName & Chr(34) & ", "

End If

Next

mySQLQueryString = Left(mySQLQueryString, Len(mySQLQueryString)-2)

If myPrimary <> "" Then

myPrimary = Left(myPrimary, Len(myPrimary)-2)

mySQLQueryString = mySQLQueryString & ", " & "CONSTRAINT Contraint PRIMARY KEY(" & myPrimary & ")"

End If

mySQLQueryString = mySQLQueryString & ");"

'Response.Write mySQLQueryString

gObjDC.execute mySQLQueryString

||||||4。显示表结构

set gObjRS = Server.CreateObject("ADODB.Recordset")

gObjRS.Open "[" & myTable & "]", gObjDC, adOpenForwardOnly, adLockReadOnly

For i = 0 to gObjRS.Fields.Count - 1

Response.Write "<TR>" & vbCrlf

Response.Write "<TD>" & gObjRS.Fields(i).Name & "</TD>" & vbCrlf

myType = GetType(gObjRS.Fields(i).Type)

Response.Write "<TD>" & myType & "</TD>"& vbCrlf

myLength = " "

If myType <> "LONGTEXT" AND myType <> "LONGBINARY" Then

myLength = gObjRS.Fields(i).DefinedSize

End If

Response.Write "<TD>" & myLength & "</TD>"& vbCrlf

Response.Write "<TD>"& vbCrlf

myLink = "dropfield1.asp?DSN_Name=" & myDSN & "&Table_Name=" & myTable & "&Field_Name=" & gObjRS.Fields(i).Name

Response.Write "<A HREF='" & myLink & "'><B> Drop " & gObjRS.Fields(i).Name & " field</B></A>"

Response.Write "</TD>"& vbCrlf

Response.Write "</TR>"& vbCrlf

Next

gObjRS.Close

Function GetType(pConstant)

Select Case pConstant

Case adBinary ’128

GetType = "BINARY"

Case adBoolean ‘11

GetType = "BOOLEAN"

Case adUnsignedTinyInt ’17

GetType = "BYTE"

Case adInteger ‘3

GetType = "LONG"

Case adCurrency ’6

GetType = "CURRENCY"

Case adDBTimeStamp ‘135

GetType = "DATETIME"

Case adSingle ’4

GetType = "SINGLE"

Case adDouble ‘5

GetType = "DOUBLE"

Case adSmallInt ’2

GetType = "SHORT"

Case adLongVarChar ‘201

GetType = "LONGTEXT"

Case adLongVarBinary ’205

GetType = "LONGBINARY"

Case adVarChar ‘200

GetType = "TEXT"

Case Else

GetType = "UNKNOW(" & pConstant & ")"

End Select

End Function

5。添加一字段

mySQLQueryString = "ALTER TABLE " & myTable & " ADD COLUMN " & Request.Form("FieldName") & " "

mySQLQueryString = mySQLQueryString & Request.Form("FieldType") & " "

myLength = Request.Form("FieldLength")

If isNumeric(myLength) Then

mySQLQueryString = mySQLQueryString & "(" & myLength & ") "

End If

mySQLQueryString = mySQLQueryString & Request.Form("FieldNull") & " "

If Request.Form("FieldUnique") <> "" Then

mySQLQueryString = mySQLQueryString & " CONSTRAINT pk" & myFieldName & " UNIQUE"

End If

gObjDC.execute mySQLQueryString

6。删除一字段

mySQLQueryString = "ALTER TABLE " & myTable & " DROP COLUMN " & Request.QueryString("Field_Name") & ";"

gObjDC.execute mySQLQueryString

7。删除一表

mySQLQueryString = "DROP TABLE " & myTable

gObjDC.execute mySQLQueryString

附:'---- 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 adDBFileTime = 137

Const adPropVariant = 138

Const adVarNumeric = 139

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有