【问题标题】:Runtime Error Exemption as been thrown by the Target of an invocation调用目标引发的运行时错误豁免
【发布时间】:2021-12-18 08:02:14
【问题描述】:

我是 SSIS 和 C# 的新手。我一直在从事一个从目录中读取文件信息并存储在 SQL 表中的项目。我有多个目录,并且能够使用脚本任务从 4 个目录中读取信息。第 5 个目录向我抛出“调用目标引发的运行时错误豁免”错误。我添加了 Messagebox.show 来解决问题,但我观察到包突然遇到此错误,而没有被扔到 zip 文件夹或空文件夹中。

请检查我的代码:

public void Main()
    {
        // TODO: Add your code here
        SqlConnection myADONETConnection = new SqlConnection();
        myADONETConnection = (SqlConnection)(Dts.Connections["DBConn"].AcquireConnection(Dts.Transaction) as SqlConnection);
        //MessageBox.Show(myADONETConnection.ConnectionString, "ADO.NET Connection");
        SqlCommand sqlCmd = new SqlCommand();
        sqlCmd.Connection = myADONETConnection;


        string DirPath = Dts.Variables["User::VarDirectoryPath"].Value.ToString();

        //get all files from directory
        string[] files = Directory.GetFiles(DirPath, "*"); /* Using "*" to identity any zip folders and read file information from them */

      if (Directory.Exists(DirPath))
        {
            string[] folders = System.IO.Directory.GetDirectories(DirPath, "*",SearchOption.AllDirectories);
            foreach (string foldername in folders)
            {
                string[] fnames = Directory.GetFiles(foldername);
                foreach (string filename in fnames)
                {

                    FileInfo file = new FileInfo(filename);

                    sqlCmd.CommandText = "Insert into dbo.FileInformation_6 Values('" + file.FullName + "','" +
                     file.Name + "','" + file.LastAccessTime + "','" + file.CreationTime + "','" + file.Length / 1024 + "')";
                    //MessageBox.Show(sqlCmd.CommandText);

                    sqlCmd.ExecuteNonQuery();
                }
            }
        }            
        else
        {
            MessageBox.Show("InComplete");
        }

        Dts.TaskResult = (int)ScriptResults.Success;
    }

如果你能帮助我,请告诉我。

谢谢, Dhipthee Pujar

【问题讨论】:

  • 在不了解有关异常的更多详细信息的情况下,我们无法为您提供帮助。听起来我们需要查看异常对象的InnerException 属性的MessageStackTrace
  • 旁注:您需要 ADO 对象上的 using 块。这里有 SQL 注入问题。您应该指定要插入的列名。您应该考虑只使用SqlBulkCopy 而不是循环使用INSERT 命令,这样会更快

标签: c# sql ssis


【解决方案1】:

我能够导航并解决问题。我的代码因多种原因被丢弃:

  1. 我创建了我的表,字符限制为 150 - 我将其更改为 255。
  2. 如果遇到带有 " 或 ' 的文件名 - 我重命名了此类文件。

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多