First at all, you have created a SqlCommand
but you didn't have execute it, you need to use theSqlCommand.ExecuteNonQuery()
Another way to achieve this using SSIS (Better way)
It is not best practice to use an SQL Task to achieve this (it will open a connection for each command, and your query is not parameterized so you have a SQL Injection ALERT!!
.
In SSIS you can do the following:
Assuming that you have added an OLEDB connection manager to the SQL server you want to import the data
- Add a SSIS Variable
User::FolderPath
of type System.String
with Value equal thef older path containning CSV Files
- Add A dataflow Task
DFT Import
- Inside the dataFlow Task add s
Script Component
(use it as a source)
- In the Script COmponent add an Output Column
OutFileName
of Type DT_WSTR
and length = 4000
- Add
User::FolderPath
variable to the script ReadOnlyVariables
In the Script Window write the following Code (I used Vb.net):
Private strFolderPath As String = String.Empty
Public Overrides Sub PreExecute()
MyBase.PreExecute()
strFolderPath = Variables.RowCount
End Sub
Public Overrides Sub CreateNewOutputRows()
Dim strFiles() As String = IO.Directory.GetFiles(strFolderPath, "*.csv", IO.SearchOption.AllDirectories)
For Each strFiles As String In strFiles
With Output0Buffer
.AddRow()
.OutFilename = strFiles
End With
Next
End Sub
Add an OLEDB Destination
- Connect the script output to the
OLEDB Connection