将数据库读取到列表中,然后对信息进行处理 [英] Reading a Database to a list and then doing Something with the info

查看:24
本文介绍了将数据库读取到列表中,然后对信息进行处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 VB 中将数据库转换为列表.我基本上使用了 2 个列表,第一个列表包含多个电子邮件地址或它们的结尾,例如 EXMPLE@hotmail.com",第二个列表是从我已经链接到表单的数据库的列中读取的.我将发布我试图使其工作的代码,但它似乎并没有被接受.假设充当警报系统,可选择发送给数据库中的 1 个人或每个人所以请帮助完成这项工作?

Converting a Database into a list in VB. I am essentially using 2 lists the 1st list contains several email addresses or the ends of them like EXMPLE "@hotmail.com" the Second list is read from a column from a database that i already linked to the form. I will post the code I was trying to make work but it doesn't seem to be taking to. It is suppose to act as an alert system with the option to send to 1 person or everyone in the Database So Please Help Make this work?

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Try

        Dim Smtp_Server As New SmtpClient
        Dim e_mail As New MailMessage()
        Dim self As New MailAddress("archernolan151@gmail.com")
        Dim strCarriers As New List(Of String) '@nd half of carriers email
        Dim Scall As New List(Of String) 'List from the linked Column


        Smtp_Server.UseDefaultCredentials = False
        Smtp_Server.Credentials = New Net.NetworkCredential("blank@gmail.com", "password")
        Smtp_Server.Port = 587
        Smtp_Server.EnableSsl = True
        Smtp_Server.Host = "smtp.gmail.com"

        strCarriers.Add("@pm.sprint.com")
        strCarriers.Add("@vtext.com")
        strCarriers.Add("@tmomail.net")
        strCarriers.Add("@txt.att.net")


        rw = ContactsDataSet.Tables(0).NewRow  'Database Columns
        rw.Item("Call") = Scall
        ContactsDataSet.Tables(0).Rows.Add(rw) 'End of database

        If rad1.Checked = True Then   'If the radio button is clicked it will take the data from the Database verses from the text box

            For Each item In Scall
                For Each Carrier As String In strCarriers

                    e_mail = New MailMessage()
                    e_mail.From = self
                    e_mail.To.Add("item" + "Carrier")
                    e_mail.Subject = txtSubject.Text
                    e_mail.IsBodyHtml = False
                    e_mail.Body = txtMessage.Text
                    Smtp_Server.Send(e_mail)

                Next Carrier
            Next

        ElseIf rad1.Checked = False Then

            For Each Carrier As String In strCarriers

                e_mail = New MailMessage()
                e_mail.From = self
                e_mail.To.Add(txtTo.Text + Carrier)
                e_mail.Subject = txtSubject.Text
                e_mail.IsBodyHtml = False
                e_mail.Body = txtMessage.Text
                Smtp_Server.Send(e_mail)

            Next
            End If



        MsgBox("Mail Sent")

    Catch error_t As Exception
        MsgBox(error_t.ToString)



    End Try
End Sub

推荐答案

显示 {e_mail.To.Add("item" + "Carrier")} 的每个循环的第一行将创建为等于的字符串"itemCarrier"作为电子邮件地址,您可能希望它使用可变 item 和可变承运人,当它们像这样用引号引起来时,它不会创建有效的 TO 地址.如果您删除引号并直接使用变量并访问适当的属性,它可能会让您走上正确的道路.

the line in the first for each loop that shows {e_mail.To.Add("item" + "Carrier")} will create as string that equals "itemCarrier" as an email address and you probably want to have it use the variable item and the variable carrier and when they are in quotes like they are it will not create a valid TO address. If you remove the quotes and use the variables directly and access the appropriate properties it might set you on the right path.

据我所知,您的第二个 for..each 在使用变量方面是正确的.

Your second for..each is correct in its use of the variables as far as I can tell.

关于您的代码的另一点...您将 Scall 作为一个新列表变暗,但随后您将数据库变量设置为 Scall 的值,而 Scall 似乎没有从任何地方获取值...为了循环 Scall 列表的外部 for-each 循环需要从某处获取一系列值.

Another point about your code... You are diming Scall as a new list but then you are setting the database variable to the value of Scall and Scall does not seem to be getting a value from anywhere...In order for the outer for-each loop that loops the Scall list that list needs to get a series of values from somewhere.

要从呼叫"列加载列表,您将运行类似这样的程序

To load the list from the "Call" column you would run something like this

...code to connect to database
...code to read to a dataset called tmp with a table that contains the column "Call"
For each dbRow as DataRow in tmp.tables(0).rows
   Scall.Add(dbRow.Item("Call"))
Next 

现在我没有添加任何类型的错误检查,如果您使用 LINQ to SQL,则有更多优化的加载列表的方法,这会使过程更快,但在您的示例中,您似乎使用了 ADO,所以我使用了 ADO在我的回答中.

Now I have not added any kind of error checking and there are more optimized ways of loading a list if you use LINQ to SQL that make the process much much faster but in your example you appear to be using ADO so I used ADO in my answer.

您对代码不起作用"的看法非常笼统,并且没有提供任何实际细节,因为究竟什么不起作用,所以我只是在查看代码并根据对什么的直接分析指出我认为不起作用的内容你张贴.

You were pretty generic about the code "not working" and did not provide any real details as WHAT exactly was not working so I am just looking at the code and indicating what I think is not working based on straight up analysis of what you posted.

这篇关于将数据库读取到列表中,然后对信息进行处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆