如何在sql server中存储图片

[复制链接]
查看11 | 回复5 | 2020-4-28 13:52:21 | 显示全部楼层 |阅读模式
VB在SQL Server 2000中存储图片,其实不是特别困难的。1、数据表必须有数据类型是Image类型的字段,这个字段是可以存储图形的二进制数据的,存储量可达2G字节。2、可想,存储图形二进制数据,必须就原来的图形转换为二进制数据,这是存储图形数据的关键。3、存储的二进制图形,如果要读取,必须就二进制数据转换为图形数据。上面是VB在SQL Server 2000中存储图片的必须要求。但是也可以在数据表存储图形的路径,这样比较简单,但是不安全,我们就以存储二进制数据讨论吧。一、存储图形的关键语句:Dim mst As New ADODB.Stream 'Stream 对象是进行二进制数据操作对象mst.Type = abTypeBinarymst.OpenIf 图片的路径和文件名变量(需要用其他方法获得) "" Thenmst.LoadFromFile 图片的路径和文件名变量End Ifrs("存储二进制数据的字段") = mst.Read二、读取存储的二进制数据的关键语句:Dim mst As New ADODB.Stream mst.Type = abTypeBinarymst.Openmst.Write rs("存储二进制数据的字段")mst.SaveToFile App.Path & "/" & list1.Text,abSaveCreateOverWritePicture1.Picture = LoadPicture(App.Path & "/" & List1.Text)mst.Close
'别忘了关闭对象!
回复

使用道具 举报

千问 | 2020-4-28 13:52:21 | 显示全部楼层
两种方式1、存放图片路径2、转换成2进制流(不过这样的话将很占用数据库空间)存路径的方式就很简单了,下面着重给出转换成2进制流的存入以及读取方法。存入: string FilePath=""; OpenFileDialog oFileDialog=new OpenFileDialog(); if(oFileDialog.ShowDialog()==DialogResult.OK) {
FilePath=oFileDialog.FileName;
FileStream fs=new FileStream(FilePath,FileMode.Open,FileAccess.Read);
byte[] bytes=new byte[fs.Length];
string sql = "insert PicSave(FileName,FileType,FileData)values(@FN,@FT,@MF)";
SqlCommand com = new SqlCommand(sql,this.sqlCon);
SqlParameter FN = new SqlParameter("@FN",SqlDbType.VarChar);
FN.Value = "泳装妹妹";
com.Parameters.Add(FN);
SqlParameter FT = new SqlParameter("@FT",SqlDbType.VarChar);
FT.Value = "JPG";
com.Parameters.Add(FT);
SqlParameter MF = new SqlParameter("@MF",SqlDbType.Image);
MF.Value = bytes;
com.Parameters.Add(MF);
com.CommandType = CommandType.Text;
sqlCon.ConnectionString
= this.conStr; try
{
sqlCon.Open();
com.ExecuteNonQuery();
sqlCon.Close();
MessageBox.Show("存入成功!");
}
catch(Exception ex)
{
throw ex;
}
finally
{
sqlCon.Close();
}
}读出:int id=Convert.ToInt32(this.textBox1.Text.Trim()); string sql = "select FileData from PicSave where id ="+id+"";
sqlCon.ConnectionString = this.conStr;
SqlCommand readComm = new SqlCommand(sql,this.sqlCon);
SqlDataReader dr = null;
FileStream fs = null;
BinaryWriter bw = null;
int bufferSize = 100;
byte[] outbyte = new byte[bufferSize];
long retval;
long startIndex = 0; try
{
sqlCon.Open();
dr = readComm.ExecuteReader();
while(dr.Read())
{
fs = new FileStream("TakeOut.jpg",FileMode.OpenOrCreate,FileAccess.Write);
bw = new BinaryWriter(fs);
retval = dr.GetBytes(0, startIndex, outbyte, 0, bufferSize);
while (retval == bufferSize)
{
bw.Write(outbyte);
bw.Flush();
startIndex += bufferSize;
retval = dr.GetBytes(0, startIndex, outbyte, 0, bufferSize);
}
bw.Write(outbyte, 0, (int)retval - 1);
bw.Flush();
bw.Close();
fs.Close();
Image im = Image.FromFile("TakeOut.jpg");
this.pictureBox1.Image = im;
}
sqlCon.Close();
}
catch(Exception ex)
{
throw ex;
}
finally
{
sqlCon.Close();
bw.Close();
fs.Close();
}
回复

使用道具 举报

千问 | 2020-4-28 13:52:21 | 显示全部楼层
最好不要把图片存在库里。这样你的库要不到好久会很大很大。。。。。是非常不明智的做法。最好是把图片放到文件夹里。然后把路径放到库里。首先SQL表字段的类型设置为IMAGE类型然后你用qsc800528 回答的代码 就可以了。他写的是存储过程。你可以在程序里把图片转成二进制 然后存进去。
回复

使用道具 举报

千问 | 2020-4-28 13:52:21 | 显示全部楼层
最好别把图片存储到sql里,我们都是将图片储存到文件,然后将图片的位置即地址储存到sql中。图片读成二进制流直接存字段里面
回复

使用道具 举报

千问 | 2020-4-28 13:52:21 | 显示全部楼层
这个只能说是理论研究,实际应用中几乎没有这样做的,一般都是在数据库中设置一个字段存储图片的相对位置,而图片还是存储在网站的某个文件夹里面,而且最好还是把图片按照提交的日期来分文件夹存储,方便以后维护,具体看图片的数量,如果不大可以直接把年份作文件夹,如果图片更新很频繁,可以考虑用年月甚至年月日来做文件夹,这样分类对后期运营维护有很大的好处。
回复

使用道具 举报

千问 | 2020-4-28 13:52:21 | 显示全部楼层
create procedure insert_emp_picture @emp_id char(5), @imageimage as begin transaction if @emp_id in(select emp_id from emp_picture) begin update emp_picture set image=@image where emp_id=@emp_id if @@error 0 or @@rowcount=0 begin goto error end end else begin insert into emp_picture
select @emp_id,@image
if @@error 0 or @@rowcount=0 begin goto error end end commit transaction return 0 error: rollback transaction return 1 go
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行