0
查詢數據庫中的數據後 我使用datareader填充數組。如下所示。從數據庫打印多行
connection.Open();
System.Data.SqlClient.SqlDataReader reader = command.ExecuteReader();
reader.Read();
_firstname = reader[0].ToString();
_Year = reader[1].ToString();
_coursename = reader[2].ToString();
_credits = reader[3].ToString();
_mark = reader[4].ToString();
_firstname2 = reader[5].ToString();
reader.Close();
我 但結果顯示如下
1 - Sam Bons
2- 2012
3- DDD
4- 3
5- 80
我怎麼能執行一個循環,從DB獲得所有結果,並打印出來?
謝謝
我這樣做是因爲這樣謝謝安
result = string.Empty;
int counter = 1;
while (reader.Read())
{
_firstname = reader[0].ToString();
_Year = reader[1].ToString();
_coursename = reader[2].ToString();
_credits = reader[3].ToString();
_mark = reader[4].ToString();
result += string.Format("{5} - {0}{1}{2}{3}{4} </br> ",
_firstname,
_Year,
_coursename,
_credits,
_mark,
counter);
counter++;
}
Response.Write(result);
reader.Close();