$Server = '.\SQLEXPRESS'
$InitialCatalog = 'master'
$DatabaseName = 'managementDB'
$ConnectionString = "Data Source=$Server; Initial Catalog=$InitialCatalog; Integrated Security=True;"
$conn = New-Object Data.SqlClient.SqlConnection;
$conn.ConnectionString = $ConnectionString;
$conn.Open();
Try {
$sql = "IF NOT EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = '$DatabaseName') CREATE DATABASE [$DatabaseName] COLLATE Cyrillic_General_CI_AS;"
$cmd = New-Object Data.SqlClient.SqlCommand $sql, $conn;
$cmd.ExecuteNonQuery();
}
Catch {
Write-Host "Error creating database" $DatabaseName ": " $_.Exception.Message
Exit
}
$cmd.Dispose();
$conn.Close();
$conn.Dispose();
$Server = '.\SQLEXPRESS'
$InitialCatalog = 'master'
$DatabaseName = 'managementDB'
# Connect to SQL server
$conn = New-Object System.Data.Sqlclient.SqlConnection
$conn.ConnectionString = "Data Source=$Server; Initial Catalog=$InitialCatalog; Integrated Security=True;"
$conn.Open()
# Создание БД
$cmd = $conn.CreateCommand()
$cmd.CommandText = "IF NOT EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = '$DatabaseName') CREATE DATABASE [$DatabaseName] COLLATE Cyrillic_General_CI_AS;"
$cmd.ExecuteNonQuery() | Out-Null
# Создание БД
$cmd.CommandText = "USE $DatabaseName; IF NOT EXISTS ( SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'ipaddress' ) CREATE TABLE [dbo].[ipaddress]([ip][varchar](15) NULL UNIQUE) ON [PRIMARY];"
$cmd.ExecuteNonQuery() | Out-Null
$cmd.Dispose();
$conn.Close();
$conn.Dispose();