//将图片转行为二进制的方式,存储到数据库
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();
using System;
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 = "< b>请你选择你要上传的文件< /b>";
} 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=Localhost;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 = "< p>< b>OK!你已经成功上传你的图片< /b>";//提示上传成功
}
} catch (Exception ex) {
txtMessage.Text = ex.Message.ToString();
}}}}
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();
//很简单吧^_^
}
}
}
ShowImage.hml
< html>
< body>
这个是从数据库读取出来的图象:< img src="ReadImage.aspx?ImgID=1">
< body>
< /html>