Net-informations.com
SiteMap  | About    

How to find column definition - Sql Server

Dataset represents a collection of data retrieved from the Data Source. The Dataset can work with the data it contains, without knowing the source of the data coming from. The SqlDataAdapter object allows us to populate DataTable in a DataSet. We can use Fill method in the SqlDataAdapter for populating data in a Dataset. The Dataset can contains more than one Table at a time. The data inside Table is in the form of Rows and Columns .

In some situations we have to find the column headers in a DataTable. There is a ColumnsCollection object in the Datatable hold the column Definitions.




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 dt As DataTable
        Dim column As DataColumn
        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()

            dt = ds.Tables(0)
            For Each column In dt.Columns
                MsgBox(column.ColumnName)
            Next

        Catch ex As Exception
            MsgBox("Can not open connection ! ")
        End Try
    End Sub
End Class

connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
sql = "Your SQL Statement Here"

You have to replace the string with your real time variables.







net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.