问题描述:将图片、二进制文件内容等数据存储在数据库中,并能从数据库中取出还原为图片或文件,数据库存储二进制数据用varbinary字段。

分析:由于之前数据库中没有用过varbinary存储数据,首先要把varbinary搞懂了,其次就是图片类型与二进制类型之间的转换,文件类型与二进制类型之间的转换。

准备工作:

    1.varbinary 与 binary的区别:     

       固定长度 (binary) 的或可变长度 (varbinary) 的 binary 数据类型。

       binary [ ( n ) ]

固定长度的 n 个字节二进制数据。N 必须从 1 到 8,000。存储空间大小为 n+4 字节。

varbinary [ ( n ) ]

n 个字节变长二进制数据。n 必须从 1 到 8,000。存储空间大小为实际输入数据长度 +4 个字节,而不是 n 个字节。输入的数据长度可能为 0 字节。在 SQL-92 中 varbinary 的同义词为 binary varying。

注释:如果在数据定义或变量声明语句中没有指定 n,默认长度为 1。如果没有用 CAST 函数指定 n,默认长度为 30。

    2.由于使用了MVC结构,生成Model类的时候竟然不知道其对应类型,网上搜也没有结果,首先用了一个System.Data.SqlTypes.SqlBytes,后来误打误撞才发现竟然就是byte[]类型。

实现示例:

//1.首先将图片装换成字节数组
Bitmap btm = new Bitmap("C:/Users/Desktop/1.jpg");
MemoryStream ms = new MemoryStream();
btm.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] bytes = ms.GetBuffer();
//byte[] bytes= ms.ToArray(); 这两句都可以
ms.Close(); try{
//2.往数据库里写图片数据
//先打开两个类库文件
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=.;database=Test;uid=sa;pwd=123456";
con.Open();
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.Text;
com.CommandText = "insert into Map(Id,BitmapData) values(1,@photo)";
com.Parameters.AddWithValue("@photo", bytes);
SqlDataReader dr = com.ExecuteReader();//执行SQL语句
dr.Close();//关闭执行
con.Close();//关闭数据库 //3.从数据库中取图片数据并显示
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=.;database=Test;uid=sa;pwd=123456";
String sql = "select * from Map where Id=1";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.Read()){
if (!dr.IsDBNull())//防止照片字段为空
{
System.Data.SqlTypes.SqlBytes dataBytes = dr.GetSqlBytes();
Image imge= Image.FromStream(dataBytes.Stream);//显示照片
pb.ImageLocation = null;
pb.Image = null;
pb.Image = imge;
}
}
}
con.Close();//关闭数据库 }
catch (Exception)
{ throw;
}

上述为测试示例,没有直接取byte[]类型,如果要转化成byte[]类型,可以用如下方法:

byte[] b=dr.GetSqlBytes(1).Value;
    byte[] b1 = dr.GetSqlBytes(1).Buffer;

数据库中用varbinary存储二进制数据的更多相关文章

  1. mssql sqlserver 可以存储二进制数据的字段类型详解

    转自: http://www.maomao365.com/?p=6738 摘要: 下文将从数据库的数据类型着手,剖析在sqlserver数据库中可以存储二进制数据的数据类型,如下所示: mssql s ...

  2. mysql 存储二进制数据

    晚上小研究了下MySQL存储于读取二进制数据的功能.关键步骤为以下三点: 最重要的一点:存储二进制数据的表的类型需要是blob类型(按长度不同分为tiny, media, long) 插入二进制数据时 ...

  3. mongodb存储二进制数据

    mongodb 3.x存储二进制数据并不是以base64的方式,虽然在mongo客户端的查询结果以base64方式显示,请放心使用.下面来分析存储文件的存储内容.base64编码数据会增长1/3成为顾 ...

  4. python django中使用sqlite3数据库 存储二进制数据ByteArray

    在python中使用sqlite3数据库存储二进制流数据ByteArray,在django使用sqlite3数据库时,有时候也要注意最好使用二进制流ByteArray插入字符串. 使用ByteArra ...

  5. 讨论贴:Sqlserver varbinary 是二进制数据,却是十六进制的表现形式

    首先创建一个数据表 CREATE TABLE [dbo].[log_info]( [id] [,) NOT NULL, [info] [varchar]() NULL, [info1] [varbin ...

  6. mongodb存储二进制数据的二种方式——binary bson或gridfs

    python 版本为2.7 mongodb版本2.6.5 使用mongodb存储文件,可以使用两种方式,一种是像存储普通数据那样,将文件转化为二进制数据存入mongodb,另一种使用gridfs,咱们 ...

  7. BLOB:大数据,大对象,在数据库中用来存储超长文本的数据,例如图片等

    将一张图片存储在mysql中,并读取出来(BLOB数据:插入BLOB类型的数据必须使用PreparedStatement,因为插入BLOB类型的数据无法使用字符串拼写): -------------- ...

  8. pg 和sql server 分别如何新建二进制数据库字段以及插入二进制数据的sql语句

    PG create table demo ( id int, name bytea ); Insert into demo (id,name)values(256,pg_read_binary_fil ...

  9. BLOB类型的字段用于存储二进制数据

    MySQL中,BLOB是个类型系列,包括:TinyBlob.Blob.MediumBlob.LongBlob,这几个类型之间的唯一区别是在存储文件的最大大小上不同. MySQL的四种BLOB类型类型 ...

随机推荐

  1. Palindrome Pairs

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  2. PostrgreSQL 表名大小些问题(public."tablename")

    问题: 今天做表查询的时候,发现用以前的代码查询出现问题,提示说表名不存在. 现象: 通过PostrgreSQL客户端查询,发现出问题的表的查询语句如下: SELECT * FROM public.& ...

  3. java项目中build path的设置

    右键点击项目新建文件libs 添加jtds  jar包引用本地动态链接库(dll)的设置方法 配置LibraryJRE的添加和更换  Java项目中build path的设置总结,包括JRE的添加和更 ...

  4. The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name

    可以强迫部署EntityFramework.SqlServer.dll这个文件到输出目录 找到1个老外的帖子,戳这里(本人测试无效,大家有可能试一下..) 解决方案以下: 在EF的上下文代码CS文件( ...

  5. 记一次TFS 的 垃圾提示(无法下载 未获取项目 的 代码)

    提示 “ 所有文件都是最新的 ”,但是在 源码管理 里面 确是 “未下载” 我艹,第一次遇到.如图.~~ 最后发现是 TFS 的项目权限设置问题. 你妈个马批的,啥子鸡巴破B提示,太阳你妈B 的 .要 ...

  6. 给angularJs的service建模

    先回顾一下我们遇到的问题: 通过一个dialogService创建对话框,并将该service的参数数据通过resolve的方式传递给对话框的controller. controller解析数据后放置 ...

  7. c++ 课堂作业(1)

    一.题目 Create a program that asks for the radius of a circle and prints the area of that circle, using ...

  8. ActionScript 3.0 自写类整理笔记(十三)——Random类

    一个简单的随机函数工具类,总共提供了9种静态方法来获取不同的随机值随便写的,如果你还有什么更好的建议,请提出来,谢谢~ index.Random类:代码:public final class Rand ...

  9. error while performing database login with the xxx driver

    在MyEclipse的安装路径下D:\Program Files\MyEclipse 6.0\eclipse下面找到eclipse.ini文件,用记事本打开 eclipse.ini文件 -showsp ...

  10. Map中的entry

    是java中的一个对象,一般可以通过map.entrySet()得到.1,entrySet实现了Set接口,里面存放的是键值对.一个K对应一个V.2,用来遍历map的一种方法.Set<Map.E ...