【发布时间】: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属性的Message和StackTrace。 -
旁注:您需要 ADO 对象上的
using块。这里有 SQL 注入问题。您应该指定要插入的列名。您应该考虑只使用SqlBulkCopy而不是循环使用INSERT命令,这样会更快