Copy multiple CSV files to SQL Server tables using PowerShell, each CSV is named same as table name

51 views Asked by At

I want to move .CSV files data into SQL Server tables, the name of .CSV file is schema.TableName. Thus the names of .CSV file and the target table are the same.

I am using SQL bulk copy. Script is not moving data to the database, also no error is generated. I have tried troubleshooting but cannot find why it is not copying data to database. Also I do not have much knowledge of PowerShell script, so I'm looking for some help.

# Function to import CSV data into SQL Server table
function Import-CsvToSqlServer {
    param (
        [System.Data.SqlClient.SqlConnection]$connection,
        [string]$csvFilePath
    )
    try {
        
        $tableName = [System.IO.Path]::GetFileNameWithoutExtension($csvFilePath)
        Write-Host $csvFilePath                                                         #Check Code
        Write-Host "$($tableName) getting loaded"                                       #Check Code
        $csvData = Import-Csv -Path $csvFilePath | Select-Object * -ExcludeProperty Type
        $sqlbulkCopy = New-Object -TypeName System.Data.SqlClient.SqlBulkCopy($connection, [System.Data.SqlClient.SqlBulkCopyOptions]::TableLock)
        $sqlbulkCopy.EnableStreaming = $true
        $sqlbulkCopy.BatchSize=50000
        $sqlbulkCopy.BulkCopyTimeout=0
        $sqlbulkCopy.DestinationTableName = $tableName
        $sqlbulkCopy.WriteToServer($csvData)
        Write-Host "CSV data from $($csvFile.Name) imported into SQL Server table $($tableName) successfully."
        Move-Item -Path $csvFilePath -Destination $archiveFolderPath -Force
        Write-Host "CSV file $($csvFile.Name) moved to archive folder successfully."
    } catch {
        Send-EmailNotification -recipient "[email protected]" -subject "Error Notification" -body "Failed to import CSV data into SQL Server table $($tableName): $_"
    }
}

# Main script
try 
{
    # Connect to SQL Server
    $sqlConnection = ConnectTo-SqlServer
   
    # Get list of CSV files in the folder
    $csvFiles = Get-ChildItem -Path $csvFolderPath -Filter *.csv
    if ($csvFiles.Count -eq 0) 
    {
        Write-Host "No CSV files found in the folder."
        exit
    }
    
    # Iterate through CSV files
    foreach ($csvFile in $csvFiles) 
    {
       Write-Host "Import started"
        Import-CsvToSqlServer -connection $sqlConnection -csvFilePath $csvFile.
        Write-Host "Import Completed"
    }
} 
0

There are 0 answers