如何存图片到数据库中
2015-09-11 14:21
507 查看
将图片以二进制形式存入数据库时,首先要在数据库中建立一张表,将存储图片的字段类型设为Image类型,用FileStream类、BinaryReader把图片读成字节的形式,赋给一个字节数组,然后用ADO.SqlCommand对象的ExecuteNonQuery()方法来把数据保存到数据库中。主要代码如下:
private void button1_Clickobject sender, EventArgs e) { openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP"; if(openFileDialog1.ShowDialog()==DialogResult.OK) { string fullpath =openFileDialog1.FileName;//文件路径 FileStream fs = new FileStream(fullpath, FileMode.Open); byte[] imagebytes =new byte[fs.Length]; BinaryReader br = new BinaryReader(fs); imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length)); //数据库连接 SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=;database=db_05"); con.Open(); SqlCommand com = new SqlCommand("insert into tb_08 values(@ImageList)",con); com.Parameters.Add("ImageList", SqlDbType.Image); com.Parameters["ImageList"].Value = imagebytes; com.ExecuteNonQuery(); con.Close(); } }
相关文章推荐
- mysql的collation
- MySQL manager or server PID file could not be found!
- 分析函数lead()over(),lag()over()
- Mysql慢查询日志
- SQL Server基础知识
- SQL查询,排除指定字段
- [Oracle]使用InstantClient访问Oracle数据库
- 枚举法计算全年周内星期几的次数
- java 操作oracle 数据库,按Time排序 ,取前10条
- SQL Server 2005 创建分区表
- oracle 字符串分割
- 【linq to sql学习笔记】查询(上)
- mysql 实现oracle里面row_number() OVER (PARTITION BY cid,author ORDER BY id DESC) 的方法
- MongoDB的地埋空间数据存储、空间索引以及空间查询
- mysql 性能分析及explain分析
- SQL 增加或删除一列
- mysql 按年度、季度、月度、周、日SQL统计查询
- 理解collate Chinese_PRC_CI_AS ,请看实例:sql语句中如何识别字符串中的中文逗号的问题
- springMVC+MyBatis+Oracle+Web实现增删改查(附带完整案例+数据库数据)
- oracle表锁定与解锁