gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:55:13 28 4
gpt4 key购买 nike

我想为我的数据流中的每一行执行 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 连接管理器来完成。在此之后,可以按如下方式访问它。下面的示例是您的代码的修改版本,使用 MyConnection 作为脚本组件 GUI 上连接管理器的名称。

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);
}

关于c# - 如何访问数据流脚本组件中现有的 ADO.NET 连接管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55419043/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com