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;

相关内容

热门资讯

辅助透视!wpk透视辅助下载,... 辅助透视!wpk透视辅助下载,hhpoker作弊实战视频,分享教程(有挂方法)1、hhpoker作弊...
透视辅助!wepoker破解是... 透视辅助!wepoker破解是真的还是假的,wepoker模拟器哪个(透视)必胜教程(有挂揭秘)1、...
透视存在!wepoker透视脚... 透视存在!wepoker透视脚本免费app,wpk俱乐部辅助器,微扑克教程(有挂秘籍)1、每一步都需...
透视能赢!wepoker轻量版... 透视能赢!wepoker轻量版透视方法,wepoker-h5下载(透视)教你攻略(有挂解说);1、透...
透视科技!线上德州的辅助器是什... 透视科技!线上德州的辅助器是什么,wepokerplus万能挂,科技教程(有挂详情)线上德州的辅助器...
透视好友!wejoker免费脚... 透视好友!wejoker免费脚本,we-poker辅助器(透视)wpk教程(有挂揭秘);1、进入游戏...
透视代打!hhpoker透视脚... 透视代打!hhpoker透视脚本,wejoker手机版透视脚本,必赢方法(有挂秘笈)1、下载好wej...
透视新版!wepoker轻量版... 透视新版!wepoker轻量版透视,wepoker透视苹果系统(透视)必赢教程(有挂技巧)1、玩家可...
透视中牌率!wpk真的有透视嘛... 透视中牌率!wpk真的有透视嘛,wepokerplus透视脚本免费,2025新版技巧(有挂规律);1...
透视透视!wepoker好友房... 透视透视!wepoker好友房开挂,wepoker软件辅助程序(透视)力荐教程(有挂教程);1、起透...