Search Tables in a Dataset Sql Server

In certain scenarios, it may be necessary to determine the number of tables present within a DataSet object. The DataSet in ADO.NET provides a DataTableCollection that holds zero or more DataTable objects, each representing a distinct table of data.

Number of tables contained within the DataSet

To determine the number of tables contained within the DataSet, we can utilize the Tables property of the DataSet object. The Tables property returns a DataTableCollection, and we can access the Count property of this collection to retrieve the total count of tables.

// Assuming we have a populated DataSet object named "dataset" int tableCount = dataset.Tables.Count; Console.WriteLine("Number of tables in the DataSet: " + tableCount);

By accessing the Count property of the Tables collection, we can obtain the number of DataTable objects present within the DataSet. This count represents the number of tables that have been populated with data using a SqlDataAdapter or any other means.

Full Source VB.NET
Imports System.Data.SqlClient Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim connetionString As String Dim connection As SqlConnection Dim command As SqlCommand Dim adapter As New SqlDataAdapter Dim ds As New DataSet Dim tables As DataTable Dim i As Integer Dim sql As String connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password" sql = "Your SQL Statement Here" connection = New SqlConnection(connetionString) Try connection.Open() command = New SqlCommand(sql, connection) adapter.SelectCommand = command adapter.Fill(ds, "SQL Temp Table") adapter.Dispose() command.Dispose() connection.Close() For Each tables In ds.Tables MsgBox(tables.TableName) Next Catch ex As Exception MsgBox("Can not open connection ! ") End Try End Sub End Class

Conclusion

It is worth mentioning that the Tables collection can also be iterated over using a foreach loop to access individual DataTable objects if further manipulation or analysis of each table is required. The following VB.NET source code shows how to find the tables inside the Dataset.