如何插入包含希伯来语文本的 html 文件并使用 filestream 选项在 sql server 2008 中读回它?

发布于 2024-08-30 05:49:39 字数 751 浏览 3 评论 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. 我尝试对此列进行“选择”,数据确实返回(在我将其转换回字符串之后),但希伯来语字符变成了“?”

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

谢谢, 加迪姆

i am new to the filestream option in sql server 2008,
but i have already understand how to open this option and how to create a table that allow you to save files.
let say my table contains:
id,name, filecontent

i tried to insert an html file (that has hebrew chars/text in it) to this table.
i'm writing in asp.net (c#), using visual studio 2008.

but when i tried to read back the content , hebrew char becomes '?'.

the actions i took were:

1. i read the file like this:

        // 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();

return stream; // returns the stream

  1. i inserted the 'stream' to the filecontent column as binary data.

  2. i tried to do a 'select' to this column and the data did return (after i coverted it back to string) but hebrew chars become '?'

how do i solve this problem ?
what I should pay attention to ?

thanks,
gadym

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自找没趣 2024-09-06 05:49:39

我成功解决了这个问题。
我错了,问题不在 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。

谢谢梅夫!

I succeeded to solve this problem.
I was wrong , the problem wasnt in the sql server , but in my code, when i transfer it from binary to string and vice versa.

when you need to convert string (that have hebrew chars) to binary you can write the following lines:

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);

and vice versa :

    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);

i used for some reason System.Text.ASCIIEncoding instead of System.Text.UTF8Encoding.

thank you Meff !

回忆凄美了谁 2024-09-06 05:49:39

看起来 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,并且我希望 HTML 文件符合该描述,则 FILESTREAM 实际上可能表现更差。您是否考虑过 NVARCHAR(MAX)?

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

Looks like the UTF8 encoding may not work with the Hebrew?

See here for an older discussion: http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/73c81574-b434-461f-b766-fb9d0e4353c7

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

Alternatively, are you sure the file is encoded in UTF8?

Also, FILESTREAM may actually perform worse if the BLOB is under 1MB, and HTML files I would expect to fit that description. Have you considered NVARCHAR(MAX) instead.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文