LinqDataSource.Insert(IDictionary) Methode

Definition

Führt einen Einfügevorgang aus.

public:
 int Insert(System::Collections::IDictionary ^ values);
public int Insert(System.Collections.IDictionary values);
member this.Insert : System.Collections.IDictionary -> int
Public Function Insert (values As IDictionary) As Integer

Parameter

values
IDictionary

Die Zeilenwerte, die in die Datenquelle eingefügt werden sollen.

Gibt zurück

Die Anzahl der Zeilen, die von dem Einfügevorgang betroffen sind.

Beispiele

Das folgende Beispiel zeigt, wie Sie programmgesteuert einen neuen Datensatz in die Datenquelle einfügen, nachdem der Benutzer auf eine Schaltfläche geklickt hat. Der Code übergibt ein ListDictionary Objekt, das Standardwerte an die Insert Methode enthält.

protected void Add_Click(object sender, EventArgs e)
{
    System.Collections.Specialized.ListDictionary listDictionary
        = new System.Collections.Specialized.ListDictionary();
    listDictionary.Add("ProductName", TextBox1.Text);
    listDictionary.Add("ProductCategory", "General");
    listDictionary.Add("Color", "Not assigned");
    listDictionary.Add("ListPrice", null);
    LinqDataSource1.Insert(listDictionary);

    TextBox1.Text = String.Empty;
    DetailsView1.DataBind();
}
Protected Sub Add_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim listDictionary As New System.Collections.Specialized.ListDictionary()
    listDictionary.Add("ProductName", TextBox1.Text)
    listDictionary.Add("ProductCategory", "General")
    listDictionary.Add("Color", "Not assigned")
    listDictionary.Add("ListPrice", Nothing)
    LinqDataSource1.Insert(listDictionary)

    TextBox1.Text = String.Empty
    DetailsView1.DataBind()
End Sub

Das folgende Beispiel zeigt das Markup für das vorherige Beispiel.

<asp:LinqDataSource 
  ContextTypeName="ExampleDataContext" 
  TableName="Products" 
  EnableInsert="true" 
  ID="LinqDataSource1" 
  runat="server">
</asp:LinqDataSource>
<asp:DetailsView 
  DataSourceID="LinqDataSource1" 
  AllowPaging="true" 
  ID="DetailsView1" 
  runat="server">
</asp:DetailsView>
New product name:<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:button ID="Button1" 
  Text="Add new product with default values" 
  runat="server" 
  onclick="Add_Click" />
<asp:LinqDataSource 
  ContextTypeName="ExampleDataContext" 
  TableName="Products" 
  EnableInsert="true" 
  ID="LinqDataSource1" 
  runat="server">
</asp:LinqDataSource>
<asp:DetailsView 
  DataSourceID="LinqDataSource1" 
  AllowPaging="true" 
  ID="DetailsView1" 
  runat="server">
</asp:DetailsView>
New product name:<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:button ID="Button1" 
  Text="Add new product with default values" 
  runat="server" 
  onclick="Add_Click" />

Hinweise

In der Regel müssen Sie die Insert Methode nicht aus Ihrem Code aufrufen. Das datengebundene Steuerelement ruft die Insert Methode automatisch auf, wenn der Benutzer Maßnahmen zum Einfügen eines neuen Datensatzes ausführt. Sie rufen die Insert Methode explizit auf, wenn Sie einen eigenen Prozess zum Einfügen von Daten erstellen möchten.

Gilt für: