Sideway
output.to from Sideway
Draft for Information Only

Content

System.Data Namespace Component
DataTableCollection Class
 Definition
  In this article
 Examples
 Remarks
 Properties
 Methods
 Events
 Extension Methods
 Applies to
   .NET Core
   .NET Framework
   .NET Standard
   Xamarin.Android
   Xamarin.iOS
   Xamarin.Mac
 Thread Safety
 See also
 Examples
 Source/Reference

System.Data Namespace Component

The System.Data namespace provides access to classes that represent the ADO.NET architecture. ADO.NET lets you build components that efficiently manage data from multiple data sources.

DataTableCollection Class

Definition

Namespace:
System.Data
Assemblies:
System.Data.dll, netstandard.dll, System.Data.Common.dll

Represents the collection of tables for the DataSet.

In this article

  1. Definition
  2. Examples
  3. Remarks
  4. Properties
  5. Methods
  6. Events
  7. Extension Methods
  8. Applies to
  9. Thread Safety
  10. See also
C#
[System.ComponentModel.ListBindable(false)]
[System.Serializable]
public sealed class DataTableCollection : System.Data.InternalDataCollectionBase
Inheritance
Object InternalDataCollectionBase DataTableCollection
Attributes
ListBindableAttribute SerializableAttribute

Examples

The first procedure in this example retrieves the DataTableCollection of a DataSet and prints the value of each column, in each row, of each table. The second procedure creates a new DataTable with two columns, and adds it to the DataTableCollection.

C#
private void GetTables(DataSet dataSet)
{
    // Get Each DataTable in the DataTableCollection and 
    // print each row value.
    foreach (DataTable table in dataSet.Tables)
        foreach (DataRow row in table.Rows)
            foreach (DataColumn column in table.Columns)
                if (row[column] != null)
                    Console.WriteLine(row[column]);
}

private void CreateTable(DataSet dataSet)
{
    DataTable newTable = new DataTable("table");
    newTable.Columns.Add("ID", typeof(int));
    newTable.Columns.Add("Name", typeof(string));
    dataSet.Tables.Add(newTable);
}

Remarks

The DataTableCollection contains all the DataTable objects for a particular DataSet. To access the DataTableCollection of a DataSet, use the Tables property.

The DataTableCollection uses methods such as Add, Clear, and Remove to manage the items in the collection.

Use the Contains method to determine whether a particular table (specified by either index or name) is in the collection.

To navigate from one table to another, use the ChildRelations or ParentRelations property of the DataTable to access its collection of DataRelation objects. You can also use the Relations property to navigate through the parent/child relationships of the DataTables in a particular DataSet collection.

Properties

Count

Gets the total number of elements in a collection.

(Inherited from InternalDataCollectionBase)
IsReadOnly

Gets a value that indicates whether the InternalDataCollectionBase is read-only.

(Inherited from InternalDataCollectionBase)
IsSynchronized

Gets a value that indicates whether the InternalDataCollectionBase is synchronized.

(Inherited from InternalDataCollectionBase)
Item[Int32]

Gets the DataTable object at the specified index.

Item[String, String]

Gets the DataTable object with the specified name in the specified namespace.

Item[String]

Gets the DataTable object with the specified name.

List

Gets the items of the collection as a list.

(Inherited from InternalDataCollectionBase)
SyncRoot

Gets an object that can be used to synchronize the collection.

(Inherited from InternalDataCollectionBase)

Methods

Add()

Creates a new DataTable object by using a default name and adds it to the collection.

Add(DataTable)

Adds the specified DataTable to the collection.

Add(String)

Creates a DataTable object by using the specified name and adds it to the collection.

Add(String, String)

Creates a DataTable object by using the specified name and adds it to the collection.

AddRange(DataTable[])

Copies the elements of the specified DataTable array to the end of the collection.

CanRemove(DataTable)

Verifies whether the specified DataTable object can be removed from the collection.

Clear()

Clears the collection of all DataTable objects.

Contains(String)

Gets a value that indicates whether a DataTable object with the specified name exists in the collection.

Contains(String, String)

Gets a value that indicates whether a DataTable object with the specified name and table namespace exists in the collection.

CopyTo(Array, Int32)

Copies all the elements of the current InternalDataCollectionBase to a one-dimensional Array, starting at the specified InternalDataCollectionBase index.

(Inherited from InternalDataCollectionBase)
CopyTo(DataTable[], Int32)

Copies all the elements of the current DataTableCollection to a one-dimensional Array, starting at the specified destination array index.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator()

Gets an IEnumerator for the collection.

(Inherited from InternalDataCollectionBase)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
IndexOf(DataTable)

Gets the index of the specified DataTable object.

IndexOf(String)

Gets the index in the collection of the DataTable object with the specified name.

IndexOf(String, String)

Gets the index in the collection of the specified DataTable object.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(DataTable)

Removes the specified DataTable object from the collection.

Remove(String)

Removes the DataTable object with the specified name from the collection.

Remove(String, String)

Removes the DataTable object with the specified name from the collection.

RemoveAt(Int32)

Removes the DataTable object at the specified index from the collection.

ToString()

Returns a string that represents the current object.

(Inherited from Object)

Events

CollectionChanged

Occurs after the DataTableCollection is changed because of DataTable objects being added or removed.

CollectionChanging

Occurs while the DataTableCollection is changing because of DataTable objects being added or removed.

Extension Methods

Cast<TResult>(IEnumerable)

Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

Enables parallelization of a query.

AsQueryable(IEnumerable)

Converts an IEnumerable to an IQueryable.

Applies to

.NET Core

3.0 Preview 8 2.2 2.1 2.0

.NET Framework

4.8 4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6 4.5.2 4.5.1 4.5 4.0 3.5 3.0 2.0 1.1

.NET Standard

2.1 Preview 2.0

Xamarin.Android

7.1

Xamarin.iOS

10.8

Xamarin.Mac

3.0

Thread Safety

This type is safe for multithreaded read operations. You must synchronize any write operations.

See also

Examples

Examples of DataTableCollection Class
ASP.NET Code Input:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
       <title>Sample Page</title>
       <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
       <script runat="server" >
           Sub Page_Load()
               Dim xstr As String
               Dim xconn As New System.Data.OleDb.OleDbConnection
               xconn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=T:\test.mdb;User Id=admin;Password=;"
               xconn.Open()
               xstr = xstr + "Connection xconn to database test.mdb  is opened successfully.<br />"
               Dim xdata, ydata, zdata As New System.Data.DataSet
               Dim xadapt, yadapt As System.Data.OleDb.OleDbDataAdapter
               Dim sql As String
               sql = "SELECT * FROM T1"
               xadapt = New System.Data.OleDb.OleDbDataAdapter(sql, xconn)
               xstr = xstr + "Dataadapter xadapt is assigned to SELECT * FROM T1 through xconn successfully.<br />"
               sql = "SELECT * FROM T2"
               yadapt = New System.Data.OleDb.OleDbDataAdapter()
               Dim xcomm As System.Data.OleDb.OleDbCommand = new System.Data.OleDb.OleDbCommand()
               xcomm.Connection = xconn
               xcomm.CommandText = "Select * from T2"
               yadapt.SelectCommand = xcomm
               xstr = xstr + "Dataadapter yadapt is assigned to SELECT * FROM T2 through xconn successfully.<br />"
               xstr = xstr + "<br />"
               xadapt.Fill(zdata,"T1")
               xstr = xstr + "Dataset zdata is filled with dataadapter xadapt successfully.<br />"
               Dim ztables As System.Data.DataTableCollection = Zdata.Tables
               xstr = xstr + "zdata.Tables is assigned to DataTableCollection ztables successfully.<br />"
               xstr = xstr + "xtables.Count:" + ztables.Count.ToString + "<br />"
               yadapt.Fill(zdata,"T2")
               xstr = xstr + "Dataset zdata is filled with dataadapter yadapt successfully.<br />"
               xstr = xstr + "xtables.Count:" + ztables.Count.ToString + "<br />"
               xstr = xstr + "xtables.Item(0):" + ztables.Item(0).ToString + "<br />"
               xstr = xstr + "xtables.Item(T2):" + ztables.Item("T2").ToString + "<br />"
               xstr = xstr + "xtables.Add(T3):" + ztables.Add("T3").ToString + "<br />"
               xstr = xstr + "xtables.IndexOf(T3):" + ztables.IndexOf("T3").ToString + "<br />"
               xstr = xstr + "<br />"
               xadapt.Dispose()
               yadapt.Dispose()
               xstr = xstr + "Dataadapter xadapt, and yadapt are disposed successfully.<br />"
               xconn.Close()
               xstr = xstr + "Connection xconn is closed successfully.<br />"
               xstr = xstr + "Dataset zdata.tables(T1).rows(0).item(1):" + zdata.Tables("T1").Rows(0).Item("f1") + "<br />"
               xstr = xstr + "Dataset zdata.tables(T2).rows(0).item(1):" + zdata.Tables(1).Rows(0).Item(1) + "<br />"
               xdata.Dispose()
               ydata.Dispose()
               zdata.Dispose()
               xstr = xstr + "Dataset xdata, ydata, and zdata are disposed successfully.<br />"
               
               lbl01.Text = xstr
           End Sub
       </script>
    </head>
    <body>
<%Response.Write("<p>Results on "& Request.ServerVariables("SERVER_SOFTWARE") & " .net: " & System.Environment.Version.ToString & " " & ScriptEngine & " Version " & ScriptEngineMajorVersion & "." & ScriptEngineMinorVersion & "</p>")%>
       <% Response.Write ("<h1>This is a Sample Page of DataTableCollection Class</h1>") %>
       <p>
           <%-- Set on Page_Load --%>
           <asp:Label id="lbl01" runat="server" />
       </p>
    </body>
</html>
HTML Web Page Embedded Output:

Source/Reference


©sideway

ID: 201100006 Last Updated: 6/11/2020 Revision: 0 Ref:

close

References

  1. Active Server Pages,  , http://msdn.microsoft.com/en-us/library/aa286483.aspx
  2. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929%28v=vs.90%29.aspx
  3. ASP Best Practices,  , http://technet.microsoft.com/en-us/library/cc939157.aspx
  4. ASP Built-in Objects,  , http://msdn.microsoft.com/en-us/library/ie/ms524716(v=vs.90).aspx
  5. Response Object,  , http://msdn.microsoft.com/en-us/library/ms525405(v=vs.90).aspx
  6. Request Object,  , http://msdn.microsoft.com/en-us/library/ms524948(v=vs.90).aspx
  7. Server Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525541(v=vs.90).aspx
  8. Application Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525360(v=vs.90).aspx
  9. Session Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms524319(8v=vs.90).aspx
  10. ASPError Object,  , http://msdn.microsoft.com/en-us/library/ms524942(v=vs.90).aspx
  11. ObjectContext Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525667(v=vs.90).aspx
  12. Debugging Global.asa Files,  , http://msdn.microsoft.com/en-us/library/aa291249(v=vs.71).aspx
  13. How to: Debug Global.asa files,  , http://msdn.microsoft.com/en-us/library/ms241868(v=vs.80).aspx
  14. Calling COM Components from ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524620(v=VS.90).aspx
  15. IIS ASP Scripting Reference,  , http://msdn.microsoft.com/en-us/library/ms524664(v=vs.90).aspx
  16. ASP Keywords,  , http://msdn.microsoft.com/en-us/library/ms524672(v=vs.90).aspx
  17. Creating Simple ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524741(v=vs.90).aspx
  18. Including Files in ASP Applications,  , http://msdn.microsoft.com/en-us/library/ms524876(v=vs.90).aspx
  19. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929(v=vs.90).aspx
  20. FileSystemObject Object,  , http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx
  21. http://msdn.microsoft.com/en-us/library/windows/desktop/ms675944(v=vs.85).aspx,  , ADO Object Model
  22. ADO Fundamentals,  , http://msdn.microsoft.com/en-us/library/windows/desktop/ms680928(v=vs.85).aspx
close

Latest Updated LinksValid XHTML 1.0 Transitional Valid CSS!Nu Html Checker Firefox53 Chromena IExplorerna
IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 337

Reference 67

Computer

Hardware 151

Software

Application 198

Digitization 25

Latex 9

Manim 122

Numeric 19

Programming

Web 285

Unicode 494

HTML 65

CSS 59

ASP.NET 194

OS 391

DeskTop 7

Python 23

Knowledge

Mathematics

Formulas 8

Algebra 29

Number Theory 206

Trigonometry 18

Geometry 21

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Physics

Electric 27

Biology 1

Geography 1


Copyright © 2000-2020 Sideway . All rights reserved Disclaimers last modified on 06 September 2019