将图像存储在数据库中,然后使用C#检索图像 [英] Store image in database and then retrieve it using c#

查看:84
本文介绍了将图像存储在数据库中,然后使用C#检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,先生/女士

我的项目包含一个页面,在该页面上有从文件夹中显示的图像,但是我想更改它.我想要一个代码,可以在其中将图像存储在sql server 2008中,然后检索该图像并显示它们.

紧急需要.

Hello Sir/Mam

my project consist of a page on which there are images which are displayed from folders, but i want to change that. I want a code in which i can store a image in sql server 2008 and then retrieve that images and display them.

Help needed urgently.

推荐答案

嗨.
首先,您从html控件ID中拖动文件作为PersonImage,在该click事件中放置了一个asp web按钮,写这个-

Hi.
first u drag file from html controls id as PersonImage put one asp web button in that click event write this-

Int64 intImageSize;
string strImageType;
Stream ImageStream;
//  Gets the Size of the Image
intImageSize = PersonImage.PostedFile.ContentLength;
//  Gets the Image Type
strImageType = PersonImage.PostedFile.ContentType;
//  Reads the Image
ImageStream = PersonImage.PostedFile.InputStream;
byte[,] ImageContent;
int intStatus;
intStatus = ImageStream.Read(ImageContent, 0, intImageSize);
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"));
SqlCommand myCommand = new SqlCommand("sp_person_isp", myConnection);
//  Mark the Command as a SPROC
myCommand.CommandType = CommandType.StoredProcedure;
SqlParameter prmPersonImage = new SqlParameter("@PersonImage", SqlDbType.Image);
prmPersonImage.Value = ImageContent;
myCommand.Parameters.Add(prmPersonImage);
SqlParameter prmPersonImageType = new SqlParameter("@PersonImageType", SqlDbType.VarChar, 255);
prmPersonImageType.Value = strImageType;
myCommand.Parameters.Add(prmPersonImageType);
try {
   myConnection.Open();
   myCommand.ExecuteNonQuery();
   myConnection.Close();
   Response.Write("New person successfully added!");
}
catch (SqlException SQLexc) {
   Response.Write(("Insert Failed. Error Details are: " + SQLexc.ToString()));
}


如果紧急的话,我会在 ^ ].

之前已经做过很多次了,在Google上有无数的例子.请看下面的链接:

http://csharpdotnetfreak.blogspot.com/2009/07/fileupload-control- save-images-database.html [ ^ ]

http://www.4guysfromrolla.com/articles/120606-1.aspx [ ^ ]
If it is that urgent, I will look it up at Google[^].

This has been done so many times before and there are tons of example on Google. Look at the links below:

http://csharpdotnetfreak.blogspot.com/2009/07/fileupload-control-save-images-database.html[^]

http://www.4guysfromrolla.com/articles/120606-1.aspx[^]


非常简短并具体说明以下链接中给出的要点答案:

http://stackoverflow.com/questions/9069742/store-image-in- database-and-retrieve-it [ ^ ]
Very short and concrete to the point answer given in below link:

http://stackoverflow.com/questions/9069742/store-image-in-database-and-retrieve-it[^]


这篇关于将图像存储在数据库中,然后使用C#检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆