数据库结构操作。适应于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