您的位置:首页 > 数据库

C#+asp.net+sql数据库完成图片的保存与读取

2012-12-15 22:21 316 查看
我们在使用asp.net时经常会用到数据库对图片进行保存和读取,因此笔者对此进行了仔细研究,图片的保存和读取有两种方式:

一.以图片的url地址的方式

在以图片url地址的方式中,我们向数据库中保存的不是图片本身,而是图片的地址,读取图片的时候也是图片的地址,根据保存的地址定位到指定的图片,首先笔者将讲解图片保存到sql数据库中的实现方法。

1.保存图片

1)在数据库的表中定义一个用来保存图片路径的字段,类型为nchar(40),长度按自己要求设置;

2)我们通过网络将图片上传至服务器,将图片保存到服务器指定的路径,因此我们需要建立一个文件夹“img'专门放置上传的图片,而我们数据库中将要保存的地址也是这个文件夹的地址;

3)在aspx文件的Form 标记的 enctype 属性应该设置成 enctype="multipart/form-data",网页中放置一个web控件 System.Web.UI.WebControls.FileUpload,这个控件是asp.net中专门用于上传文件的(html中是 Input(file)),控件命名为‘inputfile’,另外添加一个按钮button1,点击按钮执行代码:

//设置保存路径

string filepath = HttpContext.Current.Server.MapPath("~/img/");

string filefullname = filepath + inputfile.FileName;

inputfile.SaveAs(filefullname);

string connstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Data.mdf;Integrated Security=True;User Instance=True";

SqlConnection con = new SqlConnection(connstring);

con.Open();

string cmd = "insert into [user]([username],[password],[imgurl]) values('_username','_password','" + filefullname + "')";//保存图片

SqlCommand dcmd = new SqlCommand(cmd, con);

dcmd.ExecuteNonQuery();

这样我们就讲上传的图片保存到了img文件夹中,其url地址保存到了数据库中。

2.读取图片

下面是实现从数据库中读取图片地址并在Image控件中显示出来,我们只需进行一般的数据库读取得到imgurl字段的值,然后将字段值赋给Image控件的ImageUrl属性,代码如下:

cmd="select [imgurl] from [user] where ([username]='_username')";

dcmd = new SqlCommand(cmd, con);

SqlDataReader reader = dcmd.ExecuteReader();

reader.Read();

string path = reader[0].ToString();

Image1.ImageUrl = path;

这样我们就可以在Image控件中看到该图片.

二.以二进制图片的方式

图片在数据库中另一种保存方式就是将图片变为二进制方式存入,但是这种方式在读取并在Image控件中显示比较麻烦。下面我们来看实现方法:

1.保存图片

保存图片前,我们需要在数据库的表中添加image类型的字段,用于存放二进制的图片

public byte[] AddImg(System.Web.UI.WebControls.FileUpload inputimg, string ImageType, Int32 maxsize)

{

Int32 ImageSize;

String strImageType;

Stream ImageStream;

strImageType = inputimg.PostedFile.ContentType;

if (strImageType != ImageType)

{

Response.Write("<script>alert('图片类型为'" + strImageType + ")</script>");

}

ImageSize = inputimg.PostedFile.ContentLength;

if (ImageSize > maxsize)

{

Response.Write("<script>alert('图片不得大于'" + maxsize + "k)</script>");

}

ImageStream = inputimg.PostedFile.InputStream;

byte[] ImageContent = new byte[ImageSize];

int imgstatus = ImageStream.Read(ImageContent, 0, ImageSize);

return ImageContent;

}

然后在button点击事件代码:

byte[] imageContent;

imageContent = AddImg(inputfile, "image/pjpeg", 512000);

string connstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Data.mdf;Integrated Security=True;User Instance=True";

SqlConnection con = new SqlConnection(connstring);

con.Open();

string cmd = "insert into[user]([username],[password],[headimg]) values(‘_username','_password',@image)";

SqlParameter prm = new SqlParameter("@image", SqlDbType.VarBinary, imageContent.Length, ParameterDirection.Input, false,

0, 0, null, DataRowVersion.Current, imageContent);

SqlCommand dcmd = new SqlCommand(cmd, con);

dcmd.Parameters.Add(prm);

dcmd.ExecuteNonQuery();

这样我们就将图片存入了数据库

2.读取图片

但是这种方式在我们读取显示的方式有些不同,并不能直接根据url让Image控件显示

cmd = "select [headimg] from [user] where ([username]='_username')";

dcmd = new SqlCommand(cmd, con);

SqlDataReader reader = dcmd.ExecuteReader();

reader.Read();

byte[] img = (byte[])reader[0]; ;

con.Close();

Response.BinaryWrite(img);//这样我们就将图片写入二进制输出流,在网页中显示

但如果我们像让其显示在Image控件,必须将图片从数据库中保存到文件夹为图片,将图片url赋值给ImageUrl。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# asp.net sql数据库
相关文章推荐