在powershell中存储然后重新使用sql连接字符串

时间:2015-03-03 14:04:27

标签: sql-server powershell

有没有办法保存sql连接详细信息并在同一个脚本中重用它们。我写了下面的内容,在同一个数据库中导出3个sql表的内容。它可以工作,但它重新使用了大量的代码,我无法弄清楚如何减少它。

我已尝试声明所有连接信息,然后将其说明现在的完整代码,但这只是输出第一个表3次。

我也曾尝试在开头说明一次,但再次无效。

#agent_lookup

$server = "test"
$database = "MI_Lookups"
$query = "EXEC [dbo].[proc_update_new_agents]
          SELECT  [l].agent_key AS 'Agent_Key'
                  ,ISNULL([l].agent_name,'') AS 'Agent_Name'
                  ,ISNULL([l].agent_name_1,'') AS 'Agent_Name_1'
                  ,ISNULL([l].agent_type,'') AS 'Agent_Type'
          FROM [dbo].[agent_test] AS [l]
          ORDER by [l].agent_key asc"

$connection = New-Object System.Data.SqlClient.SqlConnection("Data Source=$server;Integrated Security=SSPI;Initial Catalog=$database;")
$command = New-Object System.Data.SqlClient.SqlCommand($query, $connection)
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($command)
$DataSet = New-Object System.Data.DataSet

$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0] | Export-Csv $saveloaction\"Agent_Lookup.csv"
$connection.Close()

#organisation_lookup

$server = "test"
$database = "MI_Lookups"
$query = "EXEC [dbo].[proc_update_new_organisations]
          SELECT  [l].organisation_key AS 'Organisation_Key'
                  ,ISNULL([l].broker_name,'') AS 'Broker_Name'
                  ,ISNULL([l].team_member,'') AS 'Team_Member'
                  ,ISNULL([l].team_leader,'') AS 'Team_Leader'
          FROM [dbo].[orgnisation_test] as [l]
          ORDER BY [l].organisation_key asc"

$connection = New-Object System.Data.SqlClient.SqlConnection("Data Source=$server;Integrated Security=SSPI;Initial Catalog=$database;")
$command = New-Object System.Data.SqlClient.SqlCommand($query, $connection)
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($command)
$DataSet = New-Object System.Data.DataSet

$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0] | Export-Csv $saveloaction\"Organisation_Lookup.csv"
$connection.Close()

#insurer_lookup

$server = "test"
$database = "MI_Lookups"
$query = "EXEC [dbo].[proc_update_new_insurers]
          SELECT  [l].insurer_key AS 'Insurer_Key'
                  ,ISNULL([l].insurer_name,'') AS 'Insurer_Name'
                  ,ISNULL([l].insurer_name_1,'') AS 'Insurer_Name_1'
                  ,ISNULL([l].team_member,'') AS 'Team_Member'
                  ,ISNULL([l].team_leader,'') AS 'Team_Leader'
          FROM [dbo].[insurer_test] AS [l]
          ORDER BY [l].insurer_key asc"

$connection = New-Object System.Data.SqlClient.SqlConnection("Data Source=$server;Integrated Security=SSPI;Initial Catalog=$database;")
$command = New-Object System.Data.SqlClient.SqlCommand($query, $connection)
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($command)
$DataSet = New-Object System.Data.DataSet

$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0] | Export-Csv $saveloaction\"Insurer_Lookup.csv"
$connection.Close()

对我来说,只需要编写一次连接信息似乎是合乎逻辑的,但我是PowerShell的新手,并努力使这个变得简单,因为我确信它应该是。以上基本上是相同的脚本重复三次,并引用了不同的表。

一如既往地谢谢。

2 个答案:

答案 0 :(得分:1)

我会使用一个函数来处理查询的执行。这样,您只需要更改查询和存储输出的位置。

请注意,我在这里将数据库详细信息作为全局变量。如果您只使用这一个脚本,这很好,但如果此脚本正在调用或被其他脚本调用,则最好避免使用。

function Query-ToCsv($query, $csvLocation) {

    try {
        $command = New-Object System.Data.SqlClient.SqlCommand($query, $connection)
        $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($command)
        $DataSet = New-Object System.Data.DataSet
        $SqlAdapter.Fill($DataSet)
        $DataSet.Tables[0] | Export-Csv $csvLocation
    }
    catch {
        Write-Warning "Exception: " + $_.Exception.Message
    }
}

# script globals
$server = "test"
$database = "MI_Lookups"
$connection = New-Object System.Data.SqlClient.SqlConnection("Data Source=$server;Integrated Security=SSPI;Initial Catalog=$database;")

#agent_lookup
$query = "EXEC [dbo].[proc_update_new_agents]
          SELECT  [l].agent_key AS 'Agent_Key'
                  ,ISNULL([l].agent_name,'') AS 'Agent_Name'
                  ,ISNULL([l].agent_name_1,'') AS 'Agent_Name_1'
                  ,ISNULL([l].agent_type,'') AS 'Agent_Type'
          FROM [dbo].[agent_test] AS [l]
          ORDER by [l].agent_key asc"

Query-ToCsv $query "$saveloaction\Agent_Lookup.csv"

#organisation_lookup
$query = "EXEC [dbo].[proc_update_new_organisations]
          SELECT  [l].organisation_key AS 'Organisation_Key'
                  ,ISNULL([l].broker_name,'') AS 'Broker_Name'
                  ,ISNULL([l].team_member,'') AS 'Team_Member'
                  ,ISNULL([l].team_leader,'') AS 'Team_Leader'
          FROM [dbo].[orgnisation_test] as [l]
          ORDER BY [l].organisation_key asc"


Query-ToCsv $query "$saveloaction\Organisation_Lookup.csv"

#insurer_lookup
$query = "EXEC [dbo].[proc_update_new_insurers]
          SELECT  [l].insurer_key AS 'Insurer_Key'
                  ,ISNULL([l].insurer_name,'') AS 'Insurer_Name'
                  ,ISNULL([l].insurer_name_1,'') AS 'Insurer_Name_1'
                  ,ISNULL([l].team_member,'') AS 'Team_Member'
                  ,ISNULL([l].team_leader,'') AS 'Team_Leader'
          FROM [dbo].[insurer_test] AS [l]
          ORDER BY [l].insurer_key asc"

Query-ToCsv $query "$saveloaction\Insurer_Lookup.csv"

$connection.Close()

请注意,这是未经测试的(很明显,因为我无法访问您的数据库),但应该让您朝着正确的方向前进。

答案 1 :(得分:0)

您可以简单地重复使用连接对象。我已经编辑了你的代码来执行此操作,但我还没有运行它 - 如果我做了一些不好的编辑,但是它的要点应该是正确的。

$server = "test"
$database = "MI_Lookups"
$connection = New-Object System.Data.SqlClient.SqlConnection("Data Source=$server;Integrated Security=SSPI;Initial Catalog=$database;")

#agent_lookup

$query = "EXEC [dbo].[proc_update_new_agents]
          SELECT  [l].agent_key AS 'Agent_Key'
                  ,ISNULL([l].agent_name,'') AS 'Agent_Name'
                  ,ISNULL([l].agent_name_1,'') AS 'Agent_Name_1'
                  ,ISNULL([l].agent_type,'') AS 'Agent_Type'
          FROM [dbo].[agent_test] AS [l]
          ORDER by [l].agent_key asc"

$command = New-Object System.Data.SqlClient.SqlCommand($query, $connection)
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($command)
$DataSet = New-Object System.Data.DataSet

$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0] | Export-Csv $saveloaction\"Agent_Lookup.csv"

#organisation_lookup

$query = "EXEC [dbo].[proc_update_new_organisations]
          SELECT  [l].organisation_key AS 'Organisation_Key'
                  ,ISNULL([l].broker_name,'') AS 'Broker_Name'
                  ,ISNULL([l].team_member,'') AS 'Team_Member'
                  ,ISNULL([l].team_leader,'') AS 'Team_Leader'
          FROM [dbo].[orgnisation_test] as [l]
          ORDER BY [l].organisation_key asc"

$command = New-Object System.Data.SqlClient.SqlCommand($query, $connection)
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($command)
$DataSet = New-Object System.Data.DataSet

$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0] | Export-Csv $saveloaction\"Organisation_Lookup.csv"

#insurer_lookup

$query = "EXEC [dbo].[proc_update_new_insurers]
          SELECT  [l].insurer_key AS 'Insurer_Key'
                  ,ISNULL([l].insurer_name,'') AS 'Insurer_Name'
                  ,ISNULL([l].insurer_name_1,'') AS 'Insurer_Name_1'
                  ,ISNULL([l].team_member,'') AS 'Team_Member'
                  ,ISNULL([l].team_leader,'') AS 'Team_Leader'
          FROM [dbo].[insurer_test] AS [l]
          ORDER BY [l].insurer_key asc"

$command = New-Object System.Data.SqlClient.SqlCommand($query, $connection)
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($command)
$DataSet = New-Object System.Data.DataSet

$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0] | Export-Csv $saveloaction\"Insurer_Lookup.csv"

$connection.Close()