Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#SSIS脚本-尝试将结果保存到sql server表_C#_Ssis - Fatal编程技术网

C#SSIS脚本-尝试将结果保存到sql server表

C#SSIS脚本-尝试将结果保存到sql server表,c#,ssis,C#,Ssis,我是C#的新手。我需要对一个非常糟糕的文件进行一些奇特的操作,然后将结果保存到一个表中。它们只是客户的评论,但对财务很重要。我能够通过常规任务将文件传输到SQL表。然后我使用脚本读取文件(没问题),手动操作数据(没问题),但无法将结果保存回SQL表。我可以将结果保存到arraylist并将其保存为txt文件(没问题)。这是不起作用的代码。(很抱歉缩进不好) MDTEMP2=ADO.NET连接 for (int d = 0; d < comDataRecNo.Length; d++) {

我是C#的新手。我需要对一个非常糟糕的文件进行一些奇特的操作,然后将结果保存到一个表中。它们只是客户的评论,但对财务很重要。我能够通过常规任务将文件传输到SQL表。然后我使用脚本读取文件(没问题),手动操作数据(没问题),但无法将结果保存回SQL表。我可以将结果保存到arraylist并将其保存为txt文件(没问题)。这是不起作用的代码。(很抱歉缩进不好)

MDTEMP2=ADO.NET连接

for (int d = 0; d < comDataRecNo.Length; d++)
{
  SqlConnection myCnx3 = (SqlConnection)(Dts.Connections["MDTEMP2"].AcquireConnection(Dts.Transaction) as SqlConnection);
SqlCommand writeCommentSQL = new System.Data.SqlClient.SqlCommand("INSERT INTO [dbo].[cspc2_commentswip] ([Customer No_],[Comment]) VALUES(@CustNo, @Comm)", myCnx3);

writeCommentSQL.Parameters.AddWithValue("@CustNo", comDataCustNo[d]);
writeCommentSQL.Parameters.AddWithValue("@Comm", comDataComment[d]);

try
{
myCnx3.Open();
writeCommentSQL.ExecuteNonQuery();
myCnx3.Close();

}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
for(int d=0;d
…以下是错误:
这里有几个错误

SqlConnection myCnx3 = (SqlConnection)(Dts.Connections["MDTEMP2"].AcquireConnection(Dts.Transaction) as SqlConnection);
您正在重用可能在应用程序中其他位置共享的同一连接对象。不要这样做,只需创建新的连接对象并在使用后进行处理。NET使用了一种称为连接池的东西来管理数据库连接,您不必担心重用现有对象来提高性能

try
{
    myCnx3.Open();
    writeCommentSQL.ExecuteNonQuery(); //What if this throws exception?
    myCnx3.Close();

}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
    Dts.TaskResult = (int)ScriptResults.Failure;
}
如果执行非查询行抛出异常,则连接将永远不会关闭。您应该使用下面的语句来包装您的连接

    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(
            queryString, connection);
        connection.Open();
    }

AcquireConnection似乎正在打开连接。。。下面的代码现在可以使用了

 for (int d = 0; d < comDataRecNo.Length; d++)
                {

                    using (SqlConnection myCnx3 = (SqlConnection)(Dts.Connections["MDTEMP2"].AcquireConnection(Dts.Transaction) as SqlConnection))
                    {
                        SqlCommand writeCommentSQL = new System.Data.SqlClient.SqlCommand("INSERT INTO [dbo].[cspc2_commentswip] ([Customer No_],[Comment]) VALUES(@CustNo, @Comm)", myCnx3);

                        writeCommentSQL.Parameters.AddWithValue("@CustNo", comDataCustNo[d]);
                        writeCommentSQL.Parameters.AddWithValue("@Comm", comDataComment[d]);

                        writeCommentSQL.ExecuteNonQuery();
                        myCnx3.Close();
                    } 
                }
for(int d=0;d
Steve、myCnx3和MDTEMP2仅在本节代码中使用,这是您的意思吗?我确实尝试过使用,但也出现了同样的错误。我的断点显示它从myCnx3.Open()开始,然后进入异常。@CarolineRoy您一定在其他地方打开了连接而没有关闭它。