SSIS 脚本任务 - 连接 ADO.NET 并填充数据表
SSIS Script Task- Connecting the ADO.NET and populating DataTable
我需要通过脚本任务连接到 SQL 服务器数据库以填充 DataTable
,我正在使用 ADO.Net provider/connection。然而,在我的一生中,我遇到了各种各样的错误。例如,当使用 SqlAdapter
时,我收到无效对象错误,但是 SqlCommand
在 SSMS 中执行时没有错误:
SqlConnection conn;
ConnectionManager cm;
SqlCommand cmd;
cm = Dts.Connections["AdoNet"];
conn = (SqlConnection)cm.AcquireConnection(Dts.Transaction);
using (conn)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = queryString;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(myDataTable);
}
试试下面的代码:
Using(SqlConnection conn = (SqlConnection)Dts.Connections["AdoNet"].AcquireConnection(Dts.Transaction)){
if (conn.State != ConnectionState.Open){
conn.Open();}
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = queryString;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(myDataTable);
}
我需要通过脚本任务连接到 SQL 服务器数据库以填充 DataTable
,我正在使用 ADO.Net provider/connection。然而,在我的一生中,我遇到了各种各样的错误。例如,当使用 SqlAdapter
时,我收到无效对象错误,但是 SqlCommand
在 SSMS 中执行时没有错误:
SqlConnection conn;
ConnectionManager cm;
SqlCommand cmd;
cm = Dts.Connections["AdoNet"];
conn = (SqlConnection)cm.AcquireConnection(Dts.Transaction);
using (conn)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = queryString;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(myDataTable);
}
试试下面的代码:
Using(SqlConnection conn = (SqlConnection)Dts.Connections["AdoNet"].AcquireConnection(Dts.Transaction)){
if (conn.State != ConnectionState.Open){
conn.Open();}
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = queryString;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(myDataTable);
}