两种,
1、一种是将图片转化成二进制数据流存入数据库中;
2、一种是保存图片的路径,然后前台读取路径去调用图片;
特点:
相关的代码网络一下应该会有,第二种方法实现上比较简单,就是存储路径,然后根据路径读取对应的图片显示出来。第一种就比较麻烦,要先把图片转化成二进制数据,读取时就是从数据库读取对应数据再转化成图片显示出来。
图片存储缓存的话需要通过图片转化为数据流进行存放在数据库里面,调用的时候按照数据流找到图片存放路径转化出来就可以。
❷ 图片如何存入数据库
1、新建一个数据库,数据库名为Image,表名为image。并为表添加ID,tupian两个列。
❸ 如何将图片储存在Mysql数据库里
解决方法一般有两种:
1、将图片保存的路径存储到数据库;
2、将图片以二进制数据流的形式直接写入数据库字段中。
以下为具体方法:
一、保存图片的上传路径到数据库:
string
uppath="";//用于保存图片上传路径
//获取上传图片的文件名
string fileFullname =
this.FileUpload1.FileName;
//获取图片上传的时间,以时间作为图片的名字可以防止图片重名
string
dataName =
DateTime.Now.ToString("yyyyMMddhhmmss");
//获取图片的文件名(不含扩展名)
string
fileName = fileFullname.Substring(fileFullname.LastIndexOf("\") +
1);
//获取图片扩展名
string type =
fileFullname.Substring(fileFullname.LastIndexOf(".") +
1);
//判断是否为要求的格式
if (type == "bmp" || type == "jpg" || type == "jpeg"
|| type == "gif" || type == "JPG" || type == "JPEG" || type == "BMP" || type ==
"GIF")
{
//将图片上传到指定路径的文件夹
this.FileUpload1.SaveAs(Server.MapPath("~/upload")
+ "\" + dataName + "." +
type);
//将路径保存到变量,将该变量的值保存到数据库相应字段即可
uppath
= "~/upload/" + dataName + "." +
type;
}
二、将图片以二进制数据流直接保存到数据库:
引用如下命名空间:
using
System.Drawing;
using System.IO;
using
System.Data.SqlClient;
设计数据库时,表中相应的字段类型为iamge
保存:
//图片路径
string
strPath = this.FileUpload1.PostedFile.FileName.ToString
();
//读取图片
FileStream fs = new System.IO.FileStream(strPath,
FileMode.Open, FileAccess.Read);
BinaryReader br = new
BinaryReader(fs);
byte[] photo =
br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
//存入
SqlConnection
myConn = new SqlConnection("Data Source=.;Initial Catalog=stumanage;User
ID=sa;Password=123");
string strComm = " INSERT INTO
stuInfo(stuid,stuimage) VALUES(107,@photoBinary
)";//操作数据库语句根据需要修改
SqlCommand myComm = new SqlCommand(strComm,
myConn);
myComm.Parameters.Add("@photoBinary", SqlDbType.Binary,
photo.Length);
myComm.Parameters["@photoBinary"].Value =
photo;
myConn.Open();
if (myComm.ExecuteNonQuery() >
0)
{
this.Label1.Text =
"ok";
}
myConn.Close();
读取:
...连接数据库字符串省略
mycon.Open();
SqlCommand
command = new
SqlCommand("select stuimage from stuInfo where stuid=107",
mycon);//查询语句根据需要修改
byte[] image = (byte[])command.ExecuteScalar
();
//指定从数据库读取出来的图片的保存路径及名字
string strPath =
"~/Upload/zhangsan.JPG";
string strPhotoPath =
Server.MapPath(strPath);
//按上面的路径与名字保存图片文件
BinaryWriter bw = new
BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));
bw.Write(image);
bw.Close();
//显示图片
this.Image1.ImageUrl
= strPath;
采用这两种方式可以根据实际需求灵活选择。
❹ VS中怎么从数据库读取图片
publicDataSetds=null;
publicSqlDataAdaptersda=null;
publicstaticSqlConnectionconn=null;
//连接数据库
publicvoidOpenLink()
{
conn=newSqlConnection();
conn.ConnectionString="Server=192.168.1.5;uid=sa;pwd=555;dataBase=数据库名";
try
{
conn.Open();
}
catch
{
MessageBox.Show("连接数据库丢失!");
}
}
//查询数据表
publicvoidlink(Stringsql)
{
if(conn!=null)
{
ds=newDataSet();
sda=newSqlDataAdapter();
sda.SelectCommand=newSqlCommand(sql,conn);
SqlCommandBuilderbuilder=newSqlCommandBuilder(sda);
sda.Fill(ds);
}
}
publicvoidPix(PictureBoxpx,Stringname)//显示图片
{
Stringsql="Select识别码from下单表where订票人='"+name+"'";
byte[]imagebytes=null;
SqlCommandcom=newSqlCommand(sql,conn);
SqlDataReaderdr=com.ExecuteReader();
while(dr.Read())
{
imagebytes=(byte[])dr.GetValue(0);
}
MemoryStreamms=newMemoryStream(imagebytes);
Bitmapbmpt=newBitmap(ms);
px.Image=bmpt;
px.SizeMode=PictureBoxSizeMode.StretchImage;
}
放工程启动的时候就OpenLink(),把上面代码存储在类文件里面,Pix
()函数就是显示图片。
❺ 如何用insert语句把插入image类型记录到数据库
如果非要写sql语句如下,要将image转化为二进制
INSERT INTO tbl VALUES (1,0xFFFFFFFF)
注意在写入图形信息前必须先将此数据库的 'select into/bulk' 属性设置为 True ,其语法如下:
use master
exec sp_dboption Im_Test ,'select into/bulk' ,True
public void InsertIMG()
{
//将需要存储的图片读取为数据流
FileStream fs = new FileStream(@"D:\a.jpg", FileMode.Open,FileAccess.Read);
Byte[] b = new byte[fs.Length];
fs.Read(b , 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 tbl(imgfile) values(@imgfile)";
SqlParameter par = new SqlParameter("@imgfile", SqlDbType.Image);
par.Value = b;
cmd.Parameters.Add(par);
int t=(int)(cmd.ExecuteNonQuery());
if (t > 0)
{
Console.WriteLine("插入成功");
}
conn.Close();
}
}
❻ sql2005 image字段如何添加照片
C#读取Image数据类型:
(1)控制台应用程序下演示插入图片
publicvoidInsertIMG()
{
//将需要存储的图片读取为数据流
FileStreamfs=newFileStream(@"E:c.jpg",FileMode.Open,FileAccess.Read);
Byte[]btye2=newbyte[fs.Length];
fs.Read(btye2,0,Convert.ToInt32(fs.Length));
fs.Close();
using(SqlConnectionconn=newSqlConnection(sqlconnstr))
{
conn.Open();
SqlCommandcmd=newSqlCommand();
cmd.Connection=conn;
cmd.CommandText="insertintoT_Img(imgfile)values(@imgfile)";
SqlParameterpar=newSqlParameter("@imgfile",SqlDbType.Image);
par.Value=bt;
cmd.Parameters.Add(par);
intt=(int)(cmd.ExecuteNonQuery());
if(t>0)
{
Console.WriteLine("插入成功");
}
conn.Close();
}
}
(2)控制台应用程序下读出并生成图片到物理位置
publicvoidRead()
{
byte[]MyData=newbyte[0];
using(SqlConnectionconn=newSqlConnection(sqlconnstr))
{
conn.Open();
SqlCommandcmd=newSqlCommand();
cmd.Connection=conn;
cmd.CommandText="select*fromT_img";
SqlDataReadersdr=cmd.ExecuteReader();
sdr.Read();
MyData=(byte[])sdr["ImgFile"];//读取第一个图片的位流
intArraySize=MyData.GetUpperBound(0);//获得数据库中存储的位流数组的维度上限,用作读取流的上限
FileStreamfs=newFileStream(@"c: 0.jpg",FileMode.OpenOrCreate,FileAccess.Write);
fs.Write(MyData,0,ArraySize);
fs.Close();//--写入到c: 0.jpg。
conn.Close();
Console.WriteLine("读取成功");//查看硬盘上的文件
}
}
(3)Web下picshow.aspx页将图片读取出来并写入到浏览器上呈现
publicvoidRead()
{
byte[]MyData=newbyte[0];
using(SqlConnectionconn=newSqlConnection(sqlconnstr))
{
conn.Open();
SqlCommandcmd=newSqlCommand();
cmd.Connection=conn;
cmd.CommandText="select*fromT_img";
SqlDataReadersdr=cmd.ExecuteReader();
sdr.Read();
MyData=(byte[])sdr["ImgFile"];
Response.ContentType="image/gif";
Response.BinaryWrite(MyData);
conn.Close();
Response.Write("读取成功");
}
(4)在web中可以如上picshow.aspx页面读取并显示图片,而真正引用该图片时如下示例
<imgsrc="picshow.aspx"width="500"height="300"/>
(5)Winform下将图片写入到sql数据库image类型字段中的方法和以上方法基本一致,仅区别于可以利用多个对话框来帮助选取存储图片等,各个属性可以方便的利用上
(6)Winform下读取图片在picturebox控件中显示出来
方法一:利用MemoryStream和System.Drawing.Image
publicvoidRead()
{
byte[]MyData=newbyte[0];
using(SqlConnectionconn=newSqlConnection(sqlconnstr))
{
conn.Open();
SqlCommandcmd=newSqlCommand();
cmd.Connection=conn;
cmd.CommandText="select*fromT_img";
SqlDataReadersdr=cmd.ExecuteReader();
sdr.Read();
MyData=(byte[])sdr["ImgFile"];
MemoryStreammystream=newMemoryStream(MyData);
//用指定的数据流来创建一个image图片
System.Drawing.Imageimg=System.Drawing.Image.FromStream(mystream,true);
System.Windows.Forms.PictureBoxpicbox=newPictureBox();
picbox.Image=img;
picbox.Left=30;
picbox.Top=80;
picbox.Width=800;
picbox.Height=500;
this.Controls.Add(picbox);
mystream.Close();
conn.Close();
}
}
方法二:将流直接读取成图片并写入到物理位置,然后再行利用该图片呈现
voidRead()
{
using(SqlConnectionconn=newSqlConnection(sqlconnstr))
{
conn.Open();
SqlCommandcmd=newSqlCommand();
cmd.Connection=conn;
cmd.CommandText="select*fromT_img";
SqlDataReadersdr=cmd.ExecuteReader();
sdr.Read();
byte[]Image_img=(byte[])sdr["ImgFile"];
if(Image_img.Length==0)
{
return;
}
intfilelength=Image_img.Length;
stringimageName="1.jpg";
stringmyUrl=Environment.CurrentDirectory+"\"+imageName;
FileStreamfs=newFileStream(myUrl,FileMode.OpenOrCreate,FileAccess.Write);
BinaryWriterBW=newBinaryWriter(fs);
BW.BaseStream.Write(Image_img,0,filelength);
BW.Flush();
BW.Close();
System.Windows.Forms.PictureBoxpicbox=newPictureBox();
//为picbox添加图片方法一
//picbox.ImageLocation=myUrl;
//picbox.Width=800;
//picbox.Height=300;
//为picbox添加图片方法二
Bitmapbitmap=newBitmap(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();
}
}
❼ 图片如何存入数据库
通常对用户上传的图片需要保存到数据库中。解决方法一般有两种:一种是将图片保存的路径存储到数据库;另一种是将图片以二进制数据流的形式直接写入数据库字段中。以下为具体方法:
一、保存图片的上传路径到数据库:
string uppath="";//用于保存图片上传路径
//获取上传图片的文件名
string fileFullname = this.FileUpload1.FileName;
//获取图片上传的时间,以时间作为图片的名字可以防止图片重名
string dataName = DateTime.Now.ToString("yyyyMMddhhmmss");
//获取图片的文件名(不含扩展名)
string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") + 1);
//获取图片扩展名
string type = fileFullname.Substring(fileFullname.LastIndexOf(".") + 1);
//判断是否为要求的格式
if (type == "bmp" || type == "jpg" || type == "jpeg" || type == "gif" || type == "JPG" || type == "JPEG" || type == "BMP" || type == "GIF")
{
//将图片上传到指定路径的文件夹
this.FileUpload1.SaveAs(Server.MapPath("~/upload") + "\\" + dataName + "." + type);
//将路径保存到变量,将该变量的值保存到数据库相应字段即可
uppath = "~/upload/" + dataName + "." + type;
}
二、将图片以二进制数据流直接保存到数据库:
引用如下命名空间:
using System.Drawing;
using System.IO;
using System.Data.SqlClient;
设计数据库时,表中相应的字段类型为iamge
保存:
//图片路径
string strPath = this.FileUpload1.PostedFile.FileName.ToString ();
//读取图片
FileStream fs = new System.IO.FileStream(strPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
//存入
SqlConnection myConn = new SqlConnection("Data Source=.;Initial Catalog=stumanage;User ID=sa;Password=123");
string strComm = " INSERT INTO stuInfo(stuid,stuimage) VALUES(107,@photoBinary )";//操作数据库语句根据需要修改
SqlCommand myComm = new SqlCommand(strComm, myConn);
myComm.Parameters.Add("@photoBinary", SqlDbType.Binary, photo.Length);
myComm.Parameters["@photoBinary"].Value = photo;
myConn.Open();
if (myComm.ExecuteNonQuery() > 0)
{
this.Label1.Text = "ok";
}
myConn.Close();
读取:
...连接数据库字符串省略
mycon.Open();
SqlCommand command = new
SqlCommand("select stuimage from stuInfo where stuid=107", mycon);//查询语句根据需要修改
byte[] image = (byte[])command.ExecuteScalar ();
//指定从数据库读取出来的图片的保存路径及名字
string strPath = "~/Upload/zhangsan.JPG";
string strPhotoPath = Server.MapPath(strPath);
//按上面的路径与名字保存图片文件
BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));
bw.Write(image);
bw.Close();
//显示图片
this.Image1.ImageUrl = strPath;
采用俩种方式可以根据实际需求灵活选择。
❽ C# 数据库中image 读取 参数无效
网页链接这里有讲到图片转换数组时的问题,应该对解决问题会有点帮助