第一个 vb 脚本工作正常,可以将总计数保存在变量 rowsAffected 中。这可以使用最后一条语句使用基于 smtp 的电子邮件发送。
如何在第二种情况下实现类似的情况?在第二种情况下,我需要将结果集发送到电子邮件。我应该做哪些修改才能将结果集保存在变量中?还如何发送该结果集
情况1
Dim sqlComm As System.Data.SqlClient.SqlCommand
Dim rowsAffected As Integer
sqlComm = New System.Data.SqlClient.SqlCommand("select count(*) from table1", sqlConn)
rowsAffected = sqlComm.ExecuteScalar()
myHtmlMessage = New MailMessage("email", "email", "This is a testing from VB script Task", rowsAffected.ToString)
案例2
sqlComm = New System.Data.SqlClient.SqlCommand("select * from tabl1", sqlConn)
rowsAffected = sqlComm.ExecuteScalar() ''what modification needed here?
myHtmlMessage = New MailMessage("email", "email", "This is a testing from VB script Task", rowsAffected.ToString) ''what modification here??
sqlComm.ExecuteScalar() 返回整数值,第一种情况的总行数。
我需要第二种情况来返回表中的所有行。还有如何使用 smtp 电子邮件发送
公共子主()
''mail variables
Dim myHtmlMessage As MailMessage
Dim mySmtpClient As SmtpClient
Dim value As NetworkCredential
''sql variables
Dim fireAgain As Boolean = True
Dim sqlConn As System.Data.SqlClient.SqlConnection
Dim sqlComm As System.Data.SqlClient.SqlCommand
Dim cm As ConnectionManager = Dts.Connections("cnn") ''Retrive the reference to the managed Connections
'' Request an open connection
sqlConn = cm.AcquireConnection(Dts.Transaction)
Dts.Events.FireInformation(0, "", "Connection is: " + sqlConn.State.ToString(), "", 0, fireAgain)
''Do your work
sqlComm = New System.Data.SqlClient.SqlCommand("SELECT [Total_Row_inserted_by this_Load] Column1 ,[Filename] Column2 FROM [Testing-DB].[dbo].[Emailbody]", sqlConn)
Dim sqlReader As System.Data.SqlClient.SqlDataReader
sqlReader = sqlComm.ExecuteReader
Dim dtDataTable As New DataTable
dtDataTable.Load(sqlReader)
''Inform SSIS you're done your work
cm.ReleaseConnection(sqlConn)
For Each row As DataRow In dtDataTable.Rows
Dts.Events.FireInformation(0, "", row("Column1").ToString() + " - " + row("Column2").ToString(), "", 0, fireAgain)
Next row
mySmtpClient = New SmtpClient("send.company.net")
mySmtpClient.Port = 585
value = New NetworkCredential("my.name@company.net", "pwdddd") ''this is the line added
mySmtpClient.Credentials = value
mySmtpClient.EnableSsl = True
For Each row As DataRow In dtDataTable.Rows
myHtmlMessage = New MailMessage("my.name@company.net", "my.name@company.net", "Table data is ", row("column1").ToString() + " - " + row("column2").ToString() + System.Environment.NewLine)
Next row
mySmtpClient.Send(myHtmlMessage)
Dts.TaskResult = ScriptResults.Success
End Sub