简体   繁体   English

如何使用vb.net通过电子邮件发送(忽略)SQL Server数据库中的选定项目

[英]How to send to email (outlook) the selected items in SQL Server database using vb.net

I have 2 tables in my form, RegisteredSchedule and TodaySchedule . 我有2个表格,分别为RegisteredScheduleTodaySchedule When the user inputs data such as Name, Gender, SchedueleDate and Incharge to my form and clicks the "Save" button, the data will go to the RegisteredSchedule table, and if the ScheduleDate set by users is equal to date Now , that record will show in TodaySchedule table and I want that selected data from SQL Server to be sent in an email - is that possible? 当用户在表单中输入名称,性别,SchedueleDate和Incharge等数据并单击“保存”按钮时,数据将转到RegisteredSchedule表,并且如果用户设置的ScheduleDate等于日期Now ,则该记录将显示在TodaySchedule表中,并且我希望从SQL Server中选择的数据以电子邮件形式发送-可以吗? Please help me I am a newbie. 请帮助我,我是新手。

Here is my code to if the ScheduleDate set by user is = DateNow . 这是我的代码,如果用户设置的ScheduleDate为= DateNow。 I want this select statement to be send in email also, not just show in table2. 我希望这个选择语句也可以通过电子邮件发送,而不仅仅是在表2中显示。

Public Sub OnSchedule()
    Dim conn As New SqlConnection("SERVER=x\x;database = 
 x; user=x;pwd=x; ")

    conn.Open()
    Dim cmd As SqlCommand = conn.CreateCommand
    cmd.CommandText = String.Format("select PatientName,Gender,ScheduleDate,PersonInCharge from " _
    & "Schedule where ScheduleDate = CONVERT(date,getdate()) order by ScheduleDate")
    Dim dr As SqlDataReader = cmd.ExecuteReader()
    If dr.HasRows Then
        Dim dtSerial As New DataTable
        dtSerial.Load(dr)
        dgvOnSchedule.DataSource = dtSerial
    Else
        MsgBox("no data")

    End If
    dr.Close()
    conn.Close()
End Sub

here is my code in my email, i tried to put my select query in oMail.TextBody but didn't work. 这是我电子邮件中的代码,我尝试将选择查询放入oMail.TextBody,但没有用。 please suggest. 请提出建议。

    Public Sub sendEmail()
    Dim oMail As New SmtpMail("TryIt")
    Dim oSmtp As New SmtpClient()
    oMail.To = New AddressCollection("x@x.co.th")
   oMail.Cc = New 
   AddressCollection("x@x.co.th,x@x.co.th")
   oMail.Subject = "test email from VB.NET project"

   'code below not work, what should i do to my oMail.textbody to show the 
   select condition ?

   oMail.TextBody = "On SChedule Date" & OnSchedule()



 Dim oServer As New SmtpServer("x.x.x.th")
   Try
        oSmtp.SendMail(oServer, oMail)
        MessageBox.Show("success")
    Catch ex As Exception
        MessageBox.Show("no success")
    End Try     
End Sub

On your TextBody assignment, you have called method OnSchedule() which does not return anything back, only assigns and populates a datagridview object 在TextBody分配上,您调用了方法OnSchedule(),该方法不返回任何内容,仅分配并填充一个datagridview对象

You can use a StringBuilder object 您可以使用StringBuilder对象

Then read DataGridView data using foreach loops and append those values to the stringbuilder object 然后使用foreach循环读取DataGridView数据并将这些值附加到stringbuilder对象

StringBuilder sb = new StringBuilder();
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        foreach (DataGridViewCell cell in row.Cells)
        {
            sb.Append(cell.Value);
            sb.Append("\t");
        }
        sb.AppendLine();
    }
    textBox1.Text= sb.ToString();

As the final step, use this stringbuilder value in your assignment as follows 最后一步,在您的分配中使用此stringbuilder值,如下所示

oMail.TextBody = "On SChedule Date" & sb.ToString()

For VB.NET code equivalent of above C# code, please use following 对于与上述C#代码等效的VB.NET代码,请使用以下代码

    Dim sb As New StringBuilder()
    For Each row As DataGridViewRow In DataGridView1.Rows
        For Each cell As DataGridViewCell In row.Cells
            sb.Append(cell.Value)
            sb.Append("\t")
        Next
    Next

Again you will use 同样,您将使用

oMail.TextBody = "On SChedule Date" & sb.ToString()

For creating the table as HTML object, you can use following code 要将表创建为HTML对象,可以使用以下代码

Dim sb As New StringBuilder("<table>")
For Each row As DataGridViewRow In DataGridView1.Rows
    sb.Append("<tr>")
    For Each cell As DataGridViewCell In row.Cells
        sb.Append("<td>")
        sb.Append(cell.Value)
        sb.Append("</td>")
    Next
    sb.Append("</tr>")
Next
sb.Append("</table>")

This will create an HTML table script similar to following sample 这将创建一个HTML表脚本,类似于以下示例

<table><tr><td>1</td><td>Pre-School A</td></tr><tr><td>2</td><td>Pre-School B</td></tr><tr><td></td><td></td></tr></table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM