Find Tables in a Dataset

The DataTableCollection in ADO.NET is a collection that holds zero or more DataTable objects within a DataSet. It provides a way to organize and manage multiple tables within a single DataSet.

To populate DataTables within a DataSet, the SqlDataAdapter object is commonly used. The DataAdapter acts as a bridge between the data source and the DataSet, allowing for the building and filling of each DataTable with data retrieved from the data source.

Number of tables present in a DataSet object

In certain situations, there may be a need to determine the number of tables present in a DataSet object. This can be achieved by accessing the Tables property of the DataSet. The Tables property provides access to the DataTableCollection, and by retrieving the Count property of the DataTableCollection, we can obtain the number of tables present

Default.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> <br /> <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox> <br /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
Full Source | C#
using System; using System.Data ; using System.Data.SqlClient ; using System.Configuration; public partial class _Default : System.Web.UI.Page { protected void Button1_Click(object sender, EventArgs e) { string connectionString = ConfigurationManager.ConnectionStrings["SQLDbConnection"].ToString(); SqlConnection connection = new SqlConnection(connectionString); SqlCommand command; DataSet ds = new DataSet (); SqlDataAdapter adapter = new SqlDataAdapter(); string firstSql = "select * from publishers"; string secondSql = "select * from authors"; try { connection.Open(); command = new SqlCommand(firstSql, connection); adapter.SelectCommand = command; adapter.Fill(ds, "First Table"); adapter.SelectCommand.CommandText = secondSql; adapter.Fill(ds, "Second Table"); foreach (DataTable tables in ds.Tables) { ListBox1.Items.Add(tables.TableName); } connection.Close(); } catch (Exception ex) { Label1.Text = "Error in execution " + ex.ToString(); } } }
Full Source | VB.NET
Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Partial Class _Default Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim connectionString As String Dim connection As SqlConnection Dim command As SqlCommand Dim adapter As New SqlDataAdapter Dim tables As DataTable Dim ds As New DataSet connectionString = ConfigurationManager.ConnectionStrings("SQLDbConnection").ToString connection = New SqlConnection(connectionString) Dim firstSql As String = "select * from publishers" Dim secondSql As String = "select * from authors" Try connection.Open() command = New SqlCommand(firstSql, connection) adapter.SelectCommand = command adapter.Fill(ds, "First Table") adapter.SelectCommand.CommandText = secondSql adapter.Fill(ds, "Second Table") connection.Close() For Each tables In ds.Tables ListBox1.Items.Add(tables.TableName) Next Catch ex As Exception Label1.Text = "Error in execution " & ex.ToString End Try End Sub End Class

Conclusion

The DataTableCollection within a DataSet holds multiple DataTable objects. The SqlDataAdapter facilitates the population of DataTables within a DataSet. To determine the number of tables present in a DataSet, the Tables property of the DataSet is accessed, and the Count property of the DataTableCollection is retrieved. This count value provides the number of tables within the DataSet.