如何访问数据流脚本组件中现有的 ADO.NET 连接管理器

How to access to existing ADO.NET Connection Manager within data flow script component

我想为我的数据流中的每一行执行 SQL 语句,所以我在数据流中使用脚本组件(不是脚本任务)。 我尝试了如下代码,但无法编译。

using (SqlConnection connection = this.Connections.Connection.AcquireConnection(null) as SqlConnection)
{
    using (SqlCommand command = connection.CreateCommand())
    {
        command.CommandText = "SELECT [Value] FROM dbo.MyTable";
        command.CommandType = CommandType.Text;

        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                ProfanityWords.Add(reader.GetValue(0).ToString());
            }
        }
    }

    this.Connections.Connection.ReleaseConnection(connection);
}

您是否在脚本组件上添加了对连接管理器的引用?这可以通过在脚本组件的连接管理器选项卡上添加 ADO.NET 连接管理器来完成。在此之后,可以按如下方式访问它。下面的示例是您的代码的修改版本,在脚本组件 GUI 上使用 MyConnection 作为连接管理器的名称。

using (SqlConnection connection = this.Connections.MyConnection.AcquireConnection(null) as SqlConnection)
{
    using (SqlCommand command = connection.CreateCommand())
    {
        command.CommandText = "SELECT [Value] FROM dbo.MyTable";
        command.CommandType = CommandType.Text;

        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                ProfanityWords.Add(reader.GetValue(0).ToString());
            }
        }
    }

    this.Connections.MyConnection.ReleaseConnection(connection);
}