PowerShell was integrated early in SQL Server 2005, and I used it for the first time in SQL Server 2008. Today is free to summarize a few practical examples out. Welcome this expert to refine:
First, do not SqlServerCmdletSnapin100 this snapin to write a few scripts to manipulate the common data
1. Since some readers asked how to display the tables in the database using PowerShell, here is a simple function for reference
#==============================================
# sql Server 2008-powershell
# Show user table
# <c> Zivsoft</c>
#==============================================
function showcustomizeddatatable{
$ Sqlserver=read-host "Enter SQL Server name:"
$Database =read-host "Enter Database name:"
$SqlConnection = N Ew-object System.Data.SqlClient.SqlConnection
$CnnString = "server= $SQLSERVER;D atabase= $DATABASE; integrated Security=true
$SqlConnection. ConnectionString = $CnnString
$SqlCmd = New-object System.Data.SqlClient.Sql Command
$SqlCmd. CommandText = "select name from sysobjects where type= ' u '
$SqlCmd. Connection = $SqlConnec tion
$SqlAdapter = new-object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter. SelectCommand = $SQLCMD
$DataSet = New-object System.Data.DataSet
$SqlAdapter. Fill ($DataSet)
$SqlConnection. Close ()
$ Dataset.tables[0]
}
2. Show data from SQL query
#==============================================
# SQL Server 2008 - PowerShell
# 显示查询数据内容
# <c>zivsoft</c>
#==============================================
function Get-DataTable([string]$query)
{
$dataSet= new-object "System.Data.DataSet" "DataSetName"
$da = new-object "System.Data.SqlClient.SqlDataAdapter" ($query, $CnnString)
[void] $da.Fill($dataSet)
return $dataSet.Tables[0]
}