Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字节[]-数组转换为ANSI_C#_Sql Server_Unicode_Visual Studio Lightswitch_Ansi - Fatal编程技术网

C# 将字节[]-数组转换为ANSI

C# 将字节[]-数组转换为ANSI,c#,sql-server,unicode,visual-studio-lightswitch,ansi,C#,Sql Server,Unicode,Visual Studio Lightswitch,Ansi,我正在尝试使用C#和Microsoft Lightswitch将MSSQL数据库中的byte[]blob转换为Windows-1252 ANSI格式,并将结果返回到下载文件 这是我认为应该起作用的 我正在使用创建字符串 System.Text.Encoding v_Unicode = System.Text.Encoding.Unicode; System.Text.Encoding v_ANSI_Windows_1252 = System.Text.Encoding.GetEncoding(1

我正在尝试使用C#和Microsoft Lightswitch将MSSQL数据库中的byte[]blob转换为Windows-1252 ANSI格式,并将结果返回到下载文件

这是我认为应该起作用的

我正在使用创建字符串

System.Text.Encoding v_Unicode = System.Text.Encoding.Unicode;
System.Text.Encoding v_ANSI_Windows_1252 = System.Text.Encoding.GetEncoding(1252);

string v_Content_1252 = v_ANSI_Windows_1252.GetString(System.Text.Encoding.Convert(v_Unicode, v_ANSI_Windows_1252, v_Unicode.GetBytes(v_Content)));
byte[] ansiArray = v_ANSI_Windows_1252.GetBytes(v_Content_1252);
并将其写入数据库。当我试图用

int v_fileId = Int32.Parse(context.Request.QueryString["p_FileId"]);

DataTableName v_fexpd = serverContext.DataWorkspace.ApplicationData.DataTableName_SingleOrDefault(v_fileId);
MemoryStream memStream = new MemoryStream(v_fexpd.Inhalt);

string v_Content= System.Text.Encoding.GetEncoding(1252).GetString(v_fexpd.Content);

context.Response.Clear();
context.Response.ContentType = "text/csv";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + v_fexpd.Filename);
context.Response.Write( v_Content );
context.Response.End();

。。。但它只返回Unicode。我做错了什么?

这是为任何有类似问题的人准备的。答案是越过一条小溪。。。我所做的是:

// Create a temporary file, delete if it already exists
string MyFile = Path.GetTempPath() + v_fexpd.Dateiname;
if (File.Exists(MyFile)) {
    File.Delete(MyFile);
}

using (TextWriter tw = new StreamWriter(MyFile.ToString(), true, System.Text.Encoding.GetEncoding(1252)))
    tw.WriteLine(v_Inhalt);

context.Response.Clear();
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + v_fexpd.Dateiname);
context.Response.AddHeader("Content-Type", "text/csv; charset=windows-1252");

// Write the file - which at this point is correctly-encoded - 
// directly into the output.
context.Response.WriteFile(MyFile);

context.Response.End();

// Leave no traces
File.Delete(MyFile);

这适用于任何有类似问题的人。答案是越过一条小溪。。。我所做的是:

// Create a temporary file, delete if it already exists
string MyFile = Path.GetTempPath() + v_fexpd.Dateiname;
if (File.Exists(MyFile)) {
    File.Delete(MyFile);
}

using (TextWriter tw = new StreamWriter(MyFile.ToString(), true, System.Text.Encoding.GetEncoding(1252)))
    tw.WriteLine(v_Inhalt);

context.Response.Clear();
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + v_fexpd.Dateiname);
context.Response.AddHeader("Content-Type", "text/csv; charset=windows-1252");

// Write the file - which at this point is correctly-encoded - 
// directly into the output.
context.Response.WriteFile(MyFile);

context.Response.End();

// Leave no traces
File.Delete(MyFile);

字符串的内容始终为UTF16
System.Text.Encoding.GetEncoding(1252).GetString(v_fexpd.Content)
将从
v_fexpd.Content
读取数据,将其视为ANSI并将其转换为UTF16。因此,对于我来说,真的没有办法将这个字节数组作为ANSI进行输出?那么
v_fexpd.Content
不是您想要的字节数组吗?是的。但我需要将其作为字符串提供给context.Response.Write。。。或者我现在完全是傻瓜吗?
string
不可能是UTF16以外的任何东西,所以我不知道你怎么做。string
的内容将始终是UTF16
System.Text.Encoding.GetEncoding(1252).GetString(v_fexpd.Content)
将从
v_fexpd.Content
读取数据,将其视为ANSI并将其转换为UTF16。因此,对于我来说,真的没有办法将这个字节数组作为ANSI进行输出?那么
v_fexpd.Content
不是您想要的字节数组吗?是的。但我需要将其作为字符串提供给context.Response.Write。。。或者我现在完全是个傻瓜吗?
string
除了UTF16之外,没有其他方法,所以我不知道你怎么做。