上传图片到数据库,从数据库的创建到数据库中图片的现实都给出了具体的代码,因为asp.net版本的问题,大家可能需要稍微修改下。
象文件说明字段,ImageSize Column为储存图象文件长度字段,结构如下:
[ImageID] [int] IDENTITY (1, 1) NOT NULL ,
[ImageData] [image] NULL ,
[ImageContentType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[ImageDescription] [varchar] (200) COLLATE Chinese_PRC_CI_AS NULL ,
[ImageSize] [int] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
UpLoadImage.aspx程序内容如下:
上传图片(选择你要上传的图片) | |
文件说明(添加上传图片说明,如:作者、出处) | |
UpLoadImage.cs程序内容如下:
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace UploadImage
{
public class UploadImage : Page {
protected HtmlInputFile UP_FILE; //HtmlControl、WebControls控件对象
protected TextBox txtDescription;
protected Label txtMessage;
protected Int32 FileLength = 0; //记录文件长度变量
protected void Button_Submit(System.Object sender, System.EventArgs e) {
HttpPostedFile UpFile = UP_FILE.PostedFile; //HttpPostedFile对象,用于读取图象文件属性
FileLength = UpFile.ContentLength; //记录文件长度
try {
if (FileLength == 0) { //文件长度为零时
txtMessage.Text = “请你选择你要上传的文件“;
} else {
Byte[] FileByteArray = new Byte[FileLength]; //图象文件临时储存Byte数组
Stream StreamObject = UpFile.InputStream; //建立数据流对像
//读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
StreamObject.Read(FileByteArray,0,FileLength);
//建立SQL Server链接
SqlConnection Con = new SqlConnection(“Data Source=Localh来源gaodai#ma#com搞*!代#%^码网ost;Initial
Catalog=testdb;User ID=sa;Pwd=;”);
String SqlCmd = “INSERT INTO ImageStore (ImageData, ImageContentType,
ImageDescription, ImageSize) valueS (@Image, @ContentType,
@ImageDescription, @ImageSize)”;
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add(“@Image”,SqlDbType.Binary, FileLength).value =
FileByteArray;
CmdObj.Parameters.Add(“@ContentType”, SqlDbType.VarChar,50).value =
UpFile.ContentType; //记录文件类型
//把其它单表数据记录上传
CmdObj.Parameters.Add(“@ImageDescription”, SqlDbType.VarChar,200).value =
txtDescription.Text;
//记录文件长度,读取时使用
CmdObj.Parameters.Add(“@ImageSize”, SqlDbType.BigInt,8).value =
UpFile.ContentLength;
Con.Open();
CmdObj.ExecuteNonQuery();
Con.Close();
txtMessage.Text = “
OK!你已经成功上传你的图片“;//提示上传成功
}
} catch (Exception ex) {
txtMessage.Text = ex.Message.ToString();
}}}}
//———————————————————————-
//好了,图片已经上传到数据库,现在还要干什么呢?当然是在数据库中读取及显示在Web页中啦,
请看以下程序:
ReadImage.aspx程序内容如下:
//———————————————————————-
//ReadImage.cs程序内容如下:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace ReadImage {
public class MainDisplay : System.Web.UI.Page {
public void Page_Load(System.Object sender, System.EventArgs e) {
int ImgID = Convert.ToInt32(Request.QueryString[“ImgID”]); //ImgID为图片
ID
//建立数据库链接
SqlConnection Con = new SqlConnection(“Data Source=KING;Initial
Catalog=testdb;User ID=sa;Pwd=;”);
String SqlCmd = “SELECT * FROM ImageStore WHERE ImageID = @ImageID”;
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add(“@ImageID”, SqlDbType.Int).value = ImgID;
Con.Open();
SqlDataReader SqlReader = CmdObj.ExecuteReader();
SqlReader.Read();
Response.ContentType = (string)SqlReader[“ImageContentType”];//设定输出文件类型
//输出图象文件二进制数制
Response.OutputStream.Write((byte[])SqlReader[“ImageData”], 0,
(int)SqlReader[“ImageSize”]);
Response.End();
Con.Close();
//很简单吧^_^
}
}
}
最后,我们当然要把它在Web页面显示出来啦
ShowImage.hml
这个是从数据库读取出来的图象:
以上就是ASP.NET下上传图片到数据库,并且读出图片的代码(详细版)的详细内容,更多请关注gaodaima搞代码网其它相关文章!