Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Azure Powershell to execute a .sql file

I've seen examples of running a SQL command from Azure Powershell e.g.

$connectionString = "Data Source=MyDataSource;Initial Catalog=MyDB;User ID=user1;Password=pass1;Connection Timeout=90"
$connection = New-Object -TypeName System.Data.SqlClient.SqlConnection($connectionString)
$query = "CREATE TABLE...." 
$command = New-Object -TypeName System.Data.SqlClient.SqlCommand($query, $connection)
$connection.Open()
$command.ExecuteNonQuery()
$connection.Close()

Is there a way to replace the $query with a reference to an external file that contains an entire SQL script?

Thanks.

like image 835
jqwha Avatar asked Apr 21 '15 14:04

jqwha


1 Answers

20 more mins of searching and I found it.

$connectionString = "Data Source=MyDataSource;Initial Catalog=MyDB;User ID=user1;Password=pass1;Connection Timeout=90"
$connection = New-Object -TypeName System.Data.SqlClient.SqlConnection($connectionString)
$query = [IO.File]::ReadAllText("C:\...\TestSQL.sql")
$command = New-Object -TypeName System.Data.SqlClient.SqlCommand($query, $connection)
$connection.Open()
$command.ExecuteNonQuery()
$connection.Close()
like image 159
jqwha Avatar answered Oct 26 '22 03:10

jqwha