sql server中的文件流和aspx的C# [英] filestream in sql server and C# for aspx

查看:75
本文介绍了sql server中的文件流和aspx的C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我的代码如下:

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

在此代码中,它在第fs = File.OpenText(path);行上给出了错误 为拒绝访问路径".

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

请帮助!

推荐答案

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

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.

这篇关于sql server中的文件流和aspx的C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆