Dataset Column Definition

The SqlDataAdapter object serves as a valuable tool for populating DataTables within a DataSet. By utilizing the Fill method of the SqlDataAdapter, we can seamlessly populate data in a Dataset, which can contain multiple tables simultaneously.

Within each table, the data is organized in the form of rows and columns. Each row represents a single entry of data, while each column represents a specific data attribute or field. To interact with the actual data within a table, we utilize the DataRow class. This class provides access to properties and methods that allow us to retrieve, evaluate, and manipulate the data within the table.

Definitions from a DataTable

In certain scenarios, there may be a need to retrieve the column headers or definitions from a DataTable. The DataTable class includes a ColumnsCollection object that holds these column definitions. This collection provides access to individual columns and allows us to gather information such as column names, data types, and other properties associated with each column.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> <html xmlns=""> <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); DataSet ds = new DataSet (); string sql = "select * from publishers"; try { connection.Open(); SqlDataAdapter adapter = new SqlDataAdapter(sql,connection ); adapter.Fill(ds); DataTable dt = ds.Tables[0]; foreach (DataColumn column in dt.Columns) { ListBox1.Items.Add(column.ColumnName); } 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 Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim connectionString As String Dim connection As SqlConnection Dim ds As New DataSet Dim dt As DataTable Dim column As DataColumn connectionString = ConfigurationManager.ConnectionStrings("SQLDbConnection").ToString connection = New SqlConnection(connectionString) Dim sql As String = "select * from publishers" Try connection.Open() Dim adapter As New SqlDataAdapter(sql, connection) adapter.Fill(ds) dt = ds.Tables(0) For Each column In dt.Columns ListBox1.Items.Add(column.ColumnName) Next connection.Close() Catch ex As Exception Label1.Text = "Error in execution " & ex.ToString End Try End Sub End Class

We can effectively retrieve and analyze the column definitions within a DataTable, enabling us to work with the table's structure and manipulate the data accordingly by employing the ColumnsCollection and its associated methods.


SqlDataAdapter object allows DataTable and DataSet instances to fill with data from the database, the Fill method is used to this. The collection of rows and columns within the DataSet in comprised of the DataRow class, which is the saved data in the table. The next step is to display the contents of the columns; we use ColumnsCollection object within DataTable which enables us to get individual columns and their properties. Through adopting these skills of ours, we can successfully pull and manipulate datatable's column definitions within an ASP.NET application in an efficient manner.