0

我是 sql server 2008 中的 filestream 选项的新手,但我已经了解如何打开此选项以及如何创建允许您保存文件的表。假设我的表包含:id、name、filecontent

我试图在这个表中插入一个 html 文件(其中包含希伯来字符/文本)。我正在使用 Visual Studio 2008 在 asp.net (c#) 中编写。

但是当我试图读回内容时,希伯来字符变成了“?”。

我采取的行动是:

1.我读的文件是这样的:

        // open the stream reader
        System.IO.StreamReader aFile = new StreamReader(FileName,   System.Text.UTF8Encoding.UTF8);

        // reads the file to the end
        stream = aFile.ReadToEnd();

        // closes the file
        aFile.Close();

返回流;// 返回流

  1. 我将“流”作为二进制数据插入到文件内容列中。

  2. 我尝试对此列进行“选择”并且数据确实返回(在我将其转换回字符串之后)但希伯来语字符变为“?”

我该如何解决这个问题?我应该注意什么?

谢谢,加迪姆

4

2 回答 2

2

我成功解决了这个问题。我错了,问题不在 sql server 中,而是在我的代码中,当我将它从二进制传输到字符串时,反之亦然。

当您需要将字符串(具有希伯来字符)转换为二进制时,您可以编写以下行:

System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
//HtmlFile = the file i read as string and now i want to convert it to bytes array.
byte[] ConvertTextToBytesArray = encoding.GetBytes(HtmlFile);

反之亦然:

    string str;
    System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
    // result = the binary data i want to convert it back to string
    str = enc.GetString(result);

我出于某种原因使用 System.Text.ASCIIEncoding 而不是 System.Text.UTF8Encoding。

谢谢梅夫!

于 2010-04-28T16:20:04.697 回答
1

看起来 UTF8 编码可能不适用于希伯来语?

有关较早的讨论,请参见此处:http: //social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/73c81574-b434-461f-b766-fb9d0e4353c7

sr = new StreamReader(fs, Encoding.GetEncoding("windows-1255"));

或者,您确定文件是用 UTF8 编码的吗?

此外,如果 BLOB 小于 1MB,则 FILESTREAM 的实际性能可能会更差,而我希望 HTML 文件符合该描述。您是否考虑过 NVARCHAR(MAX) 。

http://blogs.msdn.com/manisblog/archive/2007/10/21/filestream-data-type-sql-server-2008.aspx

于 2010-04-28T13:06:55.153 回答