从datareader创建asp.net批量电子邮件

时间:2015-10-14 20:51:03

标签: c# asp.net sql-server vb.net

我有一个存储过程返回的153个电子邮件地址(此数字可能有所不同),我加载到SqlDataReaderr)。我的电子邮件中继服务器一次最多可以发送50个电子邮件地址。如何创建X个组,以便我不会超出读者的限制?

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

非常感谢任何帮助。

1 个答案:

答案 0 :(得分: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个,然后循环显示
  • 在循环中使用Ling SkipTake来有效地执行相同的操作