WebForm增删(批删)改查(多条件)

本文详细介绍了如何在ASP.NET WebForm中实现数据的增删改查功能,包括单个记录操作和批量删除。通过实例代码展示了多条件查询,帮助开发者更好地理解和应用这些基本的数据操作。
摘要由CSDN通过智能技术生成
//Model层(两表)
--biao:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MODER
{
   
    public class biao
    {
   
       public int ID {
    get; set; } 
       public string NAME {
    get; set; }
       public string QUYU {
    get; set; } 
       public int PIAOJIA {
    get; set; }
       public int COID {
    get; set; }
       public string MIAOSHU {
    get; set; }
       public string ZT {
    get; set; }
    }
}

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

namespace MODER
{
   
    public class country
    {
   
        public int CID {
    get; set; }
        public string GUOJIA {
    get; set; }
    }
}
//Dal访问层
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using MODER;

namespace DAL
{
   
    public class guan
    {
   
        DBHelper db = new DBHelper();
        public int Add(biao b)     //景点表做添加方法
        {
   
            string s = string.Format("insert into biao values('{0}','{1}',{2},{3},'{4}','{5}')",b.NAME,b.QUYU,b.PIAOJIA,b.COID,b.MIAOSHU,b.ZT);
            return db.ExecuteNonQuery(s);
        }
        public List<country> Xian()      //给国家表做一个显示方法
        {
   
            string s = "select *from COUNTRY";
            return db.GetToList<country>(s);
        }
        public List<biao> Show()     //景点表的显示方法
        {
   
            string s = "select*from biao";
            return db.GetToList<biao>(s);
        }
        public int Delete(int id)     //景点表的删除方法
        {
   
            string s = string.Format("delete from biao where ID={0}",id);
            return db.ExecuteNonQuery(s);

        }
        public int PDAL(string id)      //景点表的批删方法
        {
   
            string s = string.Format("delete from biao where id in ("+id+")");
            return db.ExecuteNonQuery(s);
        }
        public int UPdate(biao b)     //景点表的修改方法
        {
   
            string s = string.Format("update biao set NAME='{0}',QUYU='{1}',PIAOJIA={2},COID={3},MIAOSHU='{4}',ZT='{5}' WHERE ID={6}",b.NAME,b.QUYU,b.PIAOJIA,b.COID,b.MIAOSHU,b.ZT,b.ID);
            return db.ExecuteNonQuery(s);
        }
        public DataTable CHaid(int id)      //根据你所修改的数据去查ID
        {
   
            string s = "select*from biao where id=" + id;
            return db.GetTable(s);
        }
        //public List<biao> CHAxun(string NAME)   单条件查询,按照姓名进行查询
        //{
   
        //    string s = string.Format("select*from biao where name like '%"+NAME+"%'");
        //    return db.GetToList<biao>(s);
        //}
        public List<biao> CHAzhao(string name, int COID) //多条件查询
        {
   
            string s = string.Format("select*from biao where 1=1");
            if (name != "")                  //根据姓名去查询
            {
   
                s += "and name like '%" + name + "%'";
            }
            else if (COID != 0)              //根据所在国家查询
            {
   
                s += "and coid=" + COID;
            }
            return db.GetToList<biao>(s);
        }

    }
}
//Dbhelper封装方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Reflection;
using System.Net;
using System.Net.Sockets;

namespace DAL
{
   
    public class DBHelper
    {
   
        private SqlConnection conn = null;
        /// <summary>
        /// 构造函数
        /// </summary>
        public DBHelper()
        {
   
            if (conn == null)
            {
   
                conn = new SqlConnection("Data Source=.;Initial Catalog=KUBLL;Integrated Security=True");
            }
        }
        /// <summary>
        /// 返回DataTable查询结果
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="par"></param>
        /// <returns></returns>
        public DataTable GetTable(string sql, SqlParameter[] par = null)
        {
   
            try
            {
   
                SqlCommand com = new SqlCommand(sql, conn);
                if (par != null)
                {
   
                    com.Parameters.AddRange(par);
                }
                SqlDataAdapter ada = new SqlDataAdapter(com);
                DataTable dt = new DataTable();
                ada.Fill(dt);
                ada.Dispose();
                if (conn.State == ConnectionState.Open)
                {
   
                    this.Close();
                }
                return dt;
            }
            catch (Exception ex)
            {
   
                if (conn.State == ConnectionState.Open)
                {
   
                    this.Close();
                }
                throw;
            }
        }
        /// <summary>
        /// 返回List查询结果
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="par"></param>
        /// <returns></returns>
        public List<T> GetToList<T>(string sql, SqlParameter[] par = null)
        {
   
            List<T> li = DataTableToList<T>(GetTable(sql
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值