PowerShell操作数据库

用powershell来连接SQL Server数据库,并返回数据集。
其实跟在.net差不多,也就是利用了.net中的数据对象来操作而已。
 
 
#basic SQL DB access helper functions
Function Query-SQLDB
{
    <#
        .Synopsis
                This function connects and queries given SQL DB
        .Parameter SQLServer
                name of the SQL servert to connect to
        .Parameter SQLDBName
                name of the DB to query for
        .Parameter SQLQuery
                SQL query
        .Example
                Query-SQLDB    
        #>

  [CmdletBinding()]
        Param(
                $SQLServer="sqlserver",            
    
                $SQLDBName="testDB",
    
                $SQLQuery="select * from testTB where UID=1"
        )
    
  $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
  $SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"    
  $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
  $SqlCmd.CommandText = $SQLQuery
  $SqlCmd.Connection = $SqlConnection    
  $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
  $SqlAdapter.SelectCommand = $SqlCmd
    
  $DataSet = New-Object System.Data.DataSet
  $SqlAdapter.Fill($DataSet) > $null
  [void]$SqlConnection.Close()    

  return $DataSet
}

你可能感兴趣的:(.net,数据库,职场,powershell,休闲)