SQL Server中的Image数据类型的操作
准备工作,在库Im_Test中建立一张表Im_Info,此表中有两个字段,分别为Pr_Id (INT),Pr_Info (IMAGE),用来存储图形编号及图形信息。其语法如下:
CREATE TEALE Im_Info (
Pr_Id INT NULL ,
Pr_Info IMAGE NULL
)
第一步: 往表中插入一条记录,并初始化PR_INFO字段。其语法如下:
INSERT INTO Im_Info VALUES (1 ,0xFFFFFFFF)
第二步往表中写入图形信息。其语法如下:
DECLARE @@ptrval varbinary(16)
SELECT @@ptrval = TEXTPTR(Pr_Info)
FROM Im_Info
WHERE Pr_Id = 1
WRITETEXT Im_Text.Im_Info
@@ptrval 0x624fd543fd…..
其中0x624fd543fd….. 为图形的十六进制数据,可以通过C 、Java等工具获得。
注意在写入图形信息前必须先将此数据库的 'select into/bulkcopy' 属性设置为 True ,其语法如下:
use master
exec sp_dboption Im_Test ,'select into/bulkcopy' ,True
C#读取Image数据类型:
(1)控制台应用程序下演示插入图片
public void InsertIMG()
{
//将需要存储的图片读取为数据流
FileStream fs = new FileStream(@"E:\c.jpg", FileMode.Open,FileAccess.Read);
Byte[] btye2 = new byte[fs.Length];
fs.Read(btye2 , 0, Convert.ToInt32(fs.Length));
fs.Close();
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "insert into T_Img(imgfile) values(@imgfile)";
SqlParameter par = new SqlParameter("@imgfile", SqlDbType.Image);
par.Value = bt;
cmd.Parameters.Add(par);
int t=(int)(cmd.ExecuteNonQuery());
if (t > 0)
{
Console.WriteLine("插入成功");
}
conn.Close();
}
}
(2)控制台应用程序下读出并生成图片到物理位置
public void Read()
{
byte[] MyData = new byte[0];
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from T_img";
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
MyData = (byte[])sdr["ImgFile"];//读取第一个图片的位流
int ArraySize= MyData.GetUpperBound(0);//获得数据库中存储的位流数组的维度上限,用作读取流的上限
FileStream fs = new FileStream(@"c:\00.jpg", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0, ArraySize);
fs.Close(); //-- 写入到c:\00.jpg。
conn.Close();
Console.WriteLine("读取成功");//查看硬盘上的文件
}
}
(3)Web下picshow.aspx页将图片读取出来并写入到浏览器上呈现
public void Read()
{
byte[] MyData = new byte[0];
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from T_img";
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
MyData = (byte[])sdr["ImgFile"];
Response.ContentType = "image/gif";
Response.BinaryWrite(MyData);
conn.Close();
Response.Write("读取成功");
}
(4)在web中可以如上picshow.aspx页面读取并显示图片,而真正引用该图片时如下示例
<img src="picshow.aspx" width="500" height="300" />
(5)Winform下将图片写入到sql数据库image类型字段中的方法和以上方法基本一致,仅区别于可以利用多个对话框来帮助选取存储图片等,各个属性可以方便的利用上
(6)Winform下读取图片在picturebox控件中显示出来
方法一:利用MemoryStream 和System.Drawing.Image
public void Read()
{
byte[] MyData = new byte[0];
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from T_img";
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
MyData = (byte[])sdr["ImgFile"];
MemoryStream mystream = new MemoryStream(MyData);
//用指定的数据流来创建一个image图片
System.Drawing.Image img = System.Drawing.Image.FromStream(mystream, true);
System.Windows.Forms.PictureBox picbox = new PictureBox();
picbox.Image = img;
picbox.Left = 30;
picbox.Top = 80;
picbox.Width = 800;
picbox.Height = 500;
this.Controls.Add(picbox);
mystream.Close();
conn.Close();
}
}
方法二:将流直接读取成图片并写入到物理位置,然后再行利用该图片呈现
void Read()
{
using (SqlConnection conn = new SqlConnection(sqlconnstr))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from T_img";
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
byte[] Image_img = (byte[])sdr["ImgFile"];
if (Image_img.Length == 0)
{
return;
}
int filelength = Image_img.Length;
string imageName = "1.jpg";
string myUrl = Environment.CurrentDirectory + "\\" + imageName;
FileStream fs = new FileStream(myUrl, FileMode.OpenOrCreate,FileAccess.Write);
BinaryWriter BW = new BinaryWriter(fs);
BW.BaseStream.Write(Image_img, 0, filelength);
BW.Flush();
BW.Close();
System.Windows.Forms.PictureBox picbox = new PictureBox();
//为picbox添加图片方法一
//picbox.ImageLocation = myUrl;
//picbox.Width = 800;
//picbox.Height = 300;
//为picbox添加图片方法二
Bitmap bitmap = new Bitmap(myUrl);
picbox.Width = 100;//bitmap.Width;
picbox.Height = 80;//bitmap.Height;
picbox.Image = (Image)bitmap;
picbox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
picbox.Left = 20;
picbox.Top = 30;
this.Controls.Add(picbox);
conn.Close();
}
}
SQL Server中的Image数据类型的操作相关推荐
- 了解SQL Server中的GUID数据类型
什么是GUID? (What is a GUID?) GUID is a 16 byte binary SQL Server data type that is globally unique acr ...
- Sql server中的Text数据类型
在什么情况下应用这个类型 [quote] 超过 8KB 的ASCII 数据可以使用Text数据类型存储.例如,因为 Html 文档全部都是 ASCII 字符,并且在一般情况下长度超过 8KB,所以这些 ...
- 从TXT文本文档向Sql Server中批量导入数据
因为工作的需要,近期在做数据的分析和数据的迁移.在做数据迁移的时候需要将原有的数据导入到新建的数据库中.本来这个单纯的数据导入导出是没有什么问题的,但是客户原有的数据全部都是存在.dat文件中的.所以 ...
- cte公用表表达式_CTE SQL删除; 在SQL Server中删除具有公用表表达式的数据时的注意事项
cte公用表表达式 In this article, the latest in our series on Common table expressions, we'll review CTE SQ ...
- 在SQL Server中使用architectureid
I attended a TDWI conference in May 2016 in Chicago. Here I got a hint about the datatype hierarchyi ...
- sql server中对xml进行操作
一.前言 SQL Server 2005 引入了一种称为 XML 的本机数据类型.用户可以创建这样的表,它在关系列之外还有一个或多个 XML 类型的列:此外,还允许带有变量和参数.为了更好地支持 XM ...
- SQL Server中Text和varchar(max)数据类型区别
SQL Server中Text和varchar(max)数据类型区别 以前只知道text和image是可能被SQL Server淘汰的数据类型,但具体原因不太清楚,今天读书的时候发现了text与var ...
- 在SQL Server中比较VARCHAR(max)与VARCHAR(n)数据类型
I have seen that SQL developers use varchar(max) data while designing the tables or temporary tables ...
- C# 学习笔记(18)操作SQL Server 中
C# 学习笔记(18)操作SQL Server 中 数据库基础操作 SQL语法可以参考 菜鸟教程 或者微软官方的SQL示例 注意SQL不区分大小写 查 1.基础查询 --最基础的查询语句, selec ...
最新文章
- Sharepoint学习笔记—ECMAScript对象模型系列-- 8、组与用户操作(一)
- 中国电声市场销售分析与投资竞争力研究报告2022版
- Android中使用Notification在状态栏上显示通知
- Netty源码学习(零)前言
- SQL查询单表数据之组合(三)
- cloudsim样例描述
- 集体智慧编程 - 优化
- bootstrap 栅格系统实现类似table跨行
- DB2 exception: Cannot create PoolableConnectionFactory SQLCODE=-142
- RJ45墙上网线插座的线序与接法
- 如何在线批量将JPG图片转Word文件
- 有趣的符号图画(颜文字)(I have a AC dream)(神兽护体)(保佑你次次Accepted)
- 交换机测试丢包软件,交换机丢包问题定位
- 2048小游戏编写思路
- pwd python 安装 模块_在windows上安装pwd模块时出错
- 前端好用的框架及工具
- Yii框架里的一些zii用法
- mPaaS 服务端核心组件:移动分析服务 MAS 架构解析
- 实践.原创 | 加速IPv6部署,建设网络强国 (普及知识)
- Java 集合深入理解(17):HashMap 在 JDK 1.8 后新增的红黑树结构
热门文章
- 临床医生如何解读Meta分析论文?
- 第18天学习Java的笔记-ArrayList
- idea中applicationContext-dao.xml文件中Cannot resolve file***** :spring xml model validation问题
- Windows下C/C++获取当前系统时间
- 工业用微型计算机(16)-指令系统(11)
- 【时间序列】NeuralProphet:Prophet的Pytorch实现!精度更高 预测更快 特性更多!...
- 曝!BAT大厂NLP学习进阶之法~
- 【小白学PyTorch】15.TF2实现一个简单的服装分类任务
- 【论文解读】基于图卷积的价格感知推荐
- 【Python基础】高效的10个Pandas函数,你都用过吗?