
Simple function to get all columns from a sql table.
Note: You should prepare the sql statement and check it for injections...
Public Shared Function GetColumnList(ByVal strCon As String, ByVal aDatasetName As String, ByVal aTablename As String) As List(Of String) Dim lstResult As New List(Of String) Using con As SqlConnection = New SqlConnection(strCon) Try con.Open() Dim ds As New DataSet(aDatasetName) Dim da As SqlDataAdapter = New SqlDataAdapter("SELECT Top 1 FROM " & aTablename, con) da.Fill(ds, aTablename) For Each dt As DataTable In ds.Tables For Each dc As DataColumn In dt.Columns lstResult.Add(dc.Caption) Next Next Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Critical) Return Nothing End Try End Using Return lstResult End Function
0 Antworten zu Get all columns from a sql table