<% response.expires = -1 SqlDatabase = "Database.mdb" '数据库路径 SqlProvider = "Microsoft.Jet.OLEDB.4.0" '驱动程序[Microsoft.Jet.OLEDB.4.0;Microsoft.ACE.OLEDB.12.0] SqlPassword = "ACCESS数据库密码" ConnStr = "Provider=" & SqlProvider & ";Jet OLEDB:Database Password=" & SqlPassword & ";Data Source=" & Server.MapPath(SqlDatabase) Set Conn = Server.CreateObject("ADODB.Connection") Conn.Open ConnStr sql = "select * from company where id=1" sql = "select * from company where id=" & Request.QueryString("id") response.write(sql) Set rs = Conn.Execute(sql) response.write("<table>") Do Until rs.EOF response.write("<tr>") For Each x in rs.Fields response.write("<td><b>" & x.Name & "</b></td>") response.write("<td>" & x.Value & "</td>") Next rs.MoveNext response.write("</tr>") Loop response.write("</table>") %> 代码修复
时间: 2024-04-10 22:27:20 浏览: 162
你提供的代码存在一些问题,我来为你修复一下:
```asp
<%
response.expires = -1
SqlDatabase = "Database.mdb" '数据库路径
SqlProvider = "Microsoft.Jet.OLEDB.4.0" '驱动程序[Microsoft.Jet.OLEDB.4.0;Microsoft.ACE.OLEDB.12.0]
SqlPassword = "ACCESS数据库密码"
ConnStr = "Provider=" & SqlProvider & ";Jet OLEDB:Database Password=" & SqlPassword & ";Data Source=" & Server.MapPath(SqlDatabase)
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open ConnStr
sql = "select * from company where id=1"
if Request.QueryString("id") <> "" then
sql = "select * from company where id=" & Request.QueryString("id")
end if
response.write(sql)
Set rs = Conn.Execute(sql)
response.write("<table>")
Do Until rs.EOF
response.write("<tr>")
For Each x in rs.Fields
response.write("<td><b>" & x.Name & "</b></td>")
response.write("<td>" & x.Value & "</td>")
Next
response.write("</tr>")
rs.MoveNext
Loop
response.write("</table>")
rs.Close
Set rs = Nothing
Conn.Close
Set Conn = Nothing
%>
```
在修复的代码中,我添加了对 `Request.QueryString("id")` 的判断,以便根据是否提供了 `id` 参数来选择查询的语句。同时,我还添加了关闭数据库连接和释放对象的代码,以确保资源的正确释放。请注意,这里只是简单修复了代码存在的问题,实际应用中还需要做一些安全性和错误处理的考虑。
阅读全文