param(
	[string]$hostname,
	[string]$database,
	[string]$login,
	[string]$password
)

try {
	$connectionStringBuilder = New-Object -TypeName System.Data.SqlClient.SqlConnectionStringBuilder
	$connectionStringBuilder.Add('Server', $hostname)
	$connectionStringBuilder.Add('Database', $database)
	$connectionStringBuilder.Add('UID', $login)
	$connectionStringBuilder.Add('PWD', $password)
	$connectionStringBuilder.Add('Connection Timeout','120')

	$connection = New-Object -TypeName System.Data.SqlClient.SqlConnection
	$connection.ConnectionString = $connectionStringBuilder.ToString()
    
	$command = $connection.CreateCommand()
	$command.CommandText = "SELECT [name] FROM master.dbo.sysdatabases"
	$adapter = New-Object -TypeName System.Data.SqlClient.SqlDataAdapter $command
	$dataset = New-Object -TypeName System.Data.DataSet
	$adapter.Fill($dataset) > $null
	$table = $dataset.Tables[0]
	$firstColumn = $table.Columns[0]
	foreach ($row in $table.Rows) {
		Write-Host $row[$firstColumn]
	}
} catch [Exception] {
	Write-Host $_.Exception.Message
	exit 1
}
