如何在变量vb.net中保存查询结果

时间:2013-08-15 10:00:34

标签: vb.net visual-studio-2010 vb6 ssis smtp

第一个vb脚本工作正常,可以将变量中的总计数保留为rowsAffected。 这可以使用最后一个语句使用基于smtp的电子邮件发送。

如何在第二种情况下实现类似的情境?在第二种情况下,我需要将结果集发送到email。我应该做什么修改才能将结果集保存在变量中?还有如何发送结果集

案例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电子邮件发送

Public Sub Main()

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

我的requiremnet是,将结果集作为电子邮件发送,电子邮件正文应为

column1 column2

aa 11

bb 323

cvc 342

aa 11

1 个答案:

答案 0 :(得分:1)

这段代码将介绍如何执行此操作。

sqlComm = New System.Data.SqlClient.SqlCommand("select Column1, Column2, COUNT(1) CNT from table1  group by Column1, Column2", sqlConn)
Dim sqlReader As System.Data.SqlClient.SqlDataReader
sqlReader = sqlComm.ExecuteReader
Dim dtDataTable As New DataTable
dtDataTable.Load(sqlReader)

For Each row As DataRow In dtDataTable.Rows
  Dts.Events.FireInformation(0, "", row("Column1").ToString() + " - " + row("Column1").ToString(), "", 0, fireAgain)
Next row

rowsAffected = sqlComm.ExecuteScalar()将此行代码更改为

sqlReader = sqlComm.ExecuteReader
    Dim dtDataTable As New DataTable
    dtDataTable.Load(sqlReader)

myHtmlMessage = New MailMessage(“email”,“email”,“这是从VB脚本任务测试”,rowsAffected.ToString)将此行代码修改为

 For Each row As DataRow In dtDataTable.Rows
myHtmlMessage = New MailMessage("email", "email", "This is a testing from  VB script Task", row("CNT").ToString())
Next row