ASP.NET知识点总结4

数据的增删改查。

第一步:封装实体。类person

 public class person
    {
        public string pID;
        public string pName;
        public string pSex;
    }

第二步:操作类。写增删改查的方法。

public class Operate
    {
        public static SqlConnection createConn()
        {
            return new SqlConnection("server=.;database=adoNetTest;uid=sa;pwd=123456;");//连接数据库
        }

         public static bool findPerson(string pID)//查用户,根据ID
        {
            SqlConnection conn = Operate.createConn();
            conn.Open();
            SqlCommand cmd = new SqlCommand("select count(*) from person where pID="+pID,conn);
            int count = Convert.ToInt32(cmd.ExecuteScalar());//第一行的第一列的数据。
            if (count>0)
            {
                return true;//用户存在
            }
            else
            {
                return false;//用户不存在可以注册
            }
 
        }
        public static bool insertOperate(person p) //添加操作
        {
            try
            {
                SqlConnection conn = Operate.createConn();
                conn.Open();
                SqlCommand cmd = new SqlCommand("insert into person values(@pID,@pName,@pSex)", conn);//带参数的SQL语句
                SqlParameter para = new SqlParameter("@pID", SqlDbType.VarChar, 10);//类型和大小
                para.Value = p.pID;//赋值
                cmd.Parameters.Add(para);//添加
                para = new SqlParameter("@pName", SqlDbType.VarChar, 20);
                para.Value = p.pName;
                cmd.Parameters.Add(para);
                para = new SqlParameter("@pSex", SqlDbType.VarChar, 2);
                para.Value = p.pSex;
                cmd.Parameters.Add(para);
                cmd.ExecuteNonQuery();//执行SQL语句,返回时int类型。
                return true;     //执行到这句,就是true。 
            }
            catch (Exception e)
            {

                return false;//发生异常,返回错误
            }
           

                                
 
        }


        public static DataTable selectAllPerson()
        {
            SqlConnection conn = Operate.createConn();
            //conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter();//数据适配器
            sda.SelectCommand = new SqlCommand("select * from person",conn);
            DataSet ds = new DataSet();
            sda.Fill(ds,"person");//适配器的fill方法,传入dataset对象,得到一张名为person的表
            return ds.Tables["person"];返回这张表

        }

        public static bool updateOperate(person p)
        {

            try
            {
                SqlConnection conn=Operate.createConn();
                conn.Open();
                SqlCommand cmd=new SqlCommand("update person set personName="+p.pName+", personSex='"+p.pSex +"'  where pID="+p.pID,conn);
                cmd.ExecuteNonQuery();

                return true; 

            }
            catch (Exception)
            {

                return false;
            }
               
            }


        public static bool deleteOperate(string pID)
        {
            try
            {
                SqlConnection conn = Operate.createConn();
                conn.Open();
                SqlCommand cmd = new SqlCommand("delete from person where pID="+pID,conn);
                cmd.ExecuteNonQuery();


                return true;
            }
            catch ( Exception e)
            {

                return false;
            }
        }
           
            }

 

第三步:自定义的验证控件,显示用户是否已经存在。

 protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string pid = args.Value;
            if (Operate.findPerson(pid))//用户存在
            {
                args.IsValid = false;//不可以注册
            }
            else
            {
                args.IsValid = true;//用户不存在,可以注册
            }
        }

第四步:添加用户。

 
   public void fillDg()//该方法,查询到所有的用户来填充GridView1
        {
            GridView1.DataSource = Operate.selectAllPerson();
            GridView1.DataBind();
        }

         protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)//页面通过了
            {
                
           
            person p = new person();//实例化person
            p.pID = txtID.Text;//赋值
            p.pName = txtName.Text;
            if (rbNan.Checked)//选中男的性别
            {
                p.pSex = "男";
            }
            else
            {
                p.pSex = "女";
            }
            if (Operate.insertOperate(p))
            {
                Response.Write("插入成功");
            }
            else
            {
                Response.Write("插入失败");
            }
            fillDg();//调用自己定义的方法,显示给用户看新的数据库中的数据。
            }
        }

第五部:修改。

 protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!CustomValidator1.IsValid)//如果严证控件为true,表明该用户不存在,取反则证明存在,才能修改。
            {
                person p = new person();
                p.pID = txtID.Text;
                p.pName = txtName.Text;
                if (rbNan.Checked)
                {
                    p.pSex = "男";
                }
                else
                {
                    p.pSex = "女";
                }
                if (Operate.updateOperate(p))
                {
                    Response.Write("修改成功了。");
                    this.fillDg();
                }
                else
                {
                    Response.Write("修改失败");
                }

            }
        }

第六部:删除。

 protected void 删除_Click(object sender, EventArgs e)
        {
            if (!this.CustomValidator1.IsValid)
            {
                if (Operate.deleteOperate(txtID.Text))
                {
                    Response.Write("删除成功");
                    this.fillDg();
                }
                else
                {
                    Response.Write("删除失败");
                }

            }
        }

第七步:查询用户。

 protected void btnQuery_Click(object sender, EventArgs e)
        {
            string condition = "" ;//用户选择的条件
            if (CheckBox1.Checked)//选择了用户名
            {
                condition = "pID="+txtID.Text;
            }
            else
            {
                condition = "pID like '%'";//没有选择用户名,like%,查询了所有,相当于忽略了这个条件。
            }
            if (CheckBox2.Checked)//选中了姓名。
            {
                condition += " and personName like '%" + txtName.Text + "%'";//名字中有这个字或词就会被选出来。
            }
            if (CheckBox3.Checked)
            {
                if (rbNan.Checked)
                {
                    condition += " and personSex='男'";//性别为男
                }
                else
                {
                    condition += " and personSex='女'";
                }
                
            }


            DataView dw = new DataView(Operate.selectAllPerson());//先用dataview查询显示所有的
            dw.RowFilter = condition;//用dataview的过滤器方法。
            dw.Sort = "pID Desc";//降序
            dw.Sort = "pID asc";//升序  任意选择一种
            GridView1.DataSource = dw;
            GridView1.DataBind();

        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值