How can I select and iterate through multiple rows from the database in C#?
I want to select many rows from my SQL Server database and combine them in a certain manner. Currently, I've been using the following method to get these rows:
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.Cur开发者_如何学编程rent; //This is the current row
//Do something with this row
}
I feel like there must be a better way of doing this. Any suggestions?
Why don't you use foreach instead? foreach is meant to work on Enumerable types.
something like:
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)
}
精彩评论