社区
C#
帖子详情
winform 怎么下载保存在数据库中的文件
KevinHu_CS
2009-05-26 02:22:45
winform 怎么下载保存在数据库中的文件
请高手指点
...全文
367
9
打赏
收藏
winform 怎么下载保存在数据库中的文件
winform 怎么下载保存在数据库中的文件 请高手指点
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
9 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
xingn
2009-06-01
打赏
举报
回复
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.FileName = fname;
saveFileDialog1.Filter = "All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 0;
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.OverwritePrompt = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
if (sID != "")
{
try
{
string localFilePath = saveFileDialog1.FileName.ToString();
myStream.Close(); //先关闭stream,不然报错"正由另一进程使用"
if (File.Exists(localFilePath))
{
File.Delete(localFilePath);
}
FileStream fileStream = new FileStream(localFilePath, FileMode.CreateNew);
BinaryWriter binWirter = new BinaryWriter(fileStream);
binWirter.Write(FileData, 0, FileData.Length);
binWirter.Close();
fileStream.Close();
System.Diagnostics.Process.Start(localFilePath);
MessageBox.Show("文件下载成功!", "系统提示");
}
catch
{
MessageBox.Show("文件下载出错,请重试..", "系统提示");
}
}
}
}
KevinHu_CS
2009-05-31
打赏
举报
回复
up
vision_n
2009-05-31
打赏
举报
回复
ding
yingzhilian2008
2009-05-31
打赏
举报
回复
ding
happyer_longlong
2009-05-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'为文件名) ”问题了。
资料引用:http://www.knowsky.com/345292.html
lxlongnw
2009-05-26
打赏
举报
回复
//SQL语句代码
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.AppSettings["SqlConStr"].ToString();
public byte[] sql3(string a)
{
byte[] sp;
string strSql;
strSql = "select FJNR from tblFJ where id='"+a+"'";
SqlCommand cmd = new SqlCommand(strSql, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
sp = (byte[])reader["FJNR"];
reader.Close();
con.Close();
return sp;
}
//C#代码
private void button2_Click(object sender, EventArgs e)
{
string a = textBox1.Text.Trim(); //获取指定ID
if (a != "")
{
string aa = @"d:\" + textBox2.Text; //指定路径,textBox2.Text为文件名称,包括后缀名
if (File.Exists(aa)) //文件不存在时先下载
{
File.Delete(aa);
}
byte[] sp = ws.sql3(a);
FileStream fileStream = new FileStream(aa, FileMode.CreateNew);
BinaryWriter binWriter = new BinaryWriter(fileStream);
binWriter.Write(sp, 0, sp.Length);
binWriter.Close();
fileStream.Close();
System.Diagnostics.Process.Start(aa);
}
}
evaa006
2009-05-26
打赏
举报
回复
[Quote=引用 1 楼 Sysping1 的回复:]
数据库字段是个字节组byte[]
读出来后用SYSTEM.IO.File去创建文件,加入byte[],保存文件
[/Quote]
对,序列化后再保存
zgke
2009-05-26
打赏
举报
回复
byte[] _FileBytes =(byte[])DataTable.Rows[?][?];
File.WriteAllBytes("需要保存的目标文件",_FileBytes);
Sysping1
2009-05-26
打赏
举报
回复
数据库字段是个字节组byte[]
读出来后用SYSTEM.IO.File去创建文件,加入byte[],保存文件
C#
winform
小程序,
数据库
保存
图片,图片显示、修改、加边框
在本文
中
,我们将深入探讨如何使用C#
Winform
开发一个小程序,实现
数据库
中
保存
图片,以及在界面上显示、修改和为图片添加边框的功能。
Winform
是.NET框架
中
的一个强大的用户界面工具,用于创建桌面应用程序。我们将...
WinForm
操作SQLite
数据库
对于SQLite,连接字符串通常很简单,只需指定
数据库
文件
的完整路径即可。 6. **CRUD操作**:创建(Create)、读取(Retrieve)、更新(Update)和删除/Delete)是
数据库
操作的基本功能。在`DataAccess.cs`
中
,可能会有对应...
C#
WINFORM
操作Sql Server
数据库
,xls csv txt 导入导出
本教程将重点关注如何在C#的
WinForm
应用
中
操作SQL Server
数据库
,并实现Excel(xls)、CSV和TXT
文件
的导入导出功能。这在数据处理、报表生成以及数据交换等场景
中
非常实用。 首先,要与SQL Server进行交互,你需要...
如何将图片或其它
文件
保存
到
数据库
中
(C#)
BLOB类型允许我们直接在
数据库
中
存储二进制数据,而
文件
流存储则倾向于将
文件
存储在
文件
系统
中
,并在
数据库
中
保存
文件
路径。 ### 使用BLOB存储图片或
文件
1. **创建
数据库
表**: 在SQL Server
中
,可以创建一个包含...
C#
winform
上传
下载
文件
(附源码)
通过结合这两者,我们可以创建一个简单的应用,允许用户输入
文件
下载
链接并选择
保存
位置。 1. **C#
WinForm
基础** 在开始编写代码之前,我们需要了解
WinForm
的基本元素,如Form、TextBox、Button等控件。Form是...
C#
111,092
社区成员
642,554
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章