开发者

How can I select and iterate through multiple rows from the database in C#?

开发者 https://www.devze.com 2023-01-08 16:24 出处:网络
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:

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)
}
0

精彩评论

暂无评论...
验证码 换一张
取 消