本来想找个兼职的,结果一个公司的CEO考偶这样的题目:要一个算法实现一个这样个功能,输入一个表的字段的序号,根据该序号字符串生成表格,一个表的字段的序号代表需要列出的字段,还要有表头(即列出所输入的序号相对应的字段的名称),要求在20分钟内搞定,结果花了偶一个多小时,暴汗:(
完整程序和数据库打包下载:https://files.cnblogs.com/Dicky/Test.rar
<form method="post" action="">
请输入需要列出的字段序号(以逗号隔开,比如1,3,5):<br><input type="text" name="No" value="<%=Trim(Request("No"))%>"> <input type="submit" name="submit" value="Submit">
</FORM>
<%
Const IsSql = 0 '定义数据库类型为Access
Function OpenConn(Conn) '打开数据库连接
Dim ConnStr
If IsSql = 1 Then '如果是SQL Server数据库
'SQL Server数据库连接参数:用户名、用户密码、数据库名、连接名(本地用local,外地用IP)
Dim SqlUsername,SqlPassword,SqlDatabaseName,SqlLocalName
SqlUsername = "sa"
SqlPassword = ""
SqlDatabaseName = "Test"
SqlLocalName = "(local)"
ConnStr = "Provider = Sqloledb; User ID = " & SqlUsername & "; Password = " & SqlPassword & "; Initial Catalog = " & SqlDatabaseName & "; Data Source = " & SqlLocalName & ";"
Else '如果是Access数据库
Dim Db
Db = "Test.mdb"
ConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=applebbs;Data Source = " & Server.MapPath(Db)
End If
On Error Resume Next
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open ConnStr
If Err Then
' Err.Clear
Set Conn = Nothing
echo "数据库连接出错,请检查连接字串。"
Response.End
End If
End Function
Function CloseConn(Conn) '关闭数据库连接
If IsObject(Conn) Then
Conn.Close
Set Conn = Nothing
End If
End Function
Function echo(Str)
Response.Write Str & VbCrlf
End Function
If Request("submit") = "Submit" Then
Call OpenConn(Conn)
strNo = Trim(Request("No"))
Strtemp = Split(strNo,",") '以逗号分割成数组
Sql = "Select * From Test"
Set Rs = Server.CreateObject("Adodb.RecordSet")
Rs.Open Sql,Conn,1,1
echo "<table border=""1"">"
MM = 1
If Rs.Eof Then '如果无记录
echo " <tr align=""center""><td>Sorry,no data!<td></tr>"
Else
echo " <tr>"
echo " <th>序号</th>"
For I = 0 To Ubound(Strtemp) '先循环列出字段名作为表头
echo " <th>" & Rs.Fields(CInt(Strtemp(I))-1).Name & "</th>"
Next
echo " </tr>"
Do While Not Rs.Eof '循环列出所有数据行
echo " <tr align=""center"">"
echo " <td>" & MM & "</td>"
For I = 0 To Ubound(Strtemp) '循环列出字段值
echo " <td>" & Rs(CInt(Strtemp(I))-1) & "</td>"
Next
echo " </tr>"
MM = MM + 1
Rs.MoveNext:Loop
End If
echo "</table>"
Rs.Close
Set Rs = Nothing
Call CloseConn(Conn)
End If %>
请输入需要列出的字段序号(以逗号隔开,比如1,3,5):<br><input type="text" name="No" value="<%=Trim(Request("No"))%>"> <input type="submit" name="submit" value="Submit">
</FORM>
<%
Const IsSql = 0 '定义数据库类型为Access
Function OpenConn(Conn) '打开数据库连接
Dim ConnStr
If IsSql = 1 Then '如果是SQL Server数据库
'SQL Server数据库连接参数:用户名、用户密码、数据库名、连接名(本地用local,外地用IP)
Dim SqlUsername,SqlPassword,SqlDatabaseName,SqlLocalName
SqlUsername = "sa"
SqlPassword = ""
SqlDatabaseName = "Test"
SqlLocalName = "(local)"
ConnStr = "Provider = Sqloledb; User ID = " & SqlUsername & "; Password = " & SqlPassword & "; Initial Catalog = " & SqlDatabaseName & "; Data Source = " & SqlLocalName & ";"
Else '如果是Access数据库
Dim Db
Db = "Test.mdb"
ConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=applebbs;Data Source = " & Server.MapPath(Db)
End If
On Error Resume Next
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open ConnStr
If Err Then
' Err.Clear
Set Conn = Nothing
echo "数据库连接出错,请检查连接字串。"
Response.End
End If
End Function
Function CloseConn(Conn) '关闭数据库连接
If IsObject(Conn) Then
Conn.Close
Set Conn = Nothing
End If
End Function
Function echo(Str)
Response.Write Str & VbCrlf
End Function
If Request("submit") = "Submit" Then
Call OpenConn(Conn)
strNo = Trim(Request("No"))
Strtemp = Split(strNo,",") '以逗号分割成数组
Sql = "Select * From Test"
Set Rs = Server.CreateObject("Adodb.RecordSet")
Rs.Open Sql,Conn,1,1
echo "<table border=""1"">"
MM = 1
If Rs.Eof Then '如果无记录
echo " <tr align=""center""><td>Sorry,no data!<td></tr>"
Else
echo " <tr>"
echo " <th>序号</th>"
For I = 0 To Ubound(Strtemp) '先循环列出字段名作为表头
echo " <th>" & Rs.Fields(CInt(Strtemp(I))-1).Name & "</th>"
Next
echo " </tr>"
Do While Not Rs.Eof '循环列出所有数据行
echo " <tr align=""center"">"
echo " <td>" & MM & "</td>"
For I = 0 To Ubound(Strtemp) '循环列出字段值
echo " <td>" & Rs(CInt(Strtemp(I))-1) & "</td>"
Next
echo " </tr>"
MM = MM + 1
Rs.MoveNext:Loop
End If
echo "</table>"
Rs.Close
Set Rs = Nothing
Call CloseConn(Conn)
End If %>