This is an awful design. Are you able to change it?
If so then consider having the table 'WhatToSelect' having 2 columns and many rows.
FieldName Select
LearnerName Yes
LearnerDOB No ... etc.
If you are not able to change it then you will have to load the entire row and examine the columns in your VB code one by one
[EDIT]
To demonstrate what I mean, this is what you have to go through if you leave your table design as it is
Dim results As StringBuilder = New StringBuilder("SELECT ")
Dim fieldFound As Boolean = False
Using myConn = New SqlConnection(ConnectionString)
Using myCmd = myConn.CreateCommand()
myCmd.CommandText = "SELECT * FROM WhatToSelect"
myConn.Open()
Using myReader = myCmd.ExecuteReader()
If myReader.Read() Then
For i As Integer = 0 To myReader.FieldCount - 1
If myReader.GetBoolean(i) Then
results.Append(myReader.GetName(i))
results.Append(",")
fieldFound = True
End If
Next
End If
End Using
If fieldFound Then
results.Remove(results.Length - 1, 1)
results.Append(" FROM myTable")
End If
End Using
myConn.Close()
End Using
MessageBox.Show(results.ToString())
But if you change the table design to something like this
create table Better
(
FieldName varchar(125),
[Select] bit
)
insert into Better values
('LearnerName', 1),
('LearnerSurname',0),
('LearnerDOB',1)
You can use
Dim results1 As StringBuilder = New StringBuilder("SELECT ")
Using myConn = New SqlConnection(ConnectionString)
Using myCmd = myConn.CreateCommand()
myCmd.CommandText = "SELECT * FROM Better WHERE [Select] = 1"
myConn.Open()
Using myReader = myCmd.ExecuteReader()
Do While myReader.Read()
results1.Append(myReader.GetString(0))
results1.Append(",")
Loop
End Using
End Using
End Using
If results1.Length <> "SELECT ".Length Then
results1.Remove(results1.Length - 1, 1)
results1.Append(" FROM myTable")
End If
MessageBox.Show(results1.ToString())
Both versions produce the same results
"SELECT LearnerName,LearnerDOB FROM myTable"