资讯

精准传达 • 有效沟通

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

sqlserver存照片,sql数据库怎么存图片

怎样SQL SERVER 中加入图片数据

有两种方法,

企业建站必须是能够以充分展现企业形象为主要目的,是企业文化与产品对外扩展宣传的重要窗口,一个合格的网站不仅仅能为公司带来巨大的互联网上的收集和信息发布平台,成都创新互联公司面向各种领域:门帘成都网站设计公司全网整合营销推广解决方案、网站设计等建站排名服务。


一种是只存图片所放在服务器的路径,只要存放varchar格式就可以了。

这种方法比较好。不占用数据库太大,但是服务器需要解析路径。

一种是存放二进制的数据Image类型。sqlserver提供了。在程序中要用流文件的形式,以下是我在网上找到的资料,希望对你有帮助:

保存images进SQL Server数据库

为了保存图片到table你首先得从客户端上传它们到你的web服务器。你可以创建一个web form,用TextBox得到图片的标题,用HTML File Server Control得到图片文件。确信你设定了Form的encType属性为multipart/form-data。

Stream imgdatastream = File1.PostedFile.InputStream;

int imgdatalen = File1.PostedFile.ContentLength;

string imgtype = File1.PostedFile.ContentType;

string imgtitle = TextBox1.Text;

byte[] imgdata = new byte[imgdatalen];

int n = imgdatastream.Read(imgdata,0,imgdatalen);

string connstr=

((NameValueCollection)Context.GetConfig

("appSettings"))["connstr"];

SqlConnection connection = new SqlConnection(connstr);

SqlCommand command = new SqlCommand

("INSERT INTO ImageStore(imgtitle,imgtype,imgdata)

VALUES ( @imgtitle, @imgtype,@imgdata )", connection );

SqlParameter paramTitle = new SqlParameter

("@imgtitle", SqlDbType.VarChar,50 );

paramTitle.Value = imgtitle;

command.Parameters.Add( paramTitle);

SqlParameter paramData = new SqlParameter

( "@imgdata", SqlDbType.Image );

paramData.Value = imgdata;

command.Parameters.Add( paramData );

SqlParameter paramType = new SqlParameter

( "@imgtype", SqlDbType.VarChar,50 );

paramType.Value = imgtype;

command.Parameters.Add( paramType );

connection.Open();

int numRowsAffected = command.ExecuteNonQuery();

connection.Close();

asp.net如何将一个图片保存到sqlserver,不是图片路径

//将图片转行为二进制的方式,存储到数据库

string name = FileUpload1.PostedFile.FileName;

string type = name.Substring(name.LastIndexOf(".") + 1); 

FileStream fs = File.OpenRead(name);

byte[] content = new byte[fs.Length];

fs.Read(content, 0, content.Length);

fs.Close();

//操作SQL数据库存储,请结合自己的软件

SqlConnection conn = new SqlConnection("Data Source=;Initial Catalog=;Persist Security Info=True;User ID=;Pooling=False;Password=");

SqlCommand cmd = conn.CreateCommand();

conn.Open();

cmd.CommandText = "insert into Images(Image_Content) values (@content)";

cmd.CommandType = CommandType.Text;

if (type == "jpg" || type == "gif" || type == "bmp" || type == "png")

{

SqlParameter para = cmd.Parameters.Add("@content", SqlDbType.Image);

para.Value = content;

cmd.ExecuteNonQuery();

}

//读取数据库字段,并展示图片

string imgid = Request.QueryString["imgid"];

SqlConnection conn1 = new SqlConnection("Data Source=;Initial Catalog=;Persist Security Info=True;User ID=sa;Pooling=False;Password=");

SqlCommand cmd1 = new SqlCommand("select Image_Content from Images where Image_ID=3", conn1);     //固定显示Image_ID为3的图片

conn1.Open();

SqlDataReader sdr = cmd1.ExecuteReader();

if (sdr.Read())

{

Response.BinaryWrite((byte[])sdr["Image_Content"]);

}

Response.End();

如何在sql server中存储图片

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

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

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

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

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

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


当前题目:sqlserver存照片,sql数据库怎么存图片
网站网址:http://cdkjz.cn/article/hcshid.html
多年建站经验

多一份参考,总有益处

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

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

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