LinqDataSource.TableName 屬性

定義

取得或設定資料內容類別中屬性或欄位的名稱,這個類別表示資料集合。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

屬性值

String

包含屬性名稱的字串,該屬性包含資料集合。A string that contains the name of the property that contains the data collection.

範例

下列範例顯示兩個 LinqDataSource 控制項。The following example shows two LinqDataSource controls. 在一個 LinqDataSource 控制項中, TableName 屬性會設定為類別中的屬性。In one LinqDataSource control, the TableName property is set to a property in a class. 該屬性會傳回字串陣列。That property returns an array of strings. 在另 LinqDataSource 一個控制項中, TableName 屬性會設定為代表資料庫資料表的屬性。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>

ExampleDataContext這個範例不會顯示名為的類別,代表資料庫資料表。The class named ExampleDataContext that represents the database table is not shown in this example. 若要讓此範例能夠運作,您必須藉由新增名為 Example 的 LINQ to SQL 類別,並將名為 Movie 的資料表拖曳到 O/R 設計工具,來建立這個類別。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. 會產生名為且名為之屬性的類別 ExampleDataContext MoviesA class named ExampleDataContext with a property named Movies is generated.

下列範例 MovieLibrary 會顯示控制項中所參考名為的類別 LinqDataSourceThe 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

備註

將屬性(property)設定 TableName 為代表您要取得之資料的屬性或欄位的名稱。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. 當您連接到記憶體中的資料收集時,請將 TableName 屬性設定為傳回資料集合的屬性或欄位的名稱。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.

您可以指派屬性,將任何類型傳回至 TableName 屬性以進行抓取作業。You can assign a property that returns any type to the TableName property for retrieval operations. 如果由屬性代表的物件未執行 IEnumerableLinqDataSource 控制項會自動將物件包裝在物件的實例中 IEnumerableIf 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.

若要啟用自動更新、插入或刪除作業,指派給屬性的類別 ContextTypeName 必須衍生自, DataContext 而且指派給屬性的屬性 TableName 必須衍生自 Table<TEntity>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>.

如需如何從類別的實例選取資料的詳細資訊,請參閱 Result 屬性。For information about how to select data from an instance of a class, see the Result property.

適用於