In SSIS, i have a “Data Flow”, which contains “OLE DB Source” and Flat File Destination (CSV).
The Original CSV is also UTF-8:
The Flat file is UTF8:
When I Run it, data transfers correctly from SQL server to Excel file.
But there will be 1 empty line as the last line.
I wrote a c# script in “script task editor” , to remove the last empty line.
It also works correct BUT It changes the German alphabet (ä, ö, ü, ß):
Although i wrote in C# about keeping UTF8 format:
using (var reader = new StreamReader(sourceFile, Encoding.UTF8))
using (var writer = new StreamWriter(tempFile, false, new UTF8Encoding(true)))
Does anyone know what is the problem with this C# code that it leads to change German alphabets?
public void Main()
{
try
{
// Retrieve the file path from the SSIS variable
string sourceFile = Dts.Variables["User::SourceFilePath"].Value.ToString();
string tempFile = Path.GetTempFileName();
// Use UTF-8 encoding for both reading and writing
using (var reader = new StreamReader(sourceFile, Encoding.UTF8))
using (var writer = new StreamWriter(tempFile, false, new UTF8Encoding(true)))
{
string line;
bool isFirstLine = true;
// Read each line from the source file
while ((line = reader.ReadLine()) != null)
{
// Check if the line is not empty or whitespace
if (!string.IsNullOrWhiteSpace(line))
{
// Write the non-empty line to the temp file
if (isFirstLine)
{
writer.Write(line);
isFirstLine = false;
}
else
{
writer.Write(Environment.NewLine + line);
}
}
}
}
// Delete the original source file
File.Delete(sourceFile);
// Rename the temp file to the original source file name
File.Move(tempFile, sourceFile);
// Indicate successful execution
Dts.TaskResult = (int)ScriptResults.Success;
}
In Connection manager, Code Page is: 65001 (UTF-8)
Locale: German
20