3

我正在开发一个教育领域的网站。我想Filestream在 SQL Server 2008 中使用二进制格式将文档(MS Word 或文本文件)存储在数据库中。但我无法在文本框中检索文档。

我的代码如下:

string path = reader.GetString(0);
SqlFileStream stream1 = new SqlFileStream(path, (byte[])reader.GetValue(1), FileAccess.Read, FileOptions.SequentialScan, 0);   
StreamReader fs = new StreamReader(stream1);

fs = File.OpenText(path);
string s = fs.ReadToEnd();

txtInput.Text = s;
//lblStatus.Text = "File Succesfully Read!"
fs.Close();

此代码仅适用于存储在文件系统而不是数据库中的文档。所以我尝试了以下代码:

string path = reader.GetString(0);
SqlFileStream stream1 = new SqlFileStream(path, (byte[])reader.GetValue(1), FileAccess.Read, FileOptions.SequentialScan, 0);   
StreamReader fs = new StreamReader(stream1);

fs = File.OpenText(path);
string s = fs.ReadToEnd();

txtInput.Text = s;
//lblStatus.Text = "File Succesfully Read!"
fs.Close();

在此代码中,它在线给出错误fs = File.OpenText(path); 为“拒绝访问路径”。

请帮忙!

4

4 回答 4

1

查看这篇文章- 它详细展示了 SQL Server 2008 的文件流操作是如何工作的。

马克

于 2009-04-20T05:47:52.300 回答
1

您应该使用 stream1 读取数据。StreamReader 和 File.OpenText 方法不起作用,您只能使用 T-SQL 或 SqlFileStream 对象读取文件流数据。

于 2009-11-30T08:00:13.453 回答
0

As per my understanding, you need to connect to the server Via Windows Authentication. It will not work with SQL Server Authentication. And the Windows User should able access the Shared folder created by SQL Server for storing the Data.

于 2009-04-20T06:33:25.997 回答
0

在这两个示例中,您都没有使用 SqlFileStream 或 StreamReader,而只是使用 File.OpenText。

StreamReader fs = new StreamReader(stream1);

fs = File.OpenText(path);
string s = fs.ReadToEnd();

由于 File.OpenText 仅适用于磁盘上的文件,而不适用于 SQL 文件流,因此您需要使用流阅读器。这应该可以解决问题:

StreamReader fs = new StreamReader(stream1);

string s = fs.ReadToEnd();
于 2013-04-08T05:36:40.553 回答