C# 将文件列表从脚本任务记录到SQL表
我有一个SSIS脚本任务中本地文件夹中的文件列表,需要将其记录到SQL表中。是否有任何方法可以直接从脚本任务中执行,或者将结果放入对象变量中,然后将其填充到SQL表中。我不选择For Each循环容器,因为我觉得我可以从脚本任务中记录更多细节 代码: 我想将每个FileList和FileLoc作为一个表中的单独列进行lofC# 将文件列表从脚本任务记录到SQL表,c#,sql-server,script-task,C#,Sql Server,Script Task,我有一个SSIS脚本任务中本地文件夹中的文件列表,需要将其记录到SQL表中。是否有任何方法可以直接从脚本任务中执行,或者将结果放入对象变量中,然后将其填充到SQL表中。我不选择For Each循环容器,因为我觉得我可以从脚本任务中记录更多细节 代码: 我想将每个FileList和FileLoc作为一个表中的单独列进行lof PS:我是C.的新手。我在这个链接中找到了一个解决我的问题的方法 但我仍然在发布我对我提出的问题的答案 ConnectionManager cm;
PS:我是C.的新手。我在这个链接中找到了一个解决我的问题的方法 但我仍然在发布我对我提出的问题的答案
ConnectionManager cm;
System.Data.SqlClient.SqlConnection sqlConn;
cm = Dts.Connections["<connection name>"];//should be an ADO .NET connection
sqlConn = (System.Data.SqlClient.SqlConnection)cm.AcquireConnection(Dts.Transaction);
foreach (string dirpath in Directory.EnumerateDirectories("<Folder Location>"))
{
//Sql Parameters object creation
SqlParameter SQLFileNameName = new SqlParameter("@FileName", SqlDbType.VarChar,200);
SqlParameter SQLFileLocation = new SqlParameter("@FileLoc", SqlDbType.VarChar, 200);
//Assign value to SQL parameter
SQLFileLocation.Value = dirpath;
foreach (string path in Directory.EnumerateFiles(dirpath))
{
//Assign value to SQL parameter
SQLFileNameName.Value = Path.GetFileName(path);
//Populate to a SQL Table
SqlCommand sqlCmd = new SqlCommand("INSERT INTO [dbo].[<Table Name>] (FileName,FileLocation) VALUES (@FileName,@FileLoc)", sqlConn);
//Assign Parameters to script
sqlCmd.Parameters.Add(SQLFileNameName);
sqlCmd.Parameters.Add(SQLFileLocation);
//Execute SQL Command
sqlCmd.ExecuteNonQuery();
//Clear the parameters and the set the object to NULL
sqlCmd.Parameters.Clear();
sqlCmd = null;
}
}
我很想把这个标记为复制品。是的!!这对我很管用!!谢谢@BCdotWEB!!
ConnectionManager cm;
System.Data.SqlClient.SqlConnection sqlConn;
cm = Dts.Connections["<connection name>"];//should be an ADO .NET connection
sqlConn = (System.Data.SqlClient.SqlConnection)cm.AcquireConnection(Dts.Transaction);
foreach (string dirpath in Directory.EnumerateDirectories("<Folder Location>"))
{
//Sql Parameters object creation
SqlParameter SQLFileNameName = new SqlParameter("@FileName", SqlDbType.VarChar,200);
SqlParameter SQLFileLocation = new SqlParameter("@FileLoc", SqlDbType.VarChar, 200);
//Assign value to SQL parameter
SQLFileLocation.Value = dirpath;
foreach (string path in Directory.EnumerateFiles(dirpath))
{
//Assign value to SQL parameter
SQLFileNameName.Value = Path.GetFileName(path);
//Populate to a SQL Table
SqlCommand sqlCmd = new SqlCommand("INSERT INTO [dbo].[<Table Name>] (FileName,FileLocation) VALUES (@FileName,@FileLoc)", sqlConn);
//Assign Parameters to script
sqlCmd.Parameters.Add(SQLFileNameName);
sqlCmd.Parameters.Add(SQLFileLocation);
//Execute SQL Command
sqlCmd.ExecuteNonQuery();
//Clear the parameters and the set the object to NULL
sqlCmd.Parameters.Clear();
sqlCmd = null;
}
}