.Net Web模糊查询+删除

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Models
{
    public class StudentInfo
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string Phone { get; set; }
        public string Hobby { get; set; }
    }
}

using Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class StudentInfoService
    {
        //1.查询所有数据的方法
        public  static  List<StudentInfo> GetStudentInfos()
        {
            //查询语句
            string sql = "select * from StudentInfo";
            //借助DBHelper查询
            DataTable dt =DBHelper.ExecuteTable(sql);
            //创建集合
            List<StudentInfo> list = new List<StudentInfo>();
            foreach (DataRow dr in dt.Rows)
            {
                StudentInfo stu = new StudentInfo();
                stu.ID =int.Parse( dr[0].ToString());
                stu.Name = dr[1].ToString();
                stu.Gender = dr[2].ToString();
                stu.Phone = dr[3].ToString();
                stu.Hobby = dr[4].ToString();
                list.Add(stu);
            }
            return list;//返回集合
        }
        //2.模糊查询的方法
        public static List<StudentInfo> GetStudentInfoByName(string key)
        {
            string sql = string.Format("select * from StudentInfo where Name like '%{0}%'",key);
            //借助DBHelper查询
            DataTable dt = DBHelper.ExecuteTable(sql);
            //创建集合
            List<StudentInfo> list = new List<StudentInfo>();
            foreach (DataRow dr in dt.Rows)
            {
                StudentInfo stu = new StudentInfo();
                stu.ID = int.Parse(dr[0].ToString());
                stu.Name = dr[1].ToString();
                stu.Gender = dr[2].ToString();
                stu.Phone = dr[3].ToString();
                stu.Hobby = dr[4].ToString();
                list.Add(stu);
            }
            return list;//返回集合
        }

        //3.删除的方法
        public static int DeleteStudentInfo(int id)
        {
            string sql = "delete from StudentInfo where ID=" + id;
            return DBHelper.ExecuteQuery(sql);//返回受影响行数
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public static class DBHelper
{
    static string connstr = "server=.;database=StudentDB;uid=sa;pwd=1234";
    
    /// <summary>
    /// 执行T-SQL返回一张表信息
    /// </summary>
    /// <param name="sqlStr">T-SQL查询语句</param>
    /// <returns></returns>
    public static DataTable ExecuteTable(string sqlStr)
    {
        using (SqlConnection conn = new SqlConnection(connstr))
        {
            conn.Open();
            using (SqlCommand comm = new SqlCommand(sqlStr, conn))
            {
                SqlDataAdapter sda = new SqlDataAdapter(comm);
                DataSet set = new DataSet();
                sda.Fill(set);
                return set.Tables[0];
            }
        }
    }


    /// <summary>
    /// 执行T-SQL完成增删改的操作
    /// </summary>
    /// <param name="sqlStr">T-SQL查询语句</param>
    /// <returns></returns>
    public static int ExecuteQuery(string sqlStr)
    {
        using (SqlConnection conn = new SqlConnection(connstr))
        {
            conn.Open();
            using (SqlCommand comm = new SqlCommand(sqlStr, conn))
            {
                return comm.ExecuteNonQuery();
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using DAL;

namespace BLL
{
    public class StudentInfoManager
    {
        public static List<StudentInfo> GetStudent()
        {
            return StudentInfoService.GetStudentInfos();
        }

        public static List<StudentInfo> GetStudentByName(string key)
        {
            return StudentInfoService.GetStudentInfoByName(key);
        }
        public static int DeleteStudentInfo(int id)
        {
            return StudentInfoService.DeleteStudentInfo(id);
        }
    }
}
<asp:TextBox ID="txtKey" runat="server"></asp:TextBox>
            <asp:Button ID="BtnSel" runat="server" Text="查询" OnClick="BtnSel_Click"/>

            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                OnRowDeleting="GridView1_RowDeleting">
                <Columns>
                    <asp:BoundField DataField="ID" HeaderText="学号" />
                    <asp:BoundField DataField="Name" HeaderText="姓名" />
                    <asp:BoundField DataField="Gender" HeaderText="性别" />
                    <asp:BoundField DataField="Phone" HeaderText="电话" />
                    <asp:BoundField DataField="Hobby" HeaderText="爱好" />
                    <%--<asp:TemplateField ShowHeader="False">
                        <ItemTemplate>
                            <asp:LinkButton ID="DelBtn" runat="server"
                                 Text="删除" CommandArgument='<%#Eval("ID") %>' OnCommand="DelBtn_Command" OnClientClick="return confirm('确认删除吗?')"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>--%>

                    <asp:TemplateField ShowHeader="False">
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server"  
                                 Text="删除" OnCommand="LinkButton1_Command"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>

                </Columns>
            </asp:GridView>
public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShowStu();
            }
        }

        private void ShowStu()
        {
            this.GridView1.DataSource = StudentInfoManager.GetStudent();
            this.GridView1.DataBind();
        }

        protected void BtnSel_Click(object sender, EventArgs e)
        {
            //获取文本框的内容
            string key = this.txtKey.Text;
            this.GridView1.DataSource=StudentInfoManager.GetStudentByName(key);
            this.GridView1.DataBind();
        }

    
        protected void DelBtn_Command(object sender, CommandEventArgs e)
        {
            //获取ID
            int id = int.Parse(e.CommandArgument.ToString());
            int a=StudentInfoManager.DeleteStudentInfo(id);
            if (a>0)
            {
                ShowStu();//刷新数据
                Response.Write("<script>alert('删除成功');</script>");
            }
            else
            {
                Response.Write("<script>alert('删除失败');</script>");
            }
        }

        //protected void LinkButton1_Command(object sender, CommandEventArgs e)
        //{
        //    Response.Write("Command方法");
        //}

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Response.Write("RowDeleting方法");
        }

        protected void LinkButton1_Command(object sender, CommandEventArgs e)
        {
            Response.Write("Command方法");
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值