用ado.net对word,excel进行存取
来源: 作者: 出处:综艺读书 2006-03-06blob表
3 id int 4 0
0 name char 50 1
0 blob image 16 1
0 type char 60 1
saveFile.aspx.cs
|
|
|||
private void Button1_Click(object sender, System.EventArgs e)
{
Stream imgdatastream = File1.PostedFile.InputStream;
int imgdatalen = File1.PostedFile.ContentLength;
string imgtype = File1.PostedFile.ContentType;
string name = this.getFileNameByURL(this.File1.PostedFile.FileName);
byte[] imgdata = new byte[imgdatalen];
int n = imgdatastream.Read(imgdata,0,imgdatalen);
string connstr = "workstation id=OVERMIND;packet size=4096;user id=sa;password=sa;data source=OVERMIND;persist security info=False;initial catalog=wztj";
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand("INSERT INTO blob(name,type,blob) VALUES ( @imgtitle, @type,@blob )", connection );
SqlParameter paramTitle = new SqlParameter("@imgtitle", SqlDbType.VarChar,50 );
paramTitle.Value = name;
command.Parameters.Add(paramTitle);
SqlParameter paramData = new SqlParameter( "@blob", SqlDbType.Image );
paramData.Value = imgdata;
command.Parameters.Add( paramData );
SqlParameter paramType = new SqlParameter( "@type", SqlDbType.VarChar,50 );
paramType.Value = imgtype;
command.Parameters.Add( paramType );
wztj.debug.TestSQL.TraceErrorSql("INSERT INTO blob(name,type,blob) VALUES ( @imgtitle, @type,@blob )",command.Parameters);
connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
connection.Close();
}
listFile.aspx//这个东西主要用来列表,把已经有的东西列出来
<asp:HyperLinkColumn DataNavigateUrlField="id" HeaderText="产品名称" DataNavigateUrlFormatString="./getFile.aspx?ID={0}" DataTextField="name" DataTextFormatString="{0}" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="160px">
listFile.aspx.cs
string connstr="workstation id=OVERMIND;packet size=4096;user id=sa;password=sa;data source=OVERMIND;persist security info=False;initial catalog=wztj";
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand("select * from blob", connection );
connection.Open();
SqlDataAdapter adaptor = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adaptor.Fill(ds,"blob");
connection.Close();
this.DataGrid1.DataSource=ds.Tables["blob"].DefaultView;
this.DataGrid1.DataBind();
getFile.aspx.cs//这个文件比较重要负责把村道数据库里面的文件,按照格式,按照名称,给传输出来
private void Page_Load(object sender, System.EventArgs e)
{
string imgid =this.Request.QueryString.Get("ID");
//Request.QueryString["imgid"];
string connstr="workstation id=OVERMIND;packet size=4096;user id=sa;password=sa;data source=OVERMIND;persist security info=False;initial catalog=wztj";
string sql="SELECT name,blob, type FROM blob WHERE id = " + imgid;
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if(dr.Read())
{
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
//Response.ContentType = "application/ms-word";//设置输出文件类型为word文件。
Response.ContentType = dr["type"].ToString();
Response.BinaryWrite( (byte[]) dr["blob"] );
string FileName = dr["name"].ToString().Trim();
FileName=System.Web.HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8 );
Response.AppendHeader("Content-Disposition", "attachment;filename="+FileName);
}
connection.Close();
}
这里要说的有两点,第一,就是把文件的名称getFile.aspx变成我们想要的名称。
Response.AppendHeader("Content-Disposition", "attachment;filename="+FileName);
第二,就是把指定的名称变成我们想要的值,是标准的中文,而不是中文的乱码。
FileName=System.Web.HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8 );
·.NET移动与嵌入式技术 (4902篇文章)
·.NET开发手册 (4696篇文章)
·ADO和ADO.NET的比较和迁移 (179篇文章)
·ADO.NET与其它语言比较篇 (179篇文章)
·.NET与数据库 (388篇文章)
·Excel快速计算、统计混合编班考试成绩表 (0次浏览)
·在Excel中将文本转换为数字的七种方法 (0次浏览)
·把Excel里的一列数据转成多列数据 (0次浏览)
·把Excel里的一列数据快速转成多列数据 (0次浏览)
·Excel单元格中正确显示身份证号的技巧 (36次浏览)
·八种常见Excel错误提示及问题解决方法 (0次浏览)
·Excel快速计算、统计混合编班考试成绩表 (0次浏览)
·在Excel中将文本转换为数字的七种方法 (0次浏览)
·把Excel里的一列数据转成多列数据 (0次浏览)
·把Excel里的一列数据快速转成多列数据 (0次浏览)
·Excel中输入分数的六种方法 (625次浏览)
·Excel 2007轻松进行多条件求和 (474次浏览)
·超级技巧:在Excel中快速添加间隔空行 (346次浏览)
·在Excel工作表单元格中手动输入各种数据 (287次浏览)
·对多个Excel表中的数据进行合并计算 (242次浏览)
·用Excel函数快速统计学生期中考试成绩 (237次浏览)
·更改多个Excel文档数据合并计算 (173次浏览)
·Excel中特定单元格或区域的选定方法 (169次浏览)
·更改多个Excel工作表的数据合并计算 (137次浏览)
·Excel快速计算、统计混合编班考试成绩表 06-22
·在Excel中将文本转换为数字的七种方法 06-20
·把Excel里的一列数据转成多列数据 06-15
·把Excel里的一列数据快速转成多列数据 06-15
·Excel单元格中正确显示身份证号的技巧 06-03
·简单四招 让你使用Excel 轻松提速 05-28
·您不知道的Microsoft Excel历史版本 05-22
·表格中的霸王:微软Excel历史版本图赏 05-22
·在Excel 2007中添加、编辑或删除批注 05-17
|
|||
| ·ACDSEE专题教程-下载使用 ·迅雷专题教程-下载使用 ·Windows XP频道 ·Windows Vista频道 ·Windows 2000频道 ·win2003频道 ·Freebsd频道 ·Oracle频道 |
·Linux频道 ·Windows频道 ·邮件服务器专题 ·协议大全 ·数据恢复指南教程 ·FreeBSD使用教程 ·Linux数据库宝典 ·Linux基础知识 |
||
| · 秘密:Vista隐蔽的动态屏保 · 腾讯开发新电子宠物--QQ熊 · 惠普否认2999元PC有价无货 |
· 驱逐Win系统“流氓”文件 · WinXP中获取未使用的IP地址 · 尝试format C:格式化硬盘? |
| · 在DOS下恢复回收站中的文件 · 拯救WinXP崩溃的救命稻草 · Linux系统中超级权限的应用 |
· 搜狗PK谷歌:谁能代言拼... · 昨日重现,一键GHOST轻松.. · 实现Web迅雷在空闲时杀毒 |
| · AVIFile函数制做AVI文件 · VC中链接动态链接库的方法 · 熊猫烧香核心源码(Delphi) |
· DateDiff函数祥解 · JavaScript去除空格的三种 · js效果 图片加载进度实时.. |
| · SQL Server数据库优化方案 · Oracle的初学者入门心得 · JSP连接Mysql数据库 |
· Photoshop为美女做艺术处理 · 用Freehand创建发光字特效 · 设计自己的个性QQ动态表情 |




