[英]Export from SQL Server 2012 to .CSV through Management Studio
[英]Change Language in Export-CSV file from SQL Server through PowerShell ISE
我需要通過 PowerShell ISE 從 SQL 服務器導出很多表格,其中包含丹麥條目(意味着我們有 Æ、Ø 和 Å 字母),並且丹麥字母沒有用問號代替???。
例如,我有一個名為NavnLeg
的表,其中包含以下數據:
Navn Måned År
--------------------
Jørgen 3 2019
Arne 4 2018
Åse 7 2018
Hans 2 2017
我想通過 Powershell 導出表(因為我有大約 70 個表需要復制到本地驅動器)。
當我在下面運行腳本時,我的 CSV 文件最終如下:
Navn M?ned ?r
--------------------
J?rgen 3 2019
Arne 4 2018
?se 7 2018
Hans 2 2017
這是我當前的腳本,我想在其中聲明 CSV 文件應該以某種方式使用丹麥字母。
Get-ChildItem –Path "E:\Backup" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-60))} | Remove-Item
$server = "DEV02"
$database = "Test"
$tablequery = "SELECT name from sys.tables WHERE NAME IN ('NavnLeg')"
$date = (date -f yyyy/MM/dd)
#Declare Connection Variables
$connectionTemplate = "Data Source={0};Integrated Security=SSPI;Initial Catalog={1};"
$connectionString = [string]::Format($connectionTemplate, $server, $database)
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandText = $tablequery
$command.Connection = $connection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$connection.Close()
foreach ($Row in $DataSet.Tables[0].Rows)
{
$queryData = "SELECT * FROM [$($Row[0])]"
$extractFile = "E:\Backup\$($date)_$($Row[0]).csv"
$command.CommandText = $queryData
$command.Connection = $connection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$connection.Close()
$DataSet.Tables[0] | Export-Csv $extractFile -NoTypeInformation
}
在 PowerShell 5.1 上,Export-CSV 默認為 ASCII。 -Encoding
參數指定目標文件的編碼類型,可以選擇ASCII, BigEndianUnicode, Default, OEM, Unicode, UTF7, UTF8, UTF32
根據我的評論,UTF8 是一個不錯的選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.