LinqDataSource.Insert(IDictionary) Metod

Definition

Utför en infogningsåtgärd.

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

Parametrar

values
IDictionary

Radvärdena som ska infogas i datakällan.

Returer

Antalet rader som påverkas av infogningsåtgärden.

Exempel

I följande exempel visas hur du programmatiskt infogar en ny post i datakällan när användaren klickar på en knapp. Koden skickar ett ListDictionary objekt som innehåller standardvärden till Insert metoden.

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

I följande exempel visas pålägget för föregående exempel.

<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" />

Kommentarer

Vanligtvis behöver du inte anropa Insert metoden från koden. Den databundna kontrollen anropar Insert automatiskt metoden när användaren vidtar åtgärder för att infoga en ny post. Du anropar uttryckligen Insert metoden när du vill skapa en egen process för att infoga data.

Gäller för