Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- List<MyData> myData = new List<MyData();
- FetchData((IDataReader reader) =>
- {
- myData.Add(new MyData(reader.GetString(0), reader.GetInt32(1)));
- }, "usp_get_my_data");
- protected void FetchData(Action<IDataReader> processor, String query)
- {
- using (var connection = CreateConnection())
- {
- connection.ConnectionString = ConnectionString;
- connection.Open();
- using (var command = connection.CreateCommand())
- {
- command.CommandType = CommandType.StoredProcedure;
- command.CommandText = query;
- using (IDataReader reader = command.ExecuteReader())
- {
- while (reader.read())
- {
- processor(reader);
- }
- }
- }
- }
- }
- protected IEnumerable<T> FetchData<T>(Func<IDataReader, T> processor, String query)
- {
- using (var connection = CreateConnection())
- {
- connection.ConnectionString = ConnectionString;
- connection.Open();
- using (var command = connection.CreateCommand())
- {
- command.CommandType = CommandType.StoredProcedure;
- command.CommandText = query;
- using (IDataReader reader = command.ExecuteReader())
- {
- while (reader.read())
- {
- yield return processor(reader);
- }
- }
- }
- }
- }
- var myData = FetchData<MyData>(reader => new MyData(reader.GetString(0), reader.GetInt32(1)), "usp_get_my_data").ToList();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement