从datareader创建asp.net批邮件

本文关键字:net asp datareader 创建 | 更新日期: 2023-09-27 18:17:40

我有153个从存储过程返回的电子邮件地址(这个数字可能会变化),我将它们加载到SqlDataReader (r)中。我的电子邮件中继服务器一次最多可以发送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

从datareader创建asp.net批邮件

基本方法是使用计数器变量:

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扩展将电子邮件批处理为50组,然后循环通过它们
  • 使用Ling SkipTake在一个循环中有效地做同样的事情