Redigera

Dela via


LinqDataSource.TableName Property

Definition

Gets or sets the name of the property or field in the data context class that represents a data collection.

public:
 property System::String ^ TableName { System::String ^ get(); void set(System::String ^ value); };
public string TableName { get; set; }
member this.TableName : string with get, set
Public Property TableName As String

Property Value

A string that contains the name of the property that contains the data collection.

Examples

The following example shows two LinqDataSource controls. In one LinqDataSource control, the TableName property is set to a property in a class. That property returns an array of strings. In the other LinqDataSource control, the TableName property is set to a property that represents a database table.

<!-- Retrieve and display data from array of string values -->
<asp:LinqDataSource 
    ContextTypeName="MovieLibrary" 
    TableName="AvailableGenres" 
    ID="LinqDataSource1" 
    runat="server">
</asp:LinqDataSource>
<asp:DropDownList 
    DataSourceID="LinqDataSource1"
    runat="server" 
    ID="DropDownList1">
</asp:DropDownList>

<!-- Retrieve and display data from database -->
<asp:LinqDataSource 
    ContextTypeName="ExampleDataContext" 
    TableName="Movies" 
    Select="Title"
    ID="LinqDataSource2" 
    runat="server">
</asp:LinqDataSource>
<asp:DropDownList 
    DataSourceID="LinqDataSource2"
    runat="server" 
    ID="DropDownList2">
</asp:DropDownList>
<!-- Retrieve and display data from array of string values -->
<asp:LinqDataSource 
    ContextTypeName="MovieLibrary" 
    TableName="AvailableGenres" 
    ID="LinqDataSource1" 
    runat="server">
</asp:LinqDataSource>
<asp:DropDownList 
    DataSourceID="LinqDataSource1"
    runat="server" 
    ID="DropDownList1">
</asp:DropDownList>

<!-- Retrieve and display data from database -->
<asp:LinqDataSource 
    ContextTypeName="ExampleDataContext" 
    TableName="Movies" 
    Select="Title"
    ID="LinqDataSource2" 
    runat="server">
</asp:LinqDataSource>
<asp:DropDownList 
    DataSourceID="LinqDataSource2"
    runat="server" 
    ID="DropDownList2">
</asp:DropDownList>

The class named ExampleDataContext that represents the database table is not shown in this example. For this example to work, you must create this class by adding a LINQ to SQL class named Example.dbml and dragging a table named Movie onto the O/R Designer. A class named ExampleDataContext with a property named Movies is generated.

The following example shows the class named MovieLibrary that is referenced in the LinqDataSource control.

public class MovieLibrary
{
    string[] _availableGenres = { "Comedy", "Drama", "Romance" };

    public MovieLibrary()
    {
    }

    public string[] AvailableGenres
    {
        get
        {
            return _availableGenres;
        }
    }
}
Public Class MovieLibrary
    Dim _availableGenres() As String = {"Comedy", "Drama", "Romance"}

    Public ReadOnly Property AvailableGenres() As String()
        Get
            Return _availableGenres
        End Get
    End Property
    
End Class

Remarks

Set the TableName property to the name of the property or field that represents the data that you want to retrieve. When you are connecting to a database table, the name of the property is usually the same as the name of the table. When you are connecting to an in-memory data collection, set the TableName property to the name of the property or field that returns the data collection.

You can assign a property that returns any type to the TableName property for retrieval operations. If the object that is represented by the property does not implement IEnumerable, the LinqDataSource control will automatically wrap the object in an instance of an IEnumerable object.

To enable automatic update, insert, or delete operations, the class that is assigned to the ContextTypeName property must derive from DataContext and the property that is assigned to the TableName property must derive from Table<TEntity>.

For information about how to select data from an instance of a class, see the Result property.

Applies to