Guest User

Untitled

a guest
Feb 22nd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. Clear-Host
  2. # no these are not my real settings!
  3. $ServerName = "my.little.server.0800.database.windows.net"
  4. $targetDatabaseName = "MyLittleDatabase"
  5. $userName = "bob"
  6. $Password = "NoPasswords4U!"
  7.  
  8. $pathToFiles = "C:\Users\richardlee\mylittleDatabase\dbo\Tables"
  9. $files = Get-ChildItem $pathToFiles
  10. Write-Host "There are $($files.Count) create table files to execute." -ForegroundColor DarkMagenta -BackgroundColor Yellow
  11.  
  12. foreach ($file in $files){
  13. $old = Get-Content $file.FullName
  14. Set-Content -Path $file.FullName -Value "PRINT '$($file.FullName)'"
  15. Add-Content -Path $file.FullName -Value $old
  16. }
  17.  
  18. ### batch files to sqlcmd
  19. $date1 = get-date
  20. sqlcmd -i $files.FullName -S $ServerName -d $TargetDatabaseName -G -U $Username -P $Password -I -y 0 -b -j
  21. $date2 = get-date
  22. $taskTime = "Bulk executing files using sqlcmd, task took(HH:MM:SS:MS) " + (New-TimeSpan -Start $date1 -End $date2)
  23. write-Host $taskTime -ForegroundColor White -BackgroundColor DarkGreen
  24.  
  25. ### foreach file execute sqlcmd
  26. $date1 = get-date
  27. foreach ($file in $files) {
  28. sqlcmd -i $file.FullName -S $ServerName -d $TargetDatabaseName -G -U $Username -P $Password -I -y 0 -b -j
  29. }
  30. $date2 = get-date
  31. $taskTime = "Using sqlcmd for each file, task took(HH:MM:SS:MS) " + (New-TimeSpan -Start $date1 -End $date2)
  32. write-Host $taskTime -ForegroundColor White -BackgroundColor DarkCyan
  33.  
  34. ### sql client
  35. $date1 = get-date
  36. $userDbCon = New-Object System.Data.SqlClient.SqlConnection
  37. $userDbCon.ConnectionString = "Server = $ServerName; Database = $targetDataBaseName; Authentication=Active Directory Password; UID = $Username; PWD = $Password;"
  38. $userDbCon.Open();
  39. $SqlExecCmd = New-Object System.Data.SqlClient.SqlCommand
  40. $SqlExecCmd.Connection = $userDbCon
  41. foreach ($file in $files) {
  42. $SqlExecCmd.CommandText = Get-Content $file.FullName
  43. $SqlExecCmd.ExecuteScalar()
  44. }
  45. $userDbCon.Close();
  46. $date2 = get-date
  47. $taskTime = "Using sqlclient for each file, task took(HH:MM:SS:MS) " + (New-TimeSpan -Start $date1 -End $date2)
  48. write-Host $taskTime -ForegroundColor White -BackgroundColor DarkMagenta
Add Comment
Please, Sign In to add comment