如何在C#中从数据库中选择并迭代多行?

时间:2010-07-20 21:41:36

标签: c# asp.net sql-server

我想从我的SQL Server数据库中选择许多行,并以某种方式将它们组合在一起。目前,我一直在使用以下方法来获取这些行:

SqlDataSource mySource = new SqlDataSource("ConnectionString","SelectStatement");
IEnumerable myEnum = mySource.Select(DataSourceSelectArguments.Empty);
IEnumerator myCount = myEnum.GetEnumerator();
while(myCount.MoveNext()) //Iterate through each row
{
    DataRowView myView = (DataRowView)myCount.Current; //This is the current row
    //Do something with this row
}

我觉得必须有更好的方法来做到这一点。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

为什么不使用foreach呢? foreach旨在处理可枚举类型 类似的东西:

foreach (var currentView in mySource.Select(DataSourceSelectArguments.Empty))
{
     // Do something with currentView (may need to give it a type if you are interested in it)
}