asp.net sql datareader loop by columns
Use the FieldCount
property of the SqlDataReader
:
while (vysledky.Read()){ // Iterate over each of the fields (columns) in the datareader's current record for (int i = 0; i < vysledky.FieldCount; i++) { var value = vysledky[i]; TableCell cell = new TableCell(); cell.Text = Convert.ToString(value); row.Controls.Add(cell); }}
You should just do it through a SqlDataAdapter
:
SqlConnection Conn = new SqlConnection(YourConnectionString);SqlCommand YourSqlCommand = new SqlCommand();YourSqlCommand.Connection = Conn;YourSqlCommand.CommandText = "select * from yourtable";DataTable dt = new DataTable();SqlDataAdapter sda = new SqlDataAdapter(YourSqlCommand);sda.Fill(dt);
At the point, your DataTable
(dt
) contains all of the data from your query.