Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 11
Cập nhật lúc 10h18' ngày 28/04/2008

Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 1
Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 2
Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 3
Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 4
Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 5
Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 6
Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 7

Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 8
Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 9
Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 10

 The MAK

Export đầu ra thành XML

Phần 10 của bài này chúng tôi đã giới thiệu cách sử dụng các kịch bản PowerShell kết hợp với SMO và các tham số để tạo kịch bản SQL Server.

Trong phần này, chúng tôi sẽ hướng dẫn các bạn cách sử dụng cmdlets của PowerShell kết hợp với SQL Server client và việc lưu đầu ra để export thành một file văn bản hoặc file XML.

Hãy giả dụ rằng chúng ta muốn truy vấn một bảng SQL Server nào đó bằng transact sql và lưu đầu ra dưới định dạng văn bản hoặc định dạng XML. Việc sử dụng cmdlets của PowerShell , kết nối SQL Server client và lưu đầu ra có thể được thực hiện rất dễ dàng.

Chúng ta hãy tạo c:\ps\output.ps1 như thể hiện bên dưới (Xem hình 1.1)

param
(
  [string] $SQLServer,
  [string] $Database,
  [string] $outputType,
  [string] $filename,
  [string] $Query
)

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = 
"Server=$SQLSERVER;Database=$DATABASE;Integrated Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand

$SqlCmd.CommandText = $Query
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()

if ($outputType  -eq "Text")
{
$DataSet.Tables[0] | format-table -auto > $filename
}

if ($outputType  -eq "xml")
{
$DataSet.Tables[0] |Export-Clixml $filename
}


Hình 1.1

Kịch bản này có thể được thực thi như thể hiện bên dưới (hình 1.2)

./output "HOME\SQLEXPRESS" "VixiaTrack" "TEXT" "c:\test.txt" "Select dbid,name from sys.sysdatabases"


Hình 1.2

Giải thích về các tham số:

  • output là kịch bản output.ps1 trong thư mục c:\ps
  • HOME là hostname
  • SQLEXPRESS là tên SQL Server instance trong host HOME
  • VixiaTrack là tên cơ sở dữ liệu cư trú trong SQLEXPRESS instance
  • TEXT là định dạng output được yêu cầu. Nó có thể là TEXT hoặc XML.
  • C:\test.txt là tên file và vị trí của nó
  • Select dbid,name from sys.sysdatabases là truy vấn Transact SQL được thực thi với cơ sở dữ liệu

Khi kịch bản PowerShell được thực thi, nó truy vấn cơ sở dữ liệu và lưu đầu ra vào một file văn bản đã được thông qua như các tham số (Tham khảo hình 1.3 và hình 1.4)


Hình 1.3

Nội dung bên trong của file test.txt

dbid name         
---- ----         
   1 master       
   2 tempdb       
   3 model        
   4 msdb         
   5 test         
   6 VixiaTrack   
   7 XMLTest      
   8 admin        
   9 AdventureWorks


Hình 1.4

Kịch bản PowerShell tương tự có thể được thực thi bằng cách sử dụng XML như một tham số để tạo định dạng XML.

Kịch bản này có thể được thực thi như hình bên dưới (hình 1.5)

./output "HOME\SQLEXPRESS" "VixiaTrack" "XML" "c:\test.xml" "Select dbid,name from sys.sysdatabases"


Hình 1.5

Giải thích các tham số:

  • output là kịch bản output.ps1 trong thư mục c:\ps
  • HOME là hostname
  • SQLEXPRESS là tên SQL Server instance trong host HOME
  • VixiaTrack là tên cơ sở dữ liệu cư trú trong SQLEXPRESS instance
  • XML là định dạng output được yêu cầu. Nó có thể là TEXT hoặc XML.
  • C:\test.txt là tên file và vị trí của nó.
  • Select dbid,name from sys.sysdatabases là truy vấn Transact SQL được thực thi với cơ sở dữ liệu

Khi kịch bản PowerShell được thực thi, nó sẽ truy vấn cơ sở dữ liệu và lưu đầu ra vào một file XML đã được thông qua như một tham số (Hình 1.6 và Hình 1.7)


Hình 1.6

Nội dung bên trong của file test.xml

- <Objs Version="1.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
- <Obj RefId="RefId-0">
- <TN RefId="RefId-0">
  <T>System.Data.DataRow</T>
  <T>System.Object</T>
  </TN>
- <Props>
  <I16 N="dbid">1</I16>
  <S N="name">master</S>
  </Props>
  </Obj>
- <Obj RefId="RefId-0">
  <TNRef RefId="RefId-0" />
- <Props>
  <I16 N="dbid">2</I16>
  <S N="name">tempdb</S>
  </Props>
  </Obj>
- <Obj RefId="RefId-0">
  <TNRef RefId="RefId-0" />
- <Props>
  <I16 N="dbid">3</I16>
  <S N="name">model</S>
  </Props>
  </Obj>
- <Obj RefId="RefId-0">
  <TNRef RefId="RefId-0" />
- <Props>
  <I16 N="dbid">4</I16>
  <S N="name">msdb</S>
  </Props>
  </Obj>
- <Obj RefId="RefId-0">
  <TNRef RefId="RefId-0" />
- <Props>
  <I16 N="dbid">5</I16>
  <S N="name">test</S>
  </Props>
  </Obj>
- <Obj RefId="RefId-0">
  <TNRef RefId="RefId-0" />
- <Props>
  <I16 N="dbid">6</I16>
  <S N="name">VixiaTrack</S>
  </Props>
  </Obj>
- <Obj RefId="RefId-0">
  <TNRef RefId="RefId-0" />
- <Props>
  <I16 N="dbid">7</I16>
  <S N="name">XMLTest</S>
  </Props>
  </Obj>
- <Obj RefId="RefId-0">
  <TNRef RefId="RefId-0" />
- <Props>
  <I16 N="dbid">8</I16>
  <S N="name">admin</S>
  </Props>
  </Obj>
- <Obj RefId="RefId-0">
  <TNRef RefId="RefId-0" />
- <Props>
  <I16 N="dbid">9</I16>
  <S N="name">AdventureWorks</S>
  </Props>
  </Obj>
  </Objs>


Hình 1.7

Kết luận

Phần 11 của loạt bài này đã minh chứng cho các bạn thấy được cách sử dụng cmdlets của PowerShell kết hợp với SQL Server client và việc lưu đầu ra để export thành một file văn bản hoặc file XML.

Văn Linh (Theo Databasejournal)
Đánh giá(?):
WinRAR 3.71
Phát hành: RARLAB
Download:301791
Dung lượng: 1.15 MB
Tìm thêm:winrar, rarlab, phần mềm nén, giải nén
 
Foxit Reader 2.3 Build 3309
Phát hành: Foxit Software
Download:34472
Dung lượng: 2.57 MB
Tìm thêm:foxit software, pdf, adobe acrobat reader, adobe, multimedia, miễn phí
 
WinZip 11.2
Phát hành: WinZip Computing
Download:26680
Dung lượng: 13.03 MB
Tìm thêm:winzip computing, trình nén, giải nén
 
WinRAR 3.80 beta 5
Phát hành: RARLAB
Download:26029
Dung lượng: 1.18 MB
Tìm thêm:winrar, rarlab, phần mềm nén, giải nén
 
Auslogics Disk Defrag 1.4.16
Phát hành: AusLogics
Download:13982
Dung lượng: 1.36 MB
Tìm thêm:auslogics, defrag, chống phân mảnh, ổ cứng, hard disk
Những chuyện kỳ bí