Как выполнить большой sql скрипт в powershell?

Пользователь

от hobart.rath , в категории: Другие , год назад

Как выполнить большой sql скрипт в powershell?

Facebook Vk Ok Twitter LinkedIn Telegram Whatsapp

2 ответа

Пользователь

от buster , год назад

@hobart.rath 

Вы можете выполнить SQL-скрипт в PowerShell, используя модуль SQLPS (SQL Server PowerShell) или используя методы ADO.NET.

  1. SQLPS (SQL Server PowerShell):
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Load the SQLPS module
Import-Module SQLPS

# Connect to the database server
$connectionString = "Data Source=server;Initial Catalog=database;Integrated Security=True"
$connection = New-Object System.Data.SqlClient.SqlConnection($connectionString)
$connection.Open()

# Execute the SQL script
$command = $connection.CreateCommand()
$command.CommandText = Get-Content "C:path	oscript.sql"
$command.ExecuteNonQuery()

# Close the connection
$connection.Close()


  1. ADO.NET:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Load the System.Data.SqlClient namespace
[System.Reflection.Assembly]::LoadWithPartialName("System.Data") | Out-Null

# Connect to the database server
$connectionString = "Data Source=server;Initial Catalog=database;Integrated Security=True"
$connection = New-Object System.Data.SqlClient.SqlConnection($connectionString)
$connection.Open()

# Execute the SQL script
$command = $connection.CreateCommand()
$command.CommandText = Get-Content "C:path	oscript.sql"
$command.ExecuteNonQuery()

# Close the connection
$connection.Close()


В приведенных примерах "Data Source=server;Initial Catalog=database;Integrated Security=True" должен быть заменен на строку подключения к вашему серверу базы данных, а "C:path oscript.sql" на путь к вашему файлу со скриптом.

Пользователь

от jaylen.trantow , 5 месяцев назад

@hobart.rath 

Также учтите, что перед выполнением скрипта вы должны убедиться, что у вас установлен PowerShell модуль для работы с SQL Server (SQLPS). Если модуль не установлен, вы можете его установить, выполнив команду:


Install-Module -Name SqlServer


После установки модуля вы можете использовать команду Import-Module SQLPS, как показано в примере.