RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
sqlserver存图片,sql数据库存图片

sql sever中照片用什么数据类型?

sql sever中照片用image数据类型。

创新互联公司是一家专注于成都网站设计、网站制作与策划设计,海阳网站建设哪家好?创新互联公司做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:海阳等地区。海阳做网站价格咨询:18982081108

sql sever数据库中的Image数据类型可以进行数据图片的存储。保存的是二进制字节,所以写入sql sever数据库Image数据类型时,sql sever数据库自动将图片转换成二进制字节后存入。读取的时候,将二进制再转换成图片从sql sever数据库中输出显示到页面或者程序中。

扩展资料:

如果SQL Server是缺省安装时, IMAGE类型字段是有长度限制,用来存储图片大小不超过2g的图片。缺点是占用了很大的数据存储空间。但是对于之前的存储物理路径来说读取图片和存储图片方便了很多。

一般开发中,照片等二进制的文件并不保存在数据库中。而是保存在服务器的特定目录中,然后在数据库中记录一下这个具体路径和文件名。

Sqlserver数据库存储的图片格式(二进制数据)怎么显示到页面?

1.将图片以二进制存入数据库

//保存图片到数据库

protected void Button1_Click(object sender, EventArgs e)

{

//图片路径

string strPath = "~/photo/03.JPG";

string strPhotoPath = Server.MapPath(strPath);

//读取图片

FileStream fs = new System.IO.FileStream(strPhotoPath, 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=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");

string strComm = " INSERT INTO personPhoto(personName, personPhotoPath, personPhoto) ";

strComm += " VALUES('wangwu', '" + strPath + "', @photoBinary )";

SqlCommand myComm = new SqlCommand(strComm, myConn);

myComm.Parameters.Add("@photoBinary", SqlDbType.Binary,photo.Length);

myComm.Parameters["@photoBinary"].Value = photo;

myConn.Open();

myComm.ExecuteNonQuery();

myConn.Close();

}

2.读取二进制图片在页面显示

//读取图片

SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");

string strComm = " SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ";

SqlCommand myComm = new SqlCommand(strComm, myConn);

myConn.Open();

SqlDataReader dr = myComm.ExecuteReader();

while (dr.Read())

{

byte[] photo = (byte[])dr["personPhoto"];

this.Response.BinaryWrite(photo);

}

dr.Close();

myConn.Close();

SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");

SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='11' ", myConn);

DataSet myds = new DataSet();

myConn.Open();

myda.Fill(myds);

myConn.Close();

byte[] photo = (byte[])myds.Tables[0].Rows[0]["personPhoto"];

this.Response.BinaryWrite(photo);

3.设置Image控件显示从数据库中读出的二进制图片

---------------------------------------------

SqlConnection myConn = new SqlConnection("Data Source=192.168.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");

SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='11' ", myConn);

DataSet myds = new DataSet();

myConn.Open();

myda.Fill(myds);

myConn.Close();

byte[] photo = (byte[])myds.Tables[0].Rows[0]["personPhoto"];

//图片路径

string strPath = "~/photo/wangwu.JPG";

string strPhotoPath = Server.MapPath(strPath);

//保存图片文件

BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));

bw.Write(photo);

bw.Close();

3.显示图片

this.Image1.ImageUrl = strPath;

4.GridView中ImageField以URL方式显示图片

--------------------------

asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

Columns

asp:BoundField DataField="personName" HeaderText="姓名" /

asp:ImageField DataImageUrlField="personPhotoPath"

HeaderText="图片"

/asp:ImageField

/Columns

/asp:GridView

5.GridView显示读出的二进制图片

//样板列

asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"

Columns

asp:BoundField DataField="personName" HeaderText="姓名" /

asp:ImageField DataImageUrlField="personPhotoPath"

HeaderText="图片"

/asp:ImageField

asp:TemplateField HeaderText="图片"

ItemTemplate

asp:Image ID="Image1" runat="server" /

/ItemTemplate

/asp:TemplateField

/Columns

/asp:GridView

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowIndex 0)

return;

// System.ComponentModel.Container

string strPersonName = (string)DataBinder.Eval(e.Row.DataItem, "personName");

Image tmp_Image = (Image)e.Row.Cells[2].FindControl("Image1");

if (!System.Convert.IsDBNull(DataBinder.Eval(e.Row.DataItem, "personPhoto")))

{

//

byte[] photo = (byte[])DataBinder.Eval(e.Row.DataItem, "personPhoto");

//图片路径

string strPath = "~/photo/" + strPersonName.Trim() + ".JPG";

string strPhotoPath = Server.MapPath(strPath);

//保存图片文件

BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath, FileMode.OpenOrCreate));

bw.Write(photo);

bw.Close();

//显示图片

tmp_Image.ImageUrl = strPath;

}

}

sqlserver 的图片怎么读取? 怎么存储 用asp.net 上传的

直接存,直接读.要注意格式,就是类型,数据库设计时,图片字段类型是IMAGE程序中取时转成IMAGE接收!

sqlserver中如何将图片存入进去?

呵呵,这个问题我喜欢。以前我一直也存在这样的疑问。

既然存在IMAGE类型,为什么不能存储图片呢?

后来查了很多资料,都发现很避讳直接回答这问题。

这是我一点心得,只是我自己理解的。

存储图片在SQL中不如设置一个NVARCHAR来存储地址好些。因为在调用数据库时,调用一条字段来读取地址,比直接从数据库中调出大字节的字段速度快很多。

如果真的想用SQL来存储图片,那我知道的,可以在不同的语言中,以不同的方式来实现。有C#,DEPHIN,VB都可以。但是写成一个过程来在应用程序中实现的。

不知道我说的对你有帮助没。谢谢了

如何在sql server中存储图片

1、首先可以存储图片链接,设置图片链接字段,如下图所示。

2、接着直接将图片的链接添加到SQL数据表的字段里即可,如下图所示。

3、或者用二进制存储图片字段,在SQL Server数据库中先制作成image字段。

4、接着在后台通过代码形式,将图片转化为二进制,如下图所示。

5、得到二进制数据后,则可通过sql语句插入到数据表中。

6、数据表即可存储了图片字段,将二进制转化为图片。

SQLServer数据库怎么存入图片?

你可以把表的类型

设计 成为二进制类型.

Binary 类型:

数据类型 描述

bit 允许 0、1 或 NULL

binary(n) 固定长度的二进制数据。最多 8,000 字节。

varbinary(n) 可变长度的二进制数据。最多 8,000 字节。

varbinary(max) 可变长度的二进制数据。最多 2GB 字节。

image 可变长度的二进制数据。最多 2GB。

不过我个人觉得,把图片 ,存到数据,会使数据库的数据,增长的很快.

不是很建议这样做.

可以把图片 存到服务器上的某个路径 下..


网页名称:sqlserver存图片,sql数据库存图片
文章路径:http://sczitong.cn/article/hoihgj.html