今早有个网友问到我这问题,以前我都是直接在数据库中存文件名的,还没有试过存储整张图片到数据库中,上网搜索了一下,自己又测试了一番,代码如下:
代码
代码
建立保存图片的表的SQL语句:
![](https://yqfile.alicdn.com/img_1c53668bcee393edac0d7b3b3daff1ae.gif?x-oss-process=image/resize,w_1400/format,webp)
![img_405b18b4b6584ae338e0f6ecaf736533.gif](https://yqfile.alicdn.com/img_405b18b4b6584ae338e0f6ecaf736533.gif?x-oss-process=image/resize,w_1400/format,webp)
USE
[
niunantest
]
GO
/* ***** 对象: Table [dbo].[picdata] 脚本日期: 03/30/2010 14:51:58 ***** */
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [ dbo ] . [ picdata ] (
[ id ] [ int ] IDENTITY ( 1 , 1 ) NOT NULL ,
[ content ] [ image ] NULL ,
[ createdate ] [ datetime ] NOT NULL CONSTRAINT [ DF_picdata_createdate ] DEFAULT ( getdate ()),
CONSTRAINT [ PK_picdata ] PRIMARY KEY CLUSTERED
(
[ id ] ASC
) WITH (PAD_INDEX = OFF , STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON [ PRIMARY ]
) ON [ PRIMARY ] TEXTIMAGE_ON [ PRIMARY ]
GO
/* ***** 对象: Table [dbo].[picdata] 脚本日期: 03/30/2010 14:51:58 ***** */
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [ dbo ] . [ picdata ] (
[ id ] [ int ] IDENTITY ( 1 , 1 ) NOT NULL ,
[ content ] [ image ] NULL ,
[ createdate ] [ datetime ] NOT NULL CONSTRAINT [ DF_picdata_createdate ] DEFAULT ( getdate ()),
CONSTRAINT [ PK_picdata ] PRIMARY KEY CLUSTERED
(
[ id ] ASC
) WITH (PAD_INDEX = OFF , STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON [ PRIMARY ]
) ON [ PRIMARY ] TEXTIMAGE_ON [ PRIMARY ]
下面是保存图片到数据库中的代码片段:
int
len
=
fu.PostedFile.ContentLength;
//
图片大小
byte [] pic = new byte [len]; // 创建一个字节数组,大小为图片的大小,数据库中就存储这个东西
fu.PostedFile.InputStream.Read(pic, 0 , len); // 把上传控件中的文件用二进制读取存到pic字节数组中
// 插入图片到数据库中
SqlConnection connection = new
SqlConnection( @" server=.\sqlexpress;database=niunantest;uid=sa;pwd=123456 " );
try
{
connection.Open();
SqlCommand cmd = new SqlCommand( " insert into picdata "
+ " ([content]) values (@pic) " , connection);
cmd.Parameters.Add( " @pic " , pic);
cmd.ExecuteNonQuery();
Label1.Text = " 图片插入数据库成功! " ;
Image1.ImageUrl = " getpic.ashx?t= " + DateTime.Now.Ticks; // 显示刚刚插入数据库的图片
}
finally
{
connection.Close();
}
byte [] pic = new byte [len]; // 创建一个字节数组,大小为图片的大小,数据库中就存储这个东西
fu.PostedFile.InputStream.Read(pic, 0 , len); // 把上传控件中的文件用二进制读取存到pic字节数组中
// 插入图片到数据库中
SqlConnection connection = new
SqlConnection( @" server=.\sqlexpress;database=niunantest;uid=sa;pwd=123456 " );
try
{
connection.Open();
SqlCommand cmd = new SqlCommand( " insert into picdata "
+ " ([content]) values (@pic) " , connection);
cmd.Parameters.Add( " @pic " , pic);
cmd.ExecuteNonQuery();
Label1.Text = " 图片插入数据库成功! " ;
Image1.ImageUrl = " getpic.ashx?t= " + DateTime.Now.Ticks; // 显示刚刚插入数据库的图片
}
finally
{
connection.Close();
}
下面是从数据库中取出图片的代码片段:
![](https://yqfile.alicdn.com/img_1c53668bcee393edac0d7b3b3daff1ae.gif?x-oss-process=image/resize,w_1400/format,webp)
![img_405b18b4b6584ae338e0f6ecaf736533.gif](https://yqfile.alicdn.com/img_405b18b4b6584ae338e0f6ecaf736533.gif?x-oss-process=image/resize,w_1400/format,webp)
MemoryStream stream
=
new
MemoryStream();
SqlConnection connection = new
SqlConnection( @" server=.\sqlexpress;database=niunantest;uid=sa;pwd=123456 " );
try
{
connection.Open();
SqlCommand command = new
SqlCommand( " select top 1 [content] from picdata order by id desc " , connection);
byte [] image = ( byte [])command.ExecuteScalar();
stream.Write(image, 0 , image.Length);
Bitmap bitmap = new Bitmap(stream);
context.Response.ContentType = " image/jpeg " ;
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
finally
{
connection.Close();
stream.Close();
}
SqlConnection connection = new
SqlConnection( @" server=.\sqlexpress;database=niunantest;uid=sa;pwd=123456 " );
try
{
connection.Open();
SqlCommand command = new
SqlCommand( " select top 1 [content] from picdata order by id desc " , connection);
byte [] image = ( byte [])command.ExecuteScalar();
stream.Write(image, 0 , image.Length);
Bitmap bitmap = new Bitmap(stream);
context.Response.ContentType = " image/jpeg " ;
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
finally
{
connection.Close();
stream.Close();
}
其实也就是通过流把图片搞成字节数组再存到数据库中,然后再从数据库中读取字节数组出来,再通过字节数组创建流,再通过流把图像输出出来,发现你存到数据库中的是gif图像的话再取出来是可以把他转为jpg的图像的,因为在取出图像的时候我们设置他的ContentType是image/jpeg了。
源码下载:http://niunan.net/download/picsave2db.7z