PowerShell some usages in SQL Server 2008

Source: Internet
Author: User

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]
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.