首先,介绍一下保存文件到数据库中。
将文件保存到数据库中,实际上是将文件转换成二进制流后,将二进制流保存到数据库相应的字段中。在SQL Server中该字段的数据类型是Image,在Access中该字段的数据类型是OLE对象。

[复制此代码]CODE:

//保存文件到SQL Server数据库中
FileInfo fi=new FileInfo(fileName);
FileStream fs=fi.OpenRead();
byte[] bytes=new byte[fs.Length];
fs.Read(bytes,0,Convert.ToInt32(fs.Length));
SqlCommand cm=new SqlCommand();
cm.Connection=cn;
cm.CommandType=CommandType.Text;
if(cn.State==0) cn.Open();
cm.CommandText="insert into "+tableName+"("+fieldName+") values(@file)";
SqlParameter spFile=new SqlParameter("@file",SqlDbType.Image);
spFile.Value=bytes;
cm.Parameters.Add(spFile);
cm.ExecuteNonQuery()
//保存文件到Access数据库中
FileInfo fi=new FileInfo(fileName);
FileStream fs=fi.OpenRead();
byte[] bytes=new byte[fs.Length];
fs.Read(bytes,0,Convert.ToInt32(fs.Length));
OleDbCommand cm=new OleDbCommand();
cm.Connection=cn;
cm.CommandType=CommandType.Text;
if(cn.State==0) cn.Open();
cm.CommandText="insert into "+tableName+"("+fieldName+") values(@file)";
OleDbParameter spFile=new OleDbParameter("@file",OleDbType.Binary);
spFile.Value=bytes;
cm.Parameters.Add(spFile);
cm.ExecuteNonQuery()
//保存客户端文件到数据库
sql="update t_mail set attachfilename=@attachfilename,attachfile=@attachfile where mailid="+mailid;
myCommand = new SqlCommand(sql, new SqlConnection(ConnStr));
string path = fl_name.PostedFile.FileName;
string filename=path.Substring(path.LastIndexOf("\\")+1,path.Length-path.LastIndexOf("\\")-1);
myCommand.Parameters.Add("@attachfilename",SqlDbType.VarChar);
myCommand.Parameters["@attachfilename"].Value=filename;
myCommand.Parameters.Add("@attachfile",SqlDbType.Image);
Stream fileStream = fl_name.PostedFile.InputStream;
int intFileSize = fl_name.PostedFile.ContentLength;
byte[] fileContent = new byte[intFileSize];
int intStatus = fileStream.Read(fileContent,0,intFileSize); //文件读取到fileContent数组中
myCommand.Parameters["@attachfile"].Value=((byte[])fileContent);
fileStream.Close();
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();

代码中的fileName是文件的完整名称,tableName是要操作的表名称,fieldName是要保存文件的字段名称。
两段代码实际上是一样的,只是操作的数据库不同,使用的对象不同而已。
接着,在说说将文件从数据库中读取出来,只介绍从SQL Server中读取。

[复制此代码]CODE:

SqlDataReader dr=null;
SqlConnection objCn=new SqlConnection();
objCn.ConnectionString="Data Source=(local);User ID=sa;Password=;Initial Catalog=Test";
SqlCommand cm=new SqlCommand();
cm.Connection=cn;
cm.CommandType=CommandType.Text;
cm.CommandText="select "+fieldName+" from "+tableName+" where ID=1";
dr=cm.ExecuteReader();
byte[] File=null;
if(dr.Read())
{
File=(byte[])dr[0];
}
FileStream fs;
FileInfo fi=new System.IO.FileInfo(fileName);
fs=fi.OpenWrite();
fs.Write(File,0,File.Length);
fs.Close();

上面的代码是将保存在数据库中的文件读取出来并保存文fileName指定的文件中。
在使用上面的代码时,别忘了添加System.Data.SqlClient和System.IO引用。
修改:
将读文件的下面部分的代码

[复制此代码]CODE:

FileStream fs;
FileInfo fi=new System.IO.FileInfo(fileName);
fs=fi.OpenWrite();
fs.Write(File,0,File.Length);
fs.Close();
修改为
FileStream fs=new FileStream(fileName,FileMode.CreateNew);
BinaryWriter bw=new BinaryWriter(fs);
bw.Write(File,0,File.Length);
bw.Close();
fs.Close(); 

转载于:https://www.cnblogs.com/colder/archive/2012/07/06/2579047.html

C# 文件保存到数据库中或者从数据库中读取文件相关推荐

  1. spark 读取ftp_scala – 使用ftp在Apache Spark中的远程计算机上读取文件

    我正在尝试使用ftp在Apache Spark( Scala版本)中的远程计算机上读取文件.目前,我在 GitHub上关注Databricks的Learning Spark回购中的一个例子.使用cur ...

  2. Node中同步与异步的方式读取文件

    场景 Node.js最大的特点就是异步式I/O(或者非阻塞I/O)与事件紧密结合的编程模式.这种模式与传统的同步式I/O线性的编程思路有很大的不同,因为控制流很大程度上要靠事件和回调函数来组织,一个逻 ...

  3. 在EXCEL中通过VBA实现批量读取文件夹内的文件名称

    需求:有时候需要批量读取某个文件夹中的文件名称,有的程序只能读取同类型的,当文件为不同类型时就无法读取,因此需要写一个VBA程序,实现批量读取同一文件夹内的所有文件名称,且不需要修改程序就可以自由选择 ...

  4. java中相对路径怎么写_java中如何使用相对路径读取文件

    java中使用相对路径读取文件的方法:1.使用文件[File file = new File("src/test.txt")]方法:2.使用类的相对路径:3.使用当前线程的类加载器 ...

  5. c语言中如何读取文件的内容,急急急!!!如何读取文件中的相关内容

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 c语言下午就要交了,急死人了!!! 在读取文件时,我需要输入一个名字,然后显示名字相关内容,如: Orlando Magic 姓名 球队 出场数 时间 得 ...

  6. node 修改html文件路径,好程序员前端教程-nodejs如何读取文件夹目录的内容

    好程序员前端教程-nodejs如何读取文件夹目录的内容? 首先,nodejs中文件,目录的操作,我们对fs文件系统分为两类操作,第一类是xxx方法,第二类是xxxSync方法.所有的fs操作几乎都是这 ...

  7. java文件保存异常_Java 实现把异常信息写入到文件中

    示例代码如下: import java.io.File; import java.io.FileNotFoundException; import java.io.PrintStream; publi ...

  8. c++ 按行读取txt文件并赋值_python操作txt文件中数据教程[3]python读取文件夹中所有txt文件并将数据转为csv文件...

    觉得有用的话,请点击右下角 推荐给更多小伙伴 neoken_xuAsurada2015Evacloud 参考文献 python 操作 txt 文件中数据教程[1]-使用 python 读写 txt 文 ...

  9. c#mvc上传文件保存到后台_C# MVC实现前端上传文件保存到服务器

    后台代码 public string UploadECCInvoice_Intranet() { //返回前端结果状态对象 StateClass sc = new StateClass(); int ...

  10. Qt笔记-Qt中Json存二进制文件并读取文件(QJsonObject、QJsonArray、QJsonDocument的使用)

    程序运行截图如下: 文件如下: 源码如下: #include <QCoreApplication> #include <QJsonDocument> #include < ...

最新文章

  1. 如何实现一个定时的任务,并且可以自己停止
  2. adobe怎么统计字数_本科毕业论文怎么写(正文写作要点精华)
  3. 去掉windows console application的dos显示
  4. 上银伺服驱动器接线图_伺服驱动器实际接线方法详解
  5. 在Excel中制作复合饼图
  6. html5 fc,HTML5_mob604756fc093d的技术博客_51CTO博客
  7. 漫步数理统计十八——相关系数
  8. jaccard相似度_推荐系统中常用计算相似度的方法和工具
  9. DOM 事件深入浅出(一)
  10. imageview获取宽高
  11. s2结业项目营业网点查询_论文发表完成科研项目的材料
  12. 2018青岛ICPC ZOJ 4061: Magic Multiplication(模拟)
  13. Oracle使用NLSSORT函数实现汉字的排序
  14. sql中update多表处理
  15. 添加 identity impersonate=true userName=username password=password/,解决问题!
  16. Python程序猿必备的几款软件
  17. h5 app跳转客服咨询 临时会话 (没有开通在线咨询、无法会话)
  18. 4月第1周榜单丨飞瓜数据B站UP主排行榜(哔哩哔哩平台)发布!
  19. dicom是指_DCM是什么文件
  20. [UE5蓝图基础一]13.类似”人类一败涂地”掉落一定距离会回到空中 最终着落点还是设定地形上

热门文章

  1. CSS进阶(7)—— 内联元素的掌管者line-height和vertical-align(上)
  2. keepalived-1.2.2 编译出错问题解决
  3. dl,dt,dd,ul,li,ol区别
  4. IOS判断用户的网络类型(2/3/4G、wifi)
  5. [WPF Bug清单]之(6)——Button的IsCancel属性失效
  6. Android Service学习之本地服务
  7. C及C++中typedef的简单使用指南
  8. NEFU84——五指山(Exgcd)
  9. poj2456 Aggressive cows(二分查找)
  10. shell特殊符号cut命令 sort_wc_uniq命令 tee_tr_split命令 shell特殊符号