0

I have 153 email addresses returned from a stored procedure (this number could vary) which I load into a SqlDataReader (r). My email relay server can send up to 50 email addresses at a time. How can I create X number of groups so I don't exceed its limit, from the reader?

Dim myCommand As New SqlCommand("sproc_Get_Emails", myConnection)
myCommand.CommandType = CommandType.StoredProcedure

myCommand.Parameters.Add(New SqlParameter("@List_Type", SqlDbType.Char))
myCommand.Parameters("@List_Type").Value = priority

Dim r As SqlDataReader
myConnection.Open()
r = myCommand.ExecuteReader()

I am at a blank on how to do this. My thinking was along this...

'get count of all email addresses and divide by 50, then send each in 50 record batches???
 Dim email_count As Integer = 0

 'get count and divide by 50 
  email_count = r.RecordsAffected

  Dim list_size As Double = email_count / 50

Any help would be much appreciated.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
Rob
  • 1,226
  • 3
  • 23
  • 41

1 Answers1

1

The basic way is to use a counter variable:

Dim count as Integer
Dim address as String

While r.Read
    address = address + r.Field("email") + ";"
    count = count + 1
    If count = 50 Then
        ' send 
        count = 0
        address = ""
    End If
End While
' see if there are any remaining addresses 
If Not String.IsNullOrEmpty(address) Then
    ' send
End If

Other ways:

  • Use a Linq extension to batch the emails into groups of 50, then loop through them
  • Use Ling Skip and Take in a loop to do effectively the same thing
Community
  • 1
  • 1
D Stanley
  • 149,601
  • 11
  • 178
  • 240