ASP SQL 简单查询页面
一、
在 Web 开发中,ASP(Active Server Pages)结合 SQL(Structured Query Language)可以实现对数据库的操作并在网页上展示数据,一个简单的查询页面可以让用户通过输入条件来查询数据库中的数据,并将结果显示在页面上。
二、环境搭建
操作系统:Windows Server(以 Windows Server 2019 为例)。
Web 服务器:Internet Information Services(IIS)。
数据库管理系统:Microsoft SQL Server(以 SQL Server 2019 为例)。
开发工具:Visual Studio Code。
三、数据库设计
假设我们有一个名为Students
的表,包含以下字段:
字段名 | 数据类型 | 说明 |
ID | int | 主键,学生编号 |
Name | nvarchar(50) | 学生姓名 |
Age | int | 学生年龄 |
Gender | nvarchar(10) | 学生性别 |
使用 SQL Server Management Studio 创建上述表并插入一些测试数据:
CREATE TABLE Students ( ID INT PRIMARY KEY, Name NVARCHAR(50), Age INT, Gender NVARCHAR(10) ); INSERT INTO Students (ID, Name, Age, Gender) VALUES (1, 'Tom', 20, 'Male'); INSERT INTO Students (ID, Name, Age, Gender) VALUES (2, 'Lucy', 19, 'Female'); INSERT INTO Students (ID, Name, Age, Gender) VALUES (3, 'Lily', 21, 'Female');
四、ASP 代码实现
(一)连接数据库
在 ASP 文件中,首先需要建立与 SQL Server 的连接,以下是示例代码:
<% Set conn = Server.CreateObject("ADODB.Connection") conn.Open "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=TestDB;User Id=sa;Password=your_password;" %>
上述代码中,TestDB
是数据库名称,sa
是 SQL Server 的登录用户名,your_password
是对应的密码,请根据实际情况修改。
(二)接收查询条件
假设我们要根据学生姓名进行查询,可以通过表单接收用户输入的条件:
<form action="query.asp" method="get"> <input type="text" name="name" placeholder="请输入学生姓名"> <input type="submit" value="查询"> </form>
在query.asp
文件中获取查询条件:
<% name = Request.QueryString("name") %>
(三)执行查询语句
根据用户输入的条件构建 SQL 查询语句并执行:
<% sql = "SELECT * FROM Students WHERE Name LIKE '%" & name & "%'" Set rs = conn.Execute(sql) %>
上述代码中,使用LIKE
关键字和通配符%
来实现模糊查询。
(四)显示查询结果
遍历结果集并显示查询到的数据:
<table border="1"> <tr> <th>ID</th> <th>Name</th> <th>Age</th> <th>Gender</th> </tr> <% Do While Not rs.EOF %> <tr> <td><%=rs("ID")%></td> <td><%=rs("Name")%></td> <td><%=rs("Age")%></td> <td><%=rs("Gender")%></td> </tr> <% rs.MoveNext Loop %> </table>
关闭记录集和连接:
<% rs.Close Set rs = Nothing conn.Close Set conn = Nothing %>
五、完整示例代码
以下是query.asp
的完整代码:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> <!DOCTYPE html> <html> <head> <meta charset="utf8"> <title>学生查询</title> </head> <body> <% Set conn = Server.CreateObject("ADODB.Connection") conn.Open "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=TestDB;User Id=sa;Password=your_password;" %> <form action="query.asp" method="get"> <input type="text" name="name" placeholder="请输入学生姓名"> <input type="submit" value="查询"> </form> <% name = Request.QueryString("name") if name <> "" then sql = "SELECT * FROM Students WHERE Name LIKE '%" & name & "%'" Set rs = conn.Execute(sql) %> <table border="1"> <tr> <th>ID</th> <th>Name</th> <th>Age</th> <th>Gender</th> </tr> <% Do While Not rs.EOF %> <tr> <td><%=rs("ID")%></td> <td><%=rs("Name")%></td> <td><%=rs("Age")%></td> <td><%=rs("Gender")%></td> </tr> <% rs.MoveNext Loop %> </table> <% rs.Close Set rs = Nothing end if conn.Close Set conn = Nothing %> </body> </html>
六、相关问题与解答
(一)问题一:如何防止 SQL 注入攻击?
解答:在上述代码中,直接将用户输入拼接到 SQL 语句中存在 SQL 注入风险,可以使用参数化查询来防止 SQL 注入,对于 ASP 结合 SQL Server,可以使用存储过程并传递参数,以下是使用存储过程的示例代码:
<% Set cmd = Server.CreateObject("ADODB.Command") Set cmd.ActiveConnection = conn cmd.CommandText = "{call GetStudentsByName ?}" cmd.Parameters.Append cmd.CreateParameter("@name", adVarChar, adParamInput, 50, name) Set rs = cmd.Execute %>
这样,用户输入的值将作为参数传递给存储过程,而不是直接拼接到 SQL 语句中,从而有效防止 SQL 注入攻击。
(二)问题二:如果查询结果为空,如何在页面上提示用户?
解答:可以在显示查询结果之前,判断记录集是否为空,如果为空,则显示相应的提示信息,以下是示例代码:
<% if rs.EOF and rs.BOF then Response.Write "没有找到匹配的学生信息。" else %> <table border="1"> <!表格内容省略 > </table> <% end if %>
这样,当没有查询到结果时,页面上会显示“没有找到匹配的学生信息。”的提示信息。