madhuri pokharkar
madhuri pokharkar

Reputation:

filestream in sql server and C# for aspx

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!

Upvotes: 3

Views: 5319

Answers (3)

Lummo
Lummo

Reputation: 1169

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

Upvotes: 0

Pawel Marciniak
Pawel Marciniak

Reputation: 2228

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.

Upvotes: 1

Anuraj
Anuraj

Reputation: 19618

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.

Upvotes: 0

Related Questions