sql server中的文件流和aspx的C#

我正在开发一个教育领域的网站。 我想在SQL Server 2008中使用Filestream以二进制格式将文档(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(); 

此代码仅适用于存储在不在数据库中的Filesystem上的文档。 所以我尝试了以下代码:

 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);上给出错误fs = File.OpenText(path); 作为“拒绝访问路径”。

请帮忙!

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

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

根据我的理解,您需要通过Windows身份validation连接到服务器。 它不适用于SQL Server身份validation。 并且Windows用户应该能够访问由SQL Server创建的共享文件夹来存储数据。

在这两个示例中,您没有使用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();