Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There are multiple csv files in a folder named parent with data in it and those files have the same format. I need to extract data in the csv file and insert them into a sql server table named log.once data are inserted, the csv files should move into a folder named archive folder. if there is any file in the parent folder where the extension is not csv, that file should move to another folder named error folder.
Table structure of sql server is as follows:
 name varchar (50),
 email varchar(100),
 address varchar(50)

Table structure in all the csv files are as follows:
name| email| address 


What I have tried:

First of all I created a folder called files and inside that I created 3 folders as parent where all the csv files and files with other extensions are included, archive folder and the error folder. In the visual studio ssdt, I used a foreach loop container in the control flow and added a data flow task inside the foreach loop container.Created a string variable named filename without assigning any value to it.

I edited the foreach loop container collection with these:
folder path as E:\files\parent
Files as *.*
Enumerator : Foreach File enumerator

and variable mapping in the editing window,I used the created variable "FileName" as the variable with 0 index.

in the data flow I used flat file source and a OLE DB destination. In order to map details I used OLEDB connection manager and mapped the log table to it and in the flat file connection manager I selected one file from the parent folder since all the csv files has the same format.

In the properties of flat file connection manager, I gave a connection string in the expressions property and set it to the created variable "FileName".
It is as follows : Propert :Connection String , Expression :@[User::FileName].

By executing this solution data is loaded from flat file sources to the log table.
Posted
Updated 8-Mar-20 4:26am

 
Share this answer
 
Comments
Maciej Los 6-Mar-20 5:23am    
5ed!
phil.o 6-Mar-20 6:15am    
Thanks :)
Member 13958707 6-Mar-20 6:01am    
Thank you for the solution but I have already completed the data extracting part and loading it to the sql server. I want to move files from the parent folder to archive folder once inserting data is completed.
phil.o 6-Mar-20 6:14am    
This is not your original question. Anyway, you can use a system task to move files from one folder to another.
Moving files from folder to another in SSIS - StackOverflow
Member 13958707 6-Mar-20 6:48am    
I tried making 2 variables to both source directory and destination directory. when I run it, it stops with an error saying the path format is wrong
Another way to do this is with the Import Export Wizard. Right Click on the DB and got Task and import and export will appear.

The MSFT documentation is below

Imp/exp Wizard:

Import and Export Data with the SQL Server Import and Export Wizard - SQL Server Integration Services (SSIS) | Microsoft Docs[^]


Flat File Docs:
Connect to a Flat File Data Source (SQL Server Import and Export Wizard) - SQL Server Integration Services (SSIS) | Microsoft Docs[^]
 
Share this answer
 
Comments
Member 13958707 8-Mar-20 10:28am    
Thank you! I tried the way as mentioned in the second link you have posted. Anyway I posted the solution that I came with and it works perfectly
I added two foreach loop containers. 1st one contains the data flow task and it is connected to a flat-file system task component. Flat file system task in the 1st foreach loop container, the source is set to the variable where the parent folder path is assigned to the value and the destination is set to the variable where the archive folder path is assigned to its value. file type in the 1st loop container should be edited as *.csv while the 2nd loop container should be edited as *.*.

The 2nd loop container contains only a flat-file system task in which the destination is set to the variable with the value of error folder path and the source is set to the variable with parent folder path. 1st foreach loop container is connected to the 2nd loop container. Running this package will transfer all the *.csv files in the parent folder to the archive folder and at the same time, data will be tranformed into the sql server table. files with different extensions will be moved to the error folder.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900