Click here to Skip to main content
15,886,873 members
Home / Discussions / C#
   

C#

 
AnswerRe: How create table in SQL Server using C# and data from Excel. Pin
Maciej Los15-Mar-14 6:43
mveMaciej Los15-Mar-14 6:43 
GeneralRe: How create table in SQL Server using C# and data from Excel. Pin
OriginalGriff16-Mar-14 0:05
mveOriginalGriff16-Mar-14 0:05 
GeneralW:Re: How create table in SQL Server using C# and data from Excel. Pin
Maciej Los16-Mar-14 0:14
mveMaciej Los16-Mar-14 0:14 
GeneralRe: How create table in SQL Server using C# and data from Excel. Pin
jschell17-Mar-14 8:34
jschell17-Mar-14 8:34 
GeneralRe: How create table in SQL Server using C# and data from Excel. Pin
OriginalGriff17-Mar-14 9:03
mveOriginalGriff17-Mar-14 9:03 
GeneralRe: How create table in SQL Server using C# and data from Excel. Pin
jschell18-Mar-14 8:07
jschell18-Mar-14 8:07 
GeneralRe: How create table in SQL Server using C# and data from Excel. Pin
OriginalGriff18-Mar-14 9:08
mveOriginalGriff18-Mar-14 9:08 
QuestionIEnumerable and disposal Pin
Rob Philpott14-Mar-14 0:31
Rob Philpott14-Mar-14 0:31 
Good morning wise ones.

You've probably seen DB code akin to this before:
C#
public List<int> GetSomeNumbers()
{
    List<int> x = new List<int>();
    using (SqlConnection)
    using (SqlCommand)
    using (SqlDataReader)
        while (reader.Read()) x.Add(reader[0]);
    return x;
}

Which would populate and return a list from the first column of the query. I've left the detail out, and yes a cast would be required. Another possibly better approach would be this:
C#
public IEnumerable<int> GetSomeNumbers()
{
    List<int> x = new List<int>();
    using (SqlConnection)
    using (SqlCommand)
    using (SqlDataReader)
        while (reader.Read()) yield return reader[0];
    return x;
}

The reason it might be better is because it immediately returns on each read so that if I do something like send the result straight over a network I can do this is a 'streaming' approach which would be faster and not require as much memory. The reason it might not be as good is that IEnumerable is inherently more confusing. The call doesn't even get invoked until something tries to iterate it and that can catch us all out.

I'm starting to adopt the latter approach more and more, but questions arise because of the more complicated execution flow. In particular, what happens if I do this:
C#
foreach (int x in GetSomeNumbers())
{
    if (x == 0) break;
}

This is the bit I'm unsure about. Assume that a zero arrives a long time before then end of the result set. IEnumerables tell us when we're at the end of can give us the next item, but what happens if the consumer bails out before the end? There's no way (that I know of) to tell the iterator function we've concluded. Where does this leave the disposal of the reader, command and connection?

Be happy, its Friday!
Regards,
Rob Philpott.

AnswerRe: IEnumerable and disposal Pin
Richard Deeming14-Mar-14 2:06
mveRichard Deeming14-Mar-14 2:06 
GeneralRe: IEnumerable and disposal Pin
Rob Philpott14-Mar-14 2:27
Rob Philpott14-Mar-14 2:27 
GeneralRe: IEnumerable and disposal Pin
Simon_Whale14-Mar-14 2:30
Simon_Whale14-Mar-14 2:30 
GeneralRe: IEnumerable and disposal Pin
Simon_Whale14-Mar-14 2:29
Simon_Whale14-Mar-14 2:29 
AnswerRe: IEnumerable and disposal Pin
jschell14-Mar-14 11:57
jschell14-Mar-14 11:57 
QuestionSharepoint Gridview Pin
DipsMak13-Mar-14 23:21
professionalDipsMak13-Mar-14 23:21 
AnswerRe: Sharepoint Gridview Pin
Richard MacCutchan14-Mar-14 0:21
mveRichard MacCutchan14-Mar-14 0:21 
AnswerRe: Sharepoint Gridview Pin
DipsMak14-Mar-14 0:35
professionalDipsMak14-Mar-14 0:35 
GeneralRe: Sharepoint Gridview Pin
Richard MacCutchan14-Mar-14 1:35
mveRichard MacCutchan14-Mar-14 1:35 
Questionhow to have Pin
jojoba201113-Mar-14 19:14
jojoba201113-Mar-14 19:14 
AnswerRe: how to have Pin
BillWoodruff13-Mar-14 23:00
professionalBillWoodruff13-Mar-14 23:00 
AnswerRe: how to have Pin
Pete O'Hanlon13-Mar-14 23:11
mvePete O'Hanlon13-Mar-14 23:11 
QuestionRe: how to have Pin
jojoba201113-Mar-14 23:23
jojoba201113-Mar-14 23:23 
AnswerRe: how to have Pin
Eddy Vluggen13-Mar-14 23:30
professionalEddy Vluggen13-Mar-14 23:30 
AnswerRe: how to have Pin
Pete O'Hanlon13-Mar-14 23:57
mvePete O'Hanlon13-Mar-14 23:57 
AnswerRe: how to have Pin
Ravi Bhavnani14-Mar-14 6:41
professionalRavi Bhavnani14-Mar-14 6:41 
QuestionHelp Please Pin
Member 1045276713-Mar-14 10:38
Member 1045276713-Mar-14 10:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.