12.16 wedfrom 查询.添加.修改.删除

查询,传值
DataClassesDataContext cont = new DataClassesDataContext();

        Repeater1.DataSource = cont.House;
        Repeater1.DataBind(); 


添加
DataClassesDataContext cont = new DataClassesDataContext();

        if (TextBox1.Text != "")
        {
            try
            {
                //取值
                string KeyWord = TextBox1.Text;
                string Area = TextBox2.Text;
                string SquareMeter = TextBox3.Text;
                string Rent = TextBox3.Text;
                string RentType = TextBox3.Text;
                string HouseType = TextBox3.Text;

                //造对象
                House data = new House();
                data.KeyWord = KeyWord;
                data.Area = Area;
                data.SquareMeter = Convert.ToInt32(SquareMeter);
                data.Rent = Convert.ToInt32(Rent);
                data.RentType = RentType;
                data.HouseType = HouseType;

                //添加到数据库
                cont.House.InsertOnSubmit(data);
                cont.SubmitChanges();
                qingkong();
	    }

添加:
    protected void Button1_Click(object sender, EventArgs e)
    {
        TestDataContext context = new TestDataContext();

        string code = txtCode.Text;
        string name = txtName.Text;
        decimal price = Convert.ToDecimal( txtPrice.Text);
        string source = txtSource.Text;
        int numbers = Convert.ToInt32( txtNumbers.Text);

        Fruit data = new Fruit();
        data.Ids = code;
        data.Name = name;
        data.Price = price;
        data.Source = source;
        data.Numbers = numbers;

        context.Fruit.InsertOnSubmit(data);
        context.SubmitChanges();

        Clear();
    }
修改:
	//先根据 ids 主键 读取数据显示到文本框中
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string ids = Request["ids"].ToString();

            TestDataContext context = new TestDataContext();

            Fruit data = context.Fruit.Where(p => p.Ids == ids).First();
            txtCode.Text = data.Ids;
            txtName.Text = data.Name;
            txtPrice.Text = data.Price.ToString();
            txtSource.Text = data.Source;
            txtNumbers.Text = data.Numbers.ToString();
        }

    }
	//修改代码
    protected void Button1_Click(object sender, EventArgs e)
    {
        TestDataContext context = new TestDataContext();
        string code = txtCode.Text;
        string name = txtName.Text;
        decimal price = Convert.ToDecimal(txtPrice.Text);
        string source = txtSource.Text;
        int numbers = Convert.ToInt32(txtNumbers.Text);

        Fruit data = context.Fruit.Where(p=>p.Ids==code).First();

        data.Name = name;
        data.Price = price;
        data.Source = source;
        data.Numbers = numbers;

        context.SubmitChanges();
    }

删除:
	//根据 ids 主键  删除
    protected void Page_Load(object sender, EventArgs e)
    {
        string ids = Request["ids"].ToString();
        TestDataContext context = new TestDataContext();

        Fruit data = context.Fruit.Where(p=>p.Ids==ids).First();

        context.Fruit.DeleteOnSubmit(data);
        context.SubmitChanges();

        Response.Redirect("Default.aspx");

    }

  

  

posted @ 2015-12-28 16:32  jlhea  阅读(149)  评论(0编辑  收藏  举报