2015-10-14 35 views
0

創建電子郵件asp.net批我有一個存儲過程返回153個的電子郵件地址(這個數字可能會有所不同),我加載到SqlDataReaderr)。我的電子郵件中繼服務器一次最多可以發送50個電子郵件地址。我怎樣才能創建X組的數量,所以我沒有超過它的限制,從讀者?從DataReader的

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() 

我在一個空白的如何做到這一點。我的想法是沿着這...

'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 

任何幫助將不勝感激。

+0

http://stackoverflow.com/q/11463734/952310 –

+0

你爲什麼標籤C#當你的示例代碼是在VB? –

回答

1

的基本方法是使用一個計數器變量:

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 

其他方式:

  • 使用Linq extension批量的電子郵件成50組,然後依次通過他們
  • 使用凌SkipTake在一個循環中做有效的相同的事情