SHARE
TWEET

Untitled

a guest Feb 19th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. List<MyData> myData = new List<MyData();
  2. FetchData((IDataReader reader) =>
  3.    {
  4.        myData.Add(new MyData(reader.GetString(0), reader.GetInt32(1)));
  5.    }, "usp_get_my_data");
  6.    
  7. protected void FetchData(Action<IDataReader> processor, String query)
  8. {
  9.     using (var connection = CreateConnection())
  10.     {
  11.         connection.ConnectionString = ConnectionString;
  12.         connection.Open();
  13.  
  14.         using (var command = connection.CreateCommand())
  15.         {
  16.             command.CommandType = CommandType.StoredProcedure;
  17.             command.CommandText = query;
  18.  
  19.             using (IDataReader reader = command.ExecuteReader())
  20.             {
  21.                 while (reader.read())
  22.                 {
  23.                     processor(reader);
  24.                 }
  25.             }
  26.         }
  27.     }
  28. }
  29.    
  30. protected IEnumerable<T> FetchData<T>(Func<IDataReader, T> processor, String query)
  31. {
  32.     using (var connection = CreateConnection())
  33.     {
  34.         connection.ConnectionString = ConnectionString;
  35.         connection.Open();
  36.  
  37.         using (var command = connection.CreateCommand())
  38.         {
  39.             command.CommandType = CommandType.StoredProcedure;
  40.             command.CommandText = query;
  41.  
  42.             using (IDataReader reader = command.ExecuteReader())
  43.             {
  44.                 while (reader.read())
  45.                 {
  46.                     yield return processor(reader);
  47.                 }
  48.             }
  49.         }
  50.     }
  51. }
  52.    
  53. var myData = FetchData<MyData>(reader => new MyData(reader.GetString(0), reader.GetInt32(1)), "usp_get_my_data").ToList();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top