Functional ADO.NET
- Posted: Sep 02, 2007 at 9:41 AM
- 5,264 Views
- 2 Comments
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Right click “Save as…”
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?
Instead of:
reader =>
{
while (reader.Read())
{
Console.WriteLine("{0}\t{1}\t{2}",
reader.GetInt32(0),
reader.GetString(1),
reader.GetString(2));
}
});
I sugest to abstract away the reader usage and put it inside the DataBase.ExecuteReader(...) method and replace the last parameter Action<IDataReader> action with Action<IDataRecord> and iterate inside the method, like so :
).
replace :
...
using (IDataReader reader = cmd.ExecuteReader())
{
action(reader);
}
...
with :
using (IDataReader reader = cmd.ExecuteReader())
{
while (reader.Read()) {
action(reader);
}
}
so:
reader =>
{
while (reader.Read())
{
Console.WriteLine("{0}\t{1}\t{2}",
reader.GetInt32(0),
reader.GetString(1),
reader.GetString(2));
}
});
becomes:
record =>
{
Console.WriteLine("{0}\t{1}\t{2}",
record .GetInt32(0),
record .GetString(1),
record .GetString(2));
});
... and all loops are gone inside user code. (perfect candidate for thread level parallelism now
Other than that nice vid and good way of using the old ado.net.
A good approach would be to have an overloaded method. One that passes the reader, another that passes each data record.
I hope people find this pattern useful and adapt it to meet their needs as you have done
Also, does anyone know if this pattern has a name?
Remove this comment
Remove this thread
close