已有56人关注
我作了个网络维护记录窗体,有个上传附件功能,想实现上传word,excel,图片,文本等格式。那里有代码可参考一下呀?
发表在C#图书答疑 2010-07-26
是否精华
版块置顶:
我作了个网络维护记录窗体,有个上传附件功能,想实现上传word,excel,图片,文本文件等格式。哪里有代码可参考一下呀?
我查看到用二进制形式只能存图片和文本文件,word,excel就存不了。把文件路径保存到数据库,再把文件复制到程序目录下,又存在个问题,就是几个人用这这个程序,每人电脑上有个程序,就不行,要不就要开个文件夹共享出来,再用程序把文件拷到该文件夹中,以后就不好维护,想实现能同时把多种文件格式保存到数据库中还能从中读出来,该怎么操作呀,请老师指导一下,谢谢。
分享到:
精彩评论 2
小科_mrkj
学分:43 LV2
2010-07-27
沙发
读者朋友:
    您好,在项目文件夹下面新建一个文件夹,然后把要上传的文件上传到那个文件夹里面,数据库里面直接保存路径。
wufayou
学分:0 LV1
TA的每日心情
第五天
2023-03-18 21:24:17
2010-07-27
板凳
C#将文件保存到数据库中或者从数据库中读取文件
 
在编程中我们常常会遇到“将文件保存到数据库中”这样一个问题,虽然这已不是什么高难度的问题,但对于一些刚刚开始编程的朋友来说可能是有一点困难。其实,方法非常的简单,只是可能由于这些朋友刚刚开始编程不久,一时没有找到方法而已。
 
下面介绍一下使用C#来完成此项任务。
 
首先,介绍一下保存文件到数据库中。
将文件保存到数据库中,实际上是将文件转换成二进制流后,将二进制流保存到数据库相应的字段中。在SQL Server中该字段的数据类型是Image,在Access中该字段的数据类型是OLE对象。
 //保存文件到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中读取。

 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引用。


修改:
将读文件的下面部分的代码
 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();
这样修改后,就可以解决朋友们提出的“如果想从数据库中取出,另存为相应的文件时。如WORD文件另存为XXX.DOC(XXX为文件名) ”问题了。
首页上一页 1 下一页尾页 2 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照