Import CSV file into SQL Server - its absolutely brain dead

I have used this puppy in the past. a bit dusty but might be helpful now or in the future if you clean it up.


$qry = 'select * from [Sheet1$]'
$sqlserver = 'yourdestserver'
$dbname = 'yourdbname'

#Create a table in destination database with 
#the with referenced columns and table name.
$tblname = 'yourtablename'
 
#######################
function Get-ExcelData
{
 
    param($connString, $qry='select * from [Sheet1$]')
 
    $conn = new-object System.Data.OleDb.OleDbConnection($connString)
    $conn.open()
    $cmd = new-object System.Data.OleDb.OleDbCommand($qry,$conn) 
    $da = new-object System.Data.OleDb.OleDbDataAdapter($cmd) 
    $dt = new-object System.Data.dataTable 
    [void]$da.fill($dt)
    $conn.close()
    $dt
 
} #Get-ExcelData
 
#######################
function Write-DataTableToDatabase
{ 
    param($dt,$destServer,$destDb,$destTbl)

    $connectionString = "Data Source=$destServer;Integrated Security=true;Initial Catalog=$destdb;"
    $bulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $connectionString
    $bulkCopy.DestinationTableName = "$destTbl"
    $bulkCopy.WriteToServer($dt)
 
}# Write-DataTableToDatabase

$RootFolder = ("C:\TheKristenShuffle\"); 
$dasfiles = $(get-childitem $RootFolder.FullName ); 

Write-Host $RootFolder

foreach( $dasfile in $dasfiles)
{
	Write-Host $dasfile
	
	$fileNameWithFullPath = $dasfile.FullName
	
	#Connection String for Excel 2007:
	$connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=`"$fileNameWithFullPath`";Extended Properties=`"Excel 12.0 Xml;HDR=YES`";"
	
	#######################
	$dt = Get-ExcelData $connString $qry
	Write-DataTableToDatabase $dt $sqlserver $dbname $tblname
}