Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public IEnumerable<object> ExecuteSelect(string commandText)
- {
- using (IDbConnection connection = CreateConnection())
- {
- using (IDbCommand cmd = CreateCommand(commandText, connection))
- {
- connection.Open();
- using (IDbDataReader reader = cmd.ExecuteReader())
- {
- while(reader.Read())
- {
- yield return reader["SomeField"];
- }
- }
- connection.Close();
- }
- }
- }
- foreach(object obj in ExecuteSelect(commandText))
- {
- break;
- }
- class Program
- {
- static void Main(string[] args)
- {
- foreach (int v in getValues())
- {
- Console.WriteLine(v);
- }
- Console.ReadKey();
- foreach (int v in getValues())
- {
- Console.WriteLine(v);
- break;
- }
- Console.ReadKey();
- }
- public static IEnumerable<int> getValues()
- {
- using (TestDisposable t = new TestDisposable())
- {
- for(int i = 0; i<10; i++)
- yield return t.GetValue();
- }
- }
- }
- public class TestDisposable : IDisposable
- {
- private int value;
- public void Dispose()
- {
- Console.WriteLine("Disposed");
- }
- public int GetValue()
- {
- value += 1;
- return value;
- }
- }
Add Comment
Please, Sign In to add comment