共用方式為


How to: Add an Updater Method

You can enable users to update business data in a SharePoint external list by creating an Updater method. For more information, see Designing a Business Data Connectivity Model.

To create an Updater method

  1. On the BDC designer, choose an entity.

  2. On the menu bar, choose View, Other Windows, BDC Method Details.

    The BDC Method Details window opens. For more information about this window, see BDC Model Design Tools Overview.

  3. In the Add a Method list, choose Create Updater Method.

    Visual Studio adds the following elements to the model. These elements appear in the BDC Method Details window.

    • A method that's named Update.

    • An input parameter for the method.

    • A type descriptor for the parameter. By default, Visual Studio uses the entity type descriptor that you defined for the Finder method (for example: Contact).

    • A method instance for the method.

    For more information, see Designing a Business Data Connectivity Model.

    Note

    If the identifier of the entity type represents a field in a database table that's not automatically generated, set the Pre-Updater Field property to True.

  4. In Solution Explorer, open the shortcut menu of the service code file that was generated for the entity, and then choose View Code.

    The entity service code file opens in the Code Editor. For more information about that file, see Creating a Business Data Connectivity Model.

  5. Add code to the Update method to update data. The following example updates information for a contact in the AdventureWorks sample database for SQL Server.

    Note

    Replace the value of the ServerName field with the name of your server.

    Public Shared Sub Update(ByVal contact As Contact)
        Const ServerName As String = "MySQLServerName" 
        Dim dataContext As AdventureWorksDataContext = _
            New AdventureWorksDataContext("Data Source=" & ServerName & _
                ";Initial Catalog=AdventureWorks;Integrated Security=True")
    
        Dim ContactToUpdate As Contact = (From Contacts In dataContext.Contacts.AsEnumerable().Take(20) _
             Where Contacts.ContactID = contact.ContactID _
             Select Contacts).Single()
    
        With ContactToUpdate
            .FirstName = contact.FirstName
            .LastName = contact.LastName
            .EmailAddress = contact.EmailAddress
            .Phone = contact.Phone
            .EmailPromotion = contact.EmailPromotion
            .NameStyle = contact.NameStyle
            .PasswordHash = contact.PasswordHash
            .PasswordSalt = contact.PasswordSalt
        End With
    
        dataContext.SubmitChanges()
    
    End Sub
    
    public static void Update(Contact contact)
    {
        const string ServerName = "MySQLServerName";
        AdventureWorksDataContext dataContext = new AdventureWorksDataContext
              ("Data Source=" + ServerName + ";" +
               "Initial Catalog=AdventureWorks;Integrated Security=True");
    
        var contactToUpdate = (from contacts in dataContext.Contacts
                                where contacts.ContactID == contact.ContactID
                                select contacts).Single();
    
        contactToUpdate.FirstName = contact.FirstName;
        contactToUpdate.LastName = contact.LastName;
        contactToUpdate.EmailAddress = contact.EmailAddress;
        contactToUpdate.Phone = contact.Phone;
        contactToUpdate.EmailPromotion = contact.EmailPromotion;
        contactToUpdate.NameStyle = contact.NameStyle;
        contactToUpdate.PasswordHash = contact.PasswordHash;
        contactToUpdate.PasswordSalt = contact.PasswordSalt;
        contactToUpdate.ModifiedDate = DateTime.Now;
        contactToUpdate.rowguid = Guid.NewGuid();
        dataContext.SubmitChanges();
    
    }
    

See Also

Tasks

How to: Add a Finder Method

How to: Add a Specific Finder Method

How to: Add a Creator Method

How to: Add an Updater Method

How to: Add a Deleter Method

How to: Add a Parameter to a Method

How to: Define a Method Instance

Concepts

BDC Model Design Tools Overview

Other Resources

Designing a Business Data Connectivity Model