代码之家  ›  专栏  ›  技术社区  ›  User

Linq to SQL:如何更新包含多个表数据的FormView?

  •  1
  • User  · 技术社区  · 15 年前

    举个简单的例子:

    我有一个客户表和一个客户表,都有customerid字段。我想要能更新两个表中数据的FormView。

    FormView:

    First Name (Customer)
    Last Name (Customer)
    Address (Customer)
    AccountRate (Account)
    

    如何使用Linq to SQL执行此操作?我是否只使用bind语句,比如bind(“account.accountRate”)?或者在itemupdating事件或类似事件中我需要做一些魔术?

    3 回复  |  直到 15 年前
        1
  •  1
  •   Paul Creasey    15 年前

    你需要加入

    from c in db.Customer
    join a in db.Account
    on a.customerID equals c.customerID
    select new Model {FirstName = c.firstName,LastName = c.lastName,Address = c.address,AccountRate = a.accountRate}
    

    将您的模型定义为某个类并绑定到该类。

        2
  •  1
  •   Ewerton    15 年前

    我从不使用Linq to SQL,但在实体框架中,我需要这样做:

    protected void ObjectDataSource_Updating(object sender, ObjectDataSourceMethodEventArgs e)
        {
            // Declaring the Entity context 
            AvalonEntities db = new AvalonEntities();
    
            // In updating méthod of a ObjectDataSource the "e" arg is my customer, so i grab this
            customer cus = (customer)e.InputParameters[0];
    
    
            // say that i have a DropDown to select the account ...
            // I Get de DropDown and get your SelectedValue.
            DropDownList ddlAccount  = (DropDownList)FormView1.FindControl("ddlAccount");
            // Whit the value i declare a new account
            account ac = new account () { id_account = int.Parse(ddlAccount.SelectedValue) };
            // and associate it to Customer
            cus.account = ac;
    

    希望能帮上忙

        3
  •  0
  •   MAbraham1    15 年前

    我已经成功地从链接类中检索了一些属性,如下所示:

    First Name:
    <asp:TextBox ID="FirstNameTextBox" runat="server"
     Text='<%# Bind("Entitycontact.Namefirst") %>' />