使用ASP.NET技术 完成姓名查询功能
时间: 2023-07-20 18:11:09 浏览: 63
完成姓名查询功能的 ASP.NET 网页可以分为以下几个步骤:
1. 创建一个 ASP.NET 的 Web 应用程序,并添加一个用于查询的页面(例如 Search.aspx)。
2. 在页面中添加一个用于输入姓名的文本框和一个查询按钮。
3. 在查询按钮的 Click 事件中,获取文本框中输入的姓名,然后使用 SQL 语句查询数据库中符合条件的数据。
4. 将查询结果显示在页面上,可以使用 GridView 控件或者 Repeater 控件等。
以下是一个简单的 ASP.NET 姓名查询功能的代码示例:
```aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="Search" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>姓名查询</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<label>姓名:</label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="查询" OnClick="btnSearch_Click" />
</div>
<hr />
<asp:GridView ID="gvResult" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Name" HeaderText="姓名" />
<asp:BoundField DataField="Age" HeaderText="年龄" />
<asp:BoundField DataField="Gender" HeaderText="性别" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
```
```csharp
using System;
using System.Data;
using System.Data.SqlClient;
public partial class Search : System.Web.UI.Page
{
protected void btnSearch_Click(object sender, EventArgs e)
{
string name = txtName.Text.Trim();
if (string.IsNullOrEmpty(name))
{
return;
}
string connectionString = "Data Source=(local);Initial Catalog=MyDatabase;Integrated Security=True";
string sql = "SELECT Name, Age, Gender FROM Persons WHERE Name LIKE @name";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(sql, connection);
command.Parameters.AddWithValue("@name", "%" + name + "%");
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable table = new DataTable();
adapter.Fill(table);
gvResult.DataSource = table;
gvResult.DataBind();
}
}
}
```
以上代码假设数据库中有一个名为 Persons 的表,包含 Name、Age 和 Gender 三个字段,可以根据 Name 字段进行查询。在实际应用中,需要根据实际情况修改数据库连接字符串和 SQL 语句。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)