SQL Change Automation 4

Setting up database connections

If you use a database as a source for any of the SQL Change Automation cmdlets, you'll first need to use the New-DatabaseConnection cmdlet to set up the connection details to the database. You can also use the Test-DatabaseConnection cmdlet to check the connection works. This page provides an overview of these cmdlets.


Use this cmdlet to specify the connection details for a database, which you can then use as the input for other SQL Change Automation cmdlets.


New-DatabaseConnection -ServerInstance "test01\sql2014" -Database "AdventureWorksIntegration"

If you aren't using Windows Authentication, you'll also need to specify the username and password for the SQL Server account:


New-DatabaseConnection -ServerInstance "test01\sql2014" -Database "AdventureWorksIntegration" -Username "sa" -Password "P@ssw0rd"

By assigning the output of New-DatabaseConnection to a variable, you can reuse this as the input for another cmdlet:


$testdb = New-DatabaseConnection -ServerInstance "test01\sql2014" -Database "AdventureWorksIntegration"
Sync-DatabaseSchema -Source "C:\Work\scripts" -Target $testdb

In the example above, we've assigned the connection to our test database to the variable, $testdb. We've then used this as the input for the Sync-DatabaseSchema cmdlet.


Use this cmdlet to check the connection you specified in the New-DatabaseConnection cmdlet works. 


$testdb = New-DatabaseConnection -ServerInstance "test01\sql2014" -Database "Test" | Test-DatabaseConnection

In the example above, we've used the pipe (|) symbol to take the output of New-DatabaseConnection and input it into Test-DatabaseConnection.

If New-DatabaseConnection can't connect to the database, it raises an error.

What next?

You're now ready to learn how to use New-DatabaseConnection and Test-DatabaseConnection with other SQL Change Automation cmdlets, to set up continuous integration.

Cmdlet reference

For full details about all the SQL Change Automation cmdlets, see the SQL Change Automation cmdlet reference.

Didn't find what you were looking for?