c# 将文件保存到数据库
来源:互联网 发布:淘宝bug反馈 编辑:程序博客网 时间:2024/05/10 13:24
最近论坛上好多人问到这个问题,其实这和保存图片到数据库一样的道理,
就是把文件转换成二进制byte数组,保存到数据库就可以了.
以Access数据库为例,数据表有两个字段,主键ID,和File字段类型为OLE对象.
private void SaveFile()
...{
Form1 frm = new Form1();
FileStream filestream = new FileStream(Application.StartupPath + "/a.txt", FileMode.Open, FileAccess.Read);
BinaryReader filerd = new BinaryReader(filestream,Encoding .Default );
byte[] filebyte = new byte[filestream.Length];
filerd.Read(filebyte, 0, (int)filestream.Length);
frm.OpenConn();
OleDbCommand comm = new OleDbCommand("insert into file (id,file) Values(@fid,@file) ", frm.dbconn );
comm.Parameters.AddWithValue("@fid", "0001");
comm.Parameters.AddWithValue("@file", DBNull.Value);
comm.Parameters["@file"].Value = filebyte;
comm.ExecuteNonQuery();
}
private void ReadFile()
...{
Form1 frm = new Form1();
frm.OpenConn();
OleDbCommand comm = new OleDbCommand("select * from file", frm.dbconn);
OleDbDataAdapter da = new OleDbDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds);
byte[] filebyte = (byte[])ds.Tables[0].Rows[0]["File"];
// System.Text.Encoding mycode = new System.Text.Encoding();
this.richTextBox1.AppendText(Encoding.Default.GetString(filebyte));
}
...{
Form1 frm = new Form1();
FileStream filestream = new FileStream(Application.StartupPath + "/a.txt", FileMode.Open, FileAccess.Read);
BinaryReader filerd = new BinaryReader(filestream,Encoding .Default );
byte[] filebyte = new byte[filestream.Length];
filerd.Read(filebyte, 0, (int)filestream.Length);
frm.OpenConn();
OleDbCommand comm = new OleDbCommand("insert into file (id,file) Values(@fid,@file) ", frm.dbconn );
comm.Parameters.AddWithValue("@fid", "0001");
comm.Parameters.AddWithValue("@file", DBNull.Value);
comm.Parameters["@file"].Value = filebyte;
comm.ExecuteNonQuery();
}
private void ReadFile()
...{
Form1 frm = new Form1();
frm.OpenConn();
OleDbCommand comm = new OleDbCommand("select * from file", frm.dbconn);
OleDbDataAdapter da = new OleDbDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds);
byte[] filebyte = (byte[])ds.Tables[0].Rows[0]["File"];
// System.Text.Encoding mycode = new System.Text.Encoding();
this.richTextBox1.AppendText(Encoding.Default.GetString(filebyte));
}
其中Form1 frm = new Form1();是因为我在Form1中已经定义了OleDbConnetion,没有必要再重新定义了.
将frm.OpenConn(); frm.dbconn 换成你自己的连接数据库代码就可以了.
如果是sql数据库,将字段该为image;是因为image字段可存储超过8000字节的容量.
- C#将文件保存到数据库中
- c# 将文件保存到数据库
- C#将文件保存到数据库中或者从数据库中读取文件
- C#将文件保存到数据库中或者从数据库中读取文件
- c#将文件保存到数据库中或者从数据库中读取文件
- C#将文件保存到数据库中或者从数据库中读取文件
- C#将文件保存到数据库中或者从数据库中读取文件
- C#将文件保存到数据库中或者从数据库中读取文件
- [转]C#将文件保存到数据库中或者从数据库中读取文件
- C#将文件保存到数据库中或者从数据库中读取文件
- C#将文件保存到数据库中或者从数据库中读取文件
- 将文件保存到数据库中
- 将文件保存到数据库中(stream)
- 将文件保存到数据库中
- 将文件保存到数据库中
- 将文件保存到数据库中
- 保存二进制 文件到数据库c#
- C#将文件上传、下载(以二进制流保存到数据库)
- MM与23个设计模式
- 解析VS.Net线程同步和异步调用
- SAP中对内表及表的操作
- C#命名规则和开发习惯
- ASP.NET 页间传值
- c# 将文件保存到数据库
- 《Windows Via C/C++》边学习,边翻译(五)操作字符和字符串-4
- C#索引器
- 目标-----开发人员要读的书(个人读书感悟1)
- Javascript中删除数组元素
- Linq究竟是否任何时候都用呢?
- 开博
- 解决eclipse不编译class文件之方法
- asp.net优化完全技巧