当前位置: 移动技术网 > IT编程>开发语言>.net > C# 文件保存到数据库中或者从数据库中读取文件

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

三对夫妻,泰州新闻综合频道,求一个网站你懂的网站

其实,方法非常的简单,只是可能由于这些朋友刚刚开始编程不久,一时没有找到方法而已。
下面介绍一下使用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();

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网