从脚本组件+ SSIS执行SQL

时间:2010-02-16 23:35:33

标签: c# ssis scripting components task

我试图从脚本组件执行sql,但在尝试建立SQL连接时遇到错误。 PreExecute()

下的脚本组件中的我的代码
base.PreExecute();
    variables = null;
    VariableDispenser.LockForWrite("User::TableName");
    VariableDispenser.LockForWrite("User::SqlConnectionString");
    VariableDispenser.GetVariables(out variables);
    tableName = variables[0].Value.ToString();
    sqlConnection = variables[1].Value.ToString();

    dt = new DataTable();

    //sqlConnection = Data Source=LOCALHOST;Initial Catalog=MergeTestDestination;Integrated Security=True;
    using (SqlConnection cnn = new SqlConnection(sqlConnection))
    {
        try
        {
            cnn.Open();
            MessageBox.Show("We are connected.....");
            cnn.Close();
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

错误:

at System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString,Int32 currentPosition,StringBuilder buffer,Boolean useOdbcRules,String& keyname,String& keyvalue)    在System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable,String connectionString,Boolean buildChain,Hashtable synonyms definitions,Boolean firstKey)    在System.Data.Common.DbConnectionOptions..ctor(String connectionString,Hashtable synonyms,Boolean useOdbcRules)    在System.Data.SqlClient.SqlConnectionString..ctor(String connectionString)    在System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString,DbConnectionOptions previous)    在System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(String connectionString,DbConnectionPoolGroupOptions poolOptions,DbConnectionOptions& userConnectionOptions)    at System.Data.SqlClient.SqlConnection.ConnectionString_Set(String value)    at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value)    在System.Data.SqlClient.SqlConnection..ctor(String connectionString)    在ScriptMain.PreExecute()    在Microsoft.SqlServer.Dts.Pipeline.ScriptComponentHost.PreExecute()

我在做错了什么?有什么帮助吗?

由于

1 个答案:

答案 0 :(得分:0)

将以下代码放在连接构造函数之前     Debug.Assert的(string.IsNullOrEmpty(SqlConnection的)!);