【问题标题】:Execute SQL script files via PowerShell using a console app使用控制台应用程序通过 PowerShell 执行 SQL 脚本文件
【发布时间】:2014-07-18 13:26:32
【问题描述】:

我想使用 powershell 文件脚本和控制台应用程序执行 3 个 SQL 文件。

我的控制台应用程序在正确的“powershell 脚本”上运行良好,但我的问题是我应该在我的脚本 (dbscript.ps1) 中编写什么来自动执行我的 SQL 文件。我看过几个例子,但都不适合我的情况。

我有一个 ASP.NET 解决方案,并且我有 3 个 SQL 文件位于解决方案项目中(没有子文件夹/没有子项目)。我还有一个项目,其中包含用于执行我的 ps1 文件的控制台应用程序。

我真正需要的是:

a) 我在app.config 中有一个连接字符串,可用于连接到我的本地数据库

b) 我需要在我的dbscript.pb1 中使用该连接字符串,然后我需要执行我的 SQL 文件(s1.sqls2.sqls3.sql 就在我的解决方案中).

我看过这个例子:

$conn = New-Object System.Data.SqlClient.SqlConnection
$conn.ConnectionString = "" // Here I should take my connection string from app.config
$conn.Open()
$sql = "" // Somehow I should take all my sql files (I don't know how to get the path for them)
 $cmd = New-Object System.Data.SqlClient.SqlCommand($sql,$conn)
$rdr = $cmd.ExecuteReader()
$test = @()
while($rdr.Read())
{
    $test += ($rdr["EMP_STATUS"].ToString())
}
Write-Output $test

那么,如何app.config获取连接字符串,如何设置所有sql文件以特定顺序执行?

【问题讨论】:

  • 你为什么不直接连接到数据库来运行脚本呢?为什么要通过 powershell 运行它们?
  • 因为将来我会处理大约 20-25 个脚本。您认为这是一种可靠的方法吗?为什么不尝试自动执行它们?
  • 如果是这种情况,只需将它们保存为 .sql 文件而不是 powershell。以某种方式命名它们,您可以更改它的顺序或创建一个 XML 文件,您将读取并按照您指定的顺序执行它们。
  • 您是否对如何使用 PowerShell 进行过调查?看来您只需要使用Get-Content 即可完成脚本。

标签: c# asp.net sql powershell connection-string


【解决方案1】:

不是您要的,但我会这样做只是因为当您已经在编写一个可以为您执行此操作的应用程序时,处理 powershell 似乎更令人头疼。

//get application path and script directory
private const string scriptPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Scripts");

private void RunScripts()
{
    string[] scripts = Directory.GetFiles(scriptPath, "*.sql", SearchOption.TopDirectoryOnly);
    //name them script01.sql, script02.sql ...etc so they sort correctly
    Array.Sort(scripts);

    if (scripts.Length > 0)
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionStringKey"]))
        {
            conn.Open();
            using (SqlCommand cmd = conn.CreateCommand())
            {
                foreach (string script in scripts)
                {
                    cmd.CommandText = File.ReadAllText(script);
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }
}

【讨论】:

  • +1 是一个不错的选择。但我不能接受它,因为我的问题是关于 PowerShell 的。不过,很酷。
  • @shemuel 创建连接字符串时,只需使用 master 作为数据库或在您的脚本中,您可以使用“use master”并让您的脚本切换它。
【解决方案2】:

那么原因是 .sql 文件的数量。好吧,您只能使用 .bat。甚至您可以在子文件夹中订购 .sql 文件并将 .bat 文件放在容器文件夹的根目录中:

C:\MySqlScripts
    |
    |_RunAllSqlFiles.bat
    |
    |_Functions
    |    |
    |    |_Function_1.sql
    |
    |_StoredProcedures
         |
         |_StoredProc_1.sql

.bat文件会递归执行所有的.sql文件,这里是RunAllSqlFiles.bat文件的内容:

@echo off
ECHO %USERNAME% process started at: %TIME%  >output.txt

for /R %%i in (*.sql) do (
sqlcmd.exe -S tcp:SERVER_INSTANCE,1433 -E -d DB_NAME -i "%%i"  >>output.txt
)
pause

您必须更改 SERVER_INSTANCE 和 DB_NAME。该 .bat 使用 sqlcmd 和 Windows 身份验证

因此,使用 .bat 双击就足够了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    相关资源
    最近更新 更多