【发布时间】:2017-02-20 12:46:38
【问题描述】:
我想将生成的 txt 文件转换为 UTF8 格式的文件,以便可以通过 Polybase 将其加载到我的 Azure SQL DW 中。要求源文件为UTF8格式。
MSDN 有一个“IO 流示例”HERE 非常适合单个作业。不过,我正在尝试为大约 30 个表构建一个 SSIS 解决方案。我相信使用这种方法会导致竞争条件,当另一个 SSIS 包需要它时,PS 脚本将被 1 个 SSIS 包锁定。
我是 sql 开发人员,而不是 .NET 开发人员,所以请原谅我。假设我知道如何将参数传递给脚本任务,如何将上述内容转换为 SSIS C# 脚本任务?
来自 MSDN 的 PowerShell 代码
#Static variables
$ascii = [System.Text.Encoding]::ASCII
$utf16le = [System.Text.Encoding]::Unicode
$utf8 = [System.Text.Encoding]::UTF8
$ansi = [System.Text.Encoding]::Default
$append = $False
#Set source file path and file name
$src = [System.IO.Path]::Combine("<MySrcFolder>","<MyUtf8stage>.txt")
#Set source file encoding (using list above)
$src_enc = $ascii
#Set target file path and file name
$tgt = [System.IO.Path]::Combine("<MyDestFolder>","<MyFinalstage>.txt")
#Set target file encoding (using list above)
$tgt_enc = $utf8
$read = New-Object System.IO.StreamReader($src,$src_enc)
$write = New-Object System.IO.StreamWriter($tgt,$append,$tgt_enc)
while ($read.Peek() -ne -1)
{
$line = $read.ReadLine();
$write.WriteLine($line);
}
$read.Close()
$read.Dispose()
$write.Close()
$write.Dispose()
更新
我发现了一个类似的帖子,我可以根据自己的需要进行调整,我发誓在发布之前我搜索了高低。无论如何,这对我有用。如果您仍然看到要改进它,请分享:
public void Main()
{
//$Package::SourceSQLObject = tablename
//$Package::StageFile_DestinationFolderPath = rootpath eg "C:\temp\"
string path = (string)Dts.Variables["$Package::StageFile_DestinationFolderPath"].Value;
string name = (string)Dts.Variables["$Package::SourceSQLObject"].Value;
string from = Path.Combine(path, name) + ".csv";
string to = Path.ChangeExtension(from, "txt");
Dts.Log("Starting " + to.ToUpper(), 0, null);
using (StreamReader reader = new StreamReader(from, Encoding.ASCII, false, 10))
using (StreamWriter writer = new StreamWriter(to, false, Encoding.UTF8, 10))
{
while (reader.Peek() >= 0)
{
writer.WriteLine(reader.ReadLine());
}
}
Dts.TaskResult = (int)ScriptResults.Success;
【问题讨论】:
-
嘿,布雷迪,如果此解决方案对您有用,您能否将其发布为答案,然后稍后再标记。它有助于搜索。谢谢
-
我想我已经完成了您的要求。让我知道我是否可以做一些不同的事情。
-
看起来不错。谢谢布雷迪
标签: c# tsql ssis azure-sqldw