日期:2014-05-18  浏览次数:20513 次

帮一下小妹,请问如何读取sql数据库中的word文件并下载?
请问如何读取sql数据库中的word文件并下载?知道的贴出代码看一下,c#的./
假设存储的字段为wdnr,类型为image.

------解决方案--------------------
/*
先从数据库中把图片生成到某个路径下,然后指定用控件显示该图片
*/
SqlConnection conn=new SqlConnection();
conn.ConnectionString=System.Configuration.ConfigurationSettings.AppSettings[ "SQLCONNECTIONSTRING "].ToString();
SqlCommand comm=new SqlCommand();
comm.Connection=conn;
conn.Open();
//按id提取文件
comm.CommandText= "select * from [file] where id= "+txtID.Text.Trim().ToString();//id应该是传递过来的值
DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=comm;
da.Fill(ds);
conn.Close();

//文件分类 fileClass
string strFileClass=ds.Tables[0].Rows[0][ "fileClass "].ToString();
//单位名称
string strUnitName=ds.Tables[0].Rows[0][ "unitName "].ToString();
//文件名称 fileName
string strFileName=ds.Tables[0].Rows[0][ "fileName "].ToString();
//文件类型 fileType
string strFileType=ds.Tables[0].Rows[0][ "fileType "].ToString();//获取扩展名
//文件内容 fileContent
byte[] strFileContent=new byte[0];
strFileContent=(byte[])ds.Tables[0].Rows[0][ "fileContent "];
int ArraySize;
ArraySize=new int();
ArraySize=strFileContent.GetUpperBound(0);//获取数据流写入的最大长度
string strCreateFilePath=this.Request.PhysicalApplicationPath+@ "fileCreate\ "+strFileName;//获取文件在服务器的创建位置
//生成文件时,如果文件重复,则删除原文件
if(File.Exists(strCreateFilePath))
{
File.Delete(strCreateFilePath);
}
FileStream fs=new FileStream(strCreateFilePath, FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(strFileContent,0,ArraySize);
fs.Close();
//指定一个HTML Image控件的地址,注意应设置该控件在服务器端运行
this.IMG1.Src=strCreateFilePath;

------解决方案--------------------
/// <summary>
/// 为指定的报表模板创建一个报表。
/// 使用该页面必须:
/// (1)传递 path 参数,用于表示要创建的报表的基础模板文件的服务器路径;
/// (2)在 Session 中添加索引键为“__ReportDataObjectLoadOption”,值为“ReportDataObjectLoadOption”对象实例的值,以便提供报表要填充的数据加载选项。
/// </summary>
public partial class ReportService_Reporter : MisPage
{
protected void Page_Load(object sender, EventArgs e)
{
string path = this.Request[ "path "];
if (String.IsNullOrEmpty(path))
throw new ArgumentNullException( "没有说明要创建的报表所依据的基础模板文件路径。 ");
ReportDataObjectLoadOption option = this.Session[ "__ReportDataObjectLoadOption "] as ReportDataObjectLoadOption;
if (option == null)
throw new InvalidOperationException( "没有提供报表的填充数据加载选项。 ");

ReportService rs = new ReportService();

byte[] buffer = null;
try
{
buffer = rs.CreateReport(this.CurrentUser.UserInfo.BranchOrgID, path, option.ToString());
}
catch
{
this.ClientMessage = "未能创建相应的报表,报表模板可能不存在,或者当前报表服务不可用或者正在忙,请稍后再试。 ";
return;
}
if (buffer == null)
{
this.ClientMessage = "报表服务还没有启动,或者报表服务器上的相应模板文件可能被意外删除了,请稍后再试! ";
return;
}
string ext = Path.GetExtension(path).ToLower();
if(ext == ".xls " || ext == ".xlt ")
this.Response.ContentType = "application/vnd.ms-excel ";
else
this.Response.ContentType = "application/vnd.ms-word ";
this.Response.AddHeader( "Content-disposition ", "attachment