在ssis 2008中的脚本任务中使用连接管理器

时间:2013-06-26 06:29:32

标签: ssis

我已经创建了ssis包,我在其中执行了Script任务,并且通过使用该脚本任务,我从一个Database表中获取计数并在邮件中发送该计数。现在,在该脚本任务中,如果我从变量获取连接,它就可以工作。但我想直接从连接管理器获取连接,但它无法正常工作。请同样建议。下面是我用过的代码片段。

    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

1 个答案:

答案 0 :(得分: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”