当前位置:首页 » 操作系统 » 保存图片的数据库中

保存图片的数据库中

发布时间: 2022-09-17 21:13:45

Ⅰ 怎样将图片存在数据库里面

一般图像是不保存在数据库的.而是先将图片放在工程下的某个文件夹中,将图片所在的工程文件路径存在数据库中,当程序加载图片的时候,从数据库中读取图片的路径,然后根据路径在工程的文件夹中读取图片文件

Ⅱ 图片如何存入数据库

通常对用户上传的图片需要保存到数据库中。解决方法一般有两种:一种是将图片保存的路径存储到数据库;另一种是将图片以二进制数据流的形式直接写入数据库字段中。以下为具体方法:
一、保存图片的上传路径到数据库:
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#怎么将图片保存到SQL数据库

this.pictureBox1.Image
=
Image.FromStream(this.openFileDialog1.OpenFile());
//获取当前图片的路径
string
path
=
openFileDialog1.FileName.ToString();
//将制定路径的图片添加到FileStream类中
FileStream
fs
=
new
FileStream(path,
FileMode.Open,
FileAccess.Read);
//通过FileStream对象实例化BinaryReader对象
BinaryReader
br
=
new
BinaryReader(fs);
//通过BinaryReader类对象的ReadBytes()方法将FileStream类对象转化为二进制数组
byte[]
imgBytesIn
=
br.ReadBytes(Convert.ToInt32(fs.Length));第二步:
//将图片添加到数据库中
string
sql="insert
into
pic
values(@pic)";
SqlParameter[]
param
=
new
SqlParameter[]
{
new
SqlParameter("@pic",
imgBytesIn)
};
DBHelper.GetExecuteQuery(sql,
param);第三步:
//将图片从数据库中取出
string
sql="select
*
from
pic
where
id=0";
SqlDataReader
reader
=
DBHelper.GetExecuteReader(sql,
null);
MemoryStream
mss
=
null;

Ⅳ 如何将图片存到数据库

通常对用户上传的图片需要保存到数据库中。解决方法一般有两种:一种是将图片保存的路径存储到数据库;另一种是将图片以二进制数据流的形式直接写入数据库字段中。以下为具体方法:
一、保存图片的上传路径到数据库:
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;
采用俩种方式可以根据实际需求灵活选择。

Ⅵ 怎么把图片保存到数据库里

把你的图片放在你项目的根目录下面,把路径保存在数据库中。。数据库一般不是用来放图片的,如果你是做网站,你的空间根本不够放那么多。。建议你还是在数据库中保存你图片的地址

java如何将图片保存在数据库中

一般都是这样的,就是在你服务器有一个专门放置图片的文件夹,然后数据库保存的是你服务器图片的路径。需要用的时候就去数据库里面取路径。得到路径以后你想怎么处理图片是你的事情了。
至于如何去数据库取路径这个就是简单的db操作。
加载驱动类:
Class.forName(DBDriver);
获取连接:
Connection
conn
=
DriverManager.getConnection(url,username,password);
创建操作对象:
PreparedStatement
stmt
=
con.prepareStatement(sql);
执行操作:
ResultSet
rs
=
stmt.executeQuery();
遍历结果:
List
list
=
new
ArrayList();
while(rs.next()){
//具体操作,通常用rs.getString(name)取值
Image
img
=
new
Image();//图片类对应你数据库中图片表格
img.setSrc(rs.getString("src"));//假设你数据库中image表中图片地址字段是src
list.add(img);
}
记得关闭资源:
rs.close();
stmt.close();
con.close();
看你的意思是已经取出来了不知道怎么显示:
你取出来之后可以把图片放在一个list里面然后去页面上遍历这个list
<c:forEach
var="chakan1"
items="list">
<tr>
<td>
<img
src="${chakan1.src}"/>
</td>
</tr>
</c:forEach>
大致应该是这样

Ⅷ 怎么把图片,视频存储在数据库中

图片、视频一般都是存储在磁盘中,然后把存储在磁盘里的路径存储在数据库中

热点内容
linux支持线程 发布:2025-05-17 21:26:14 浏览:182
元神队伍配置都由什么组成 发布:2025-05-17 21:20:18 浏览:475
闲鱼和安卓哪个赚钱 发布:2025-05-17 21:15:56 浏览:583
c语言一个c源程序 发布:2025-05-17 21:11:44 浏览:314
如何加密手机的文件 发布:2025-05-17 21:11:43 浏览:915
ios开发文件上传 发布:2025-05-17 21:10:40 浏览:983
g92编程 发布:2025-05-17 21:00:31 浏览:170
汇编语言第三版脚本之家 发布:2025-05-17 20:54:26 浏览:399
资源配置最佳状态叫什么 发布:2025-05-17 20:48:58 浏览:84
定义dns服务器的ip 发布:2025-05-17 20:32:37 浏览:954