但我不知道這樣使用是否仍有 ConnectionPool 的功能
所以改用 SqlDataSource,
但我又不想去 Bind 其他元件(我只是單純要讀出資料來用而已)
故決定直接使用 SqlDataSource.Select()
程式碼如下:
SqlDataSource1.SelectCommand = "SELECT * FROM mytable";
IEnumerator enumerator = SqlDataSource1.Select(new DataSourceSelectArguments()).GetEnumerator();
while (enumerator.MoveNext())
Response.Write("
" + enumerator.Current.GetType());
執行後輸出的結果是一堆 System.Data.Common.DataRecordInternal 物件
我試著去使用這個類別,
System.Data.Common.DataRecordInternal aRow = (System.Data.Common.DataRecordInternal)enumerator.Current;
但compile後是錯誤的,無法使用
上網查了一下,發現也有很多人遇到這個問題
最後的結果是: 要強制轉型為 System.Data.Common.DbDataRecord
於是最後的程式碼:
SqlDataSource1.SelectCommand = "SELECT * FROM mytable";
IEnumerator enumerator = SqlDataSource1.Select(new DataSourceSelectArguments()).GetEnumerator();
while (enumerator.MoveNext())
System.Data.Common.DbDataRecord dr = (System.Data.Common.DbDataRecord)enumerator.Current;
Response.Write("
" + dr.GetString(0));
}
這樣就可以正確取得資料囉~
沒有留言:
張貼留言