我的表单中有2个表,RegisteredSchedule
和TodaySchedule
。当用户将“姓名”,“性别”,“SchedueleDate”和“Incharge”等数据输入到我的表单并单击“保存”按钮时,数据将转到RegisteredSchedule
表,如果用户设置的ScheduleDate
是等于日期Now
,该记录将显示在TodaySchedule
表中,我希望从电子邮件中发送来自SQL Server的所选数据 - 这可能吗?请帮帮我,我是新手。
这是我的代码,如果用户设置的ScheduleDate是= DateNow。我希望这个select语句也可以通过电子邮件发送,而不仅仅是在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中但是没有用。请建议。
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
答案 0 :(得分:0)
在TextBody赋值中,您调用了方法OnSchedule(),它不会返回任何内容,只分配和填充datagridview对象
您可以使用StringBuilder对象
然后使用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();
作为最后一步,请在作业中使用此stringbuilder值,如下所示
oMail.TextBody = "On SChedule Date" & sb.ToString()
对于相当于上述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
您将再次使用
oMail.TextBody = "On SChedule Date" & sb.ToString()
答案 1 :(得分:0)
要将表格创建为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>")
这将创建一个类似于以下示例
的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>