Guest User

Untitled

a guest
Feb 22nd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. namespace SqlStuff
  2. {
  3. using System.Data.SqlClient;
  4. using System;
  5. using System.Collections.Generic;
  6.  
  7. public static class Class1
  8. {
  9. public static IEnumerable<IDictionary<String, Object>> ReadStuff(this SqlConnection connection)
  10. {
  11. string query = "SELECT * FROM dbo.test";
  12.  
  13. using (var command = new SqlCommand(query, connection))
  14. using (SqlDataReader reader = command.ExecuteReader())
  15. {
  16. while (reader.Read())
  17. {
  18. yield return ReadAsDictionary(reader);
  19. }
  20. }
  21. }
  22.  
  23. private static IDictionary<String, Object> ReadAsDictionary(SqlDataReader reader)
  24. {
  25. IDictionary<String, Object> dataRecord = new Dictionary<String, Object>();
  26.  
  27. for (var i = 0; i < reader.FieldCount; i++)
  28. {
  29. dataRecord.Add(reader.GetName(i), reader.GetValue(i));
  30. }
  31.  
  32. return dataRecord;
  33. }
  34. }
  35. }
Add Comment
Please, Sign In to add comment