2

我已经制作了 ssis 包,在其中我执行了脚本任务,并通过使用该脚本任务,我从数据库表之一中计数并在邮件中发送该计数。现在,在那个脚本任务中,如果我从变量中获取连接,它就可以工作。但我想直接从连接管理器获取连接,但它不起作用。请提出相同的建议。以下是我使用的代码片段。

    Public Sub Main()
    Dim str_con As String
    str_con = Dts.Connections("INPUND79.Sample.sample").ConnectionString.ToString()
    Dim con As New SqlConnection(str_con)
    con.Open()
    Dim cmd As New SqlCommand("select count(*) from Department", con)
    Dim a As Integer = Convert.ToInt32(cmd.ExecuteScalar())
    Dim message As String = "Total Number of Records in Department Table is :" & a & ""
    Dim SmtpServer As New SmtpClient()
    Dim mail As New MailMessage()
    SmtpServer.Host = "UKCAMSCAS10.aveva.com"
    mail = New MailMessage()
    mail.From = New MailAddress("mandar.dandage@aveva.com")
    mail.To.Add("mandar.dandage@aveva.com")
    mail.Subject = "Count in Table"
    mail.Body = message
    SmtpServer.Send(mail)
    Dts.TaskResult = ScriptResults.Success
    End Sub
4

1 回答 1

0

即使你已经回答了你的问题;但是,这不是最好的方法,因为您现在使用的是单独的连接,例如,如果您使用包中的事务,它不会受到影响。您应该使用以下脚本进行OLE DB连接:

ConnectionManager cm = Dts.Connections["INPUND79.Sample.sample"];
IDTSConnectionManagerDatabaseParameters100 cmParams = cm.InnerObject as IDTSConnectionManagerDatabaseParameters100;
connection = cmParams.GetConnectionForSchema() as OleDbConnection;
// Do whatever you want with the connection
cm.ReleaseConnection(connection);

这是正确的方法。您应该参考"C:\Program Files (x86)\Microsoft SQL Server\110\SDK\Assemblies\Microsoft.SQLServer.DTSRuntimeWrap.dll"

于 2014-04-02T18:06:03.707 回答