资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

sqlserver图片,sqlserver图片的十六进制怎么算的

SQLServer数据库怎么存入图片?

你可以把表的类型

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站建设、网站设计、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的额济纳网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

设计 成为二进制类型.

Binary 类型:

数据类型 描述

bit 允许 0、1 或 NULL

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

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

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

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

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

不是很建议这样做.

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

如何在sql server中存储图片

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

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

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

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

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

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

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 上传的

图片保存:

SqlConnection conn = new SqlConnection("Server=.;Database=znglxt;uid=sa;pwd=123");

conn.Open();

//将文件转换成字节数组

byte[] b = File.ReadAllBytes(textBox1.Text); //创建SQL语句字符串(带参数:表示图片的字节数组)

string sql = "insert into P values (@NewImage)";

//中间过度操作 //--创建SQL专用命令参数

SqlParameter param = new SqlParameter();

param.SqlDbType = SqlDbType.Image; //设置参数类型

param.ParameterName = "@NewImage"; //建立对应关系

param.Value = b; //设置参数的值 //--创建命令对象

SqlCommand cmd = new SqlCommand(sql, conn);

cmd.Parameters.Add(param); //添加SQL专用命令参数到命令对象里面,并且将"insert into P values (@NewImage)"中的@NewImage替换成b的内容

//执行插入数据

cmd.ExecuteNonQuery(); 图片读取://取出数据,并装到读取器dr中 SqlConnection conn = new SqlConnection("Server=.;Database=znglxt;uid=sa;pwd=123");

conn.Open();

SqlCommand cmd = new SqlCommand("select * from P",conn);

SqlDataReader dr = cmd.ExecuteReader();

//判断读取器dr中是否有数据

if (dr.Read())

{//读取器就定位在第一条记录上 //将图片(字节)信息转换成流

Stream s = dr.GetSqlBytes(1).Stream; //将流转换成图片

Image img = Image.FromStream(s); //显示图片到pictureBox1

pictureBox1.Image = img; } dr.Dispose();

cmd.Dispose();

conn.Dispose();

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

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

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

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

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

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

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

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

java如何向SQLserver库中插入图片

图片的存储有两种:

*(1)存储字符串类型的图片的地址 显示页面使用image标签

*(2)数据库有一种类型累Blok(不知道也错没)还有好多大类型,使用这个来存储

,但他存储的是2进制,在存储前先使用io读取图片,转成2进制,然后就可以存储在数据库中,

读取时也是用io


分享标题:sqlserver图片,sqlserver图片的十六进制怎么算的
文章分享:http://cdkjz.cn/article/dsijjdi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220