ASP.NETMVC中使用动态SQL进行搜索,而不使用EntityFramework。
创始人
2024-09-19 09:30:51
0
  1. 在控制器中创建适当的动作方法。
public ActionResult Search(string searchTerm)
{
    List customers = new List();
    string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();

        string query = "SELECT * FROM Customers WHERE FirstName LIKE '%' + @searchTerm + '%' OR LastName LIKE '%' + @searchTerm + '%'";
        SqlCommand command = new SqlCommand(query, connection);
        command.Parameters.AddWithValue("@searchTerm", searchTerm);

        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            Customer customer = new Customer();
            customer.Id = (int)reader["Id"];
            customer.FirstName = reader["FirstName"].ToString();
            customer.LastName = reader["LastName"].ToString();
            customer.Email = reader["Email"].ToString();
            customer.Country = reader["Country"].ToString();
            customers.Add(customer);
        }
    }

    return View(customers);
}
  1. 在视图中创建适当的搜索表单。
@using (Html.BeginForm("Search", "Customer", FormMethod.Get))
{
    
    
}
  1. 在视图中创建适当的表格来显示搜索结果。
@if (Model.Count > 0)
{
    
            @foreach (Customer customer in Model)
            {
                
            }
        
First Name Last Name Email Country
@customer.FirstName @customer.LastName @customer.Email @customer.Country
} else {

No results found.

}
  1. 可能需要创建一个Customer类来存储搜索结果。
public class Customer
{
    public int Id { get; set;

相关内容

热门资讯

普及透视!xpoker辅助怎么... 普及透视!xpoker辅助怎么用,微乐小程序免费脚本,办法教程(有挂攻略)-哔哩哔哩1、打开软件启动...
专业透视!epoker有透视吗... 专业透视!epoker有透视吗,小程序微乐陕西挖坑辅助,窍门教程(新版有挂)-哔哩哔哩一、epoke...
关于透视!拱趴大菠萝挂怎么安装... 关于透视!拱趴大菠萝挂怎么安装,微信小程序微乐辅助器破解版,演示教程(讲解有挂)-哔哩哔哩1、玩家可...
教你透视!破解辅助插件wepo... 教你透视!破解辅助插件wepoker,微乐微信小程序辅助软件,举措教程(确实有挂)-哔哩哔哩所有人都...
分享透视!拱趴大菠萝挂怎么安装... 分享透视!拱趴大菠萝挂怎么安装,微乐小程序辅助器,妙招教程(有挂讲解)-哔哩哔哩1、起透看视 拱趴大...
揭露透视!wepoker国外版... 揭露透视!wepoker国外版透视,微乐陕西小程序破解器下载,策略教程(有挂实锤)-哔哩哔哩1、实时...
分享透视!菠萝德州透视脚本,微... 分享透视!菠萝德州透视脚本,微乐小程序黑科技辅助工具推荐,方案教程(有挂教程)-哔哩哔哩1、菠萝德州...
辅助透视!大菠萝789辅助器下... 辅助透视!大菠萝789辅助器下载,微乐江苏自建房辅助app,演示教程(有挂讲解)-哔哩哔哩一、大菠萝...
解谜透视!fishpoker透... 解谜透视!fishpoker透视,微信小程序修改器,教程书教程(有挂方略)-哔哩哔哩1.fishpo...
解迷透视!wepoker科技辅... 解迷透视!wepoker科技辅助器,微乐麻辣自建房辅助视频,妙招教程(详细教程)-哔哩哔哩1、这是跨...