如何将图像保存在数据库中并在asp.net上显示图像 [英] How to save the image on database and display the image on asp.net

查看:62
本文介绍了如何将图像保存在数据库中并在asp.net上显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将图像上传到数据库并在< asp:Image>上显示图像并使用linq报告图像,请帮助我。

I need to upload the image into database and display the image on <asp:Image> and also on report image using linq, Please help me.

推荐答案

Select file to save into the database: 

<asp:fileupload runat="server" id="FileUpload1" xmlns:asp="#unknown" />

<asp:button runat="server" id="btnSave" onclick="SaveToTheDatabase" text="Save to the database" xmlns:asp="#unknown" />

<p><asp:label id="lblMessage" runat="server" enableviewstate="false" xmlns:asp="#unknown" /></p>





http://www.dotnetfunda.com/articles/show/1084/saving-images-into-数据库在aspnet中并显示到网格 [ ^ ]





http://www.dotnetfunda.com/articles/show/1084/saving-images-into-the-database-in-aspnet-and-displaying-to-the-gridvi[^]

protected void SaveToTheDatabase(object sender, EventArgs e)

{

string fileName = FileUpload1.PostedFile.FileName;

int fileLength = FileUpload1.PostedFile.ContentLength;

 

byte[] imageBytes = new byte[fileLength];

FileUpload1.PostedFile.InputStream.Read(imageBytes, 0, fileLength);

 

string connStr = ConfigurationManager.AppSettings["ConnStr"].ToString();

using (SqlConnection conn = new SqlConnection(connStr))

{

string sql = "INSERT INTO ImageUpload (PictureName, PictureFile) VALUES (@pictureName, @pictureFile)";

SqlParameter[] prms = new SqlParameter[2];

prms[0] = new SqlParameter("@pictureName", SqlDbType.VarChar, 50);

prms[0].Value = fileName;

prms[1] = new SqlParameter("@pictureFile", SqlDbType.Image);

prms[1].Value = imageBytes;

using (SqlCommand cmd = new SqlCommand(sql, conn))

{

cmd.Parameters.AddRange(prms);

conn.Open();

cmd.ExecuteNonQuery();

conn.Close();

}

lblMessage.Text = "Picture uploaded successsfully !";

}

}


这篇关于如何将图像保存在数据库中并在asp.net上显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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