## open database connection
$conn = New-Object System.Data.SqlClient.SqlConnection("Data Source=servername; Initial Catalog=databasename; Integrated Security=SSPI")
$conn.Open()
$cmd = $conn.CreateCommand()
$cmd.CommandText ="INSERT tblTemp VALUES ('Server1', 'C:', 456)"
$cmd.ExecuteNonQuery()
$conn.Close()
Save the above code as .ps1 in your machine and then use below code in powershell.exe window to execute your script ....
powershell -ExecutionPolicy ByPass -File D:\Projects\PowerShell_ImportProject\TestScript_InsertSQL.ps1
And you are done .. same way you can use update statement or so .... you can use for loop as per your requirement to insert data into sql server....
No comments:
Post a Comment