如何使用 vb.net 将 SQL Server 数据库中的选定项目发送到电子邮件(Outlook)

How to send to email (outlook) the selected items in SQL Server database using vb.net(如何使用 vb.net 将 SQL Server 数据库中的选定项目发送到电子邮件(Outlook))
本文介绍了如何使用 vb.net 将 SQL Server 数据库中的选定项目发送到电子邮件(Outlook)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的表单中有 2 个表,RegisteredScheduleTodaySchedule.当用户在我的表单中输入 Name、Gender、SchedueleDate 和 Incharge 等数据并单击保存"按钮时,数据将转到 RegisteredSchedule 表,如果 ScheduleDate 等于日期 Now,该记录将显示在 TodaySchedule 表中,我希望从 SQL Server 中选择的数据通过电子邮件发送 - 是可能的?请帮助我我是新手.

I have 2 tables in my form, RegisteredSchedule and TodaySchedule. 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? Please help me I am a newbie.

这是我的代码,如果用户设置的 ScheduleDate 是 = DateNow .我希望这个 select 语句也通过电子邮件发送,而不仅仅是在 table2 中显示.

Here is my code to if the ScheduleDate set by user is = DateNow . I want this select statement to be send in email also, not just show in table2.

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

这是我的电子邮件中的代码,我试图将我的选择查询放在 oMail.TextBody 中,但没有奏效.请提出建议.

here is my code in my email, i tried to put my select query in oMail.TextBody but didn't work. 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

推荐答案

将表格创建为 HTML 对象,可以使用以下代码

For creating the table as HTML object, you can use following code

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

这将创建一个类似于以下示例的 HTML 表格脚本

This will create an HTML table script similar to following sample

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

这篇关于如何使用 vb.net 将 SQL Server 数据库中的选定项目发送到电子邮件(Outlook)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
SQL query to group by day(按天分组的 SQL 查询)
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)