filestream in sql server and C# for aspx

痞子三分冷 提交于 2019-12-12 13:09:52

问题


I am developing a website for educational domain. I want to store a document (MS Word or text file) in database in binary format using Filestream in SQL Server 2008. but I am unable to retrieve the document in a textbox.

My code is as follows:

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

This code only works for documents that are stored on the Filesystem not in the database. So I tried the following code:

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

In this code, it gives error on line fs = File.OpenText(path); as "Access denied to path".

Please help!


回答1:


Check out this article - it shows in great detail how the filestream operations with SQL Server 2008 work.

Marc




回答2:


You should read your data using stream1. The StreamReader and File.OpenText approaches will not work, you can only read filestream data using T-SQL or SqlFileStream object.




回答3:


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.




回答4:


In both examples you are not using your SqlFileStream or StreamReader and just using File.OpenText.

StreamReader fs = new StreamReader(stream1);

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

As File.OpenText only works for files on disk and not SQL filestreams you need to use the stream reader. This should do the trick:

StreamReader fs = new StreamReader(stream1);

string s = fs.ReadToEnd();


来源:https://stackoverflow.com/questions/766926/filestream-in-sql-server-and-c-sharp-for-aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!