"散步的蠕虫"的IT日志

将图片保存(上传)到数据库、并显示图片(ACCESS OLE对象)

MyDB类
namespace portfolio
{
 /// <summary>
 /// MyDB 的摘要说明。
 /// </summary>
 public class MyDB
 {
  OleDbConnection conn;
  OleDbCommand cmd;
  public MyDB()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
            string sqlconn=@"provider=microsoft.jet.oledb.4.0;data source=c:/SunSDAS.MDB";
   conn=new OleDbConnection(sqlconn);
  }
  public void MyCmd(string cmdSql,int FileLength,byte[] FileByte)
  {
   cmd=new OleDbCommand(cmdSql,conn);
   cmd.Parameters.Add("@Image",OleDbType.Binary,FileLength).Value=FileByte;
   //cmd.Parameters.Add("@ImageName",OleDbType.VarChar,100).Value=ImageName;
   conn.Open();
   cmd.ExecuteNonQuery();
   conn.Close();
  }

  public void ShowPic()
  {
   try
   {
    conn.Open();

    //Retrieve BLOB from database into DataSet.
    cmd = new OleDbCommand("SELECT TP FROM XS_XC",conn); 
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds, "xs_xc");
    int c = ds.Tables["xs_xc"].Rows.Count;

    if(c>0)
    {  
     //BLOB is read into Byte array, then used to construct MemoryStream,
     //then passed to PictureBox.
     Byte[] byteTP =  new Byte[0];
     byteTP = (Byte[])(ds.Tables["xs_xc"].Rows[c - 1]["TP"]);
     MemoryStream stmTP = new MemoryStream(byteTP);
    
    }
    conn.Close();
   }
   catch
   {
    
   }
  }

 }
}
将图片上传到数据库
private void btnSubmit_Click(object sender, System.EventArgs e)
  {
   try
   {
    MyDB db=new MyDB();
    string FileName=myFile.Value;
    HttpPostedFile UpFile=myFile.PostedFile;//获取对由客户端指定的上传文件的访问
    int FileLength=UpFile.ContentLength;//获取上传文件的字节大小
    if(FileLength==0)
    {
     Response.Write("<script>alert('对不起,请选择要上传的图片')</script>");
     return;
    }
    string exName=FileName.Substring(FileName.LastIndexOf(".")+1).ToUpper();//截取图片的后缀名
    if(exName=="JPG"||exName=="BMP"||exName=="GIF")//判断图片的类型
    {
     if(FileLength>204800)//判断图片是否大于200k(根据自己的需要判断大小)
     {
      Response.Write("<script>alert('对不起,图片大小不能大于200K')</script>");
      return;
     }
     else
     {
      string ImageName=this.txtImageName.Text.ToString().Trim()+"."+exName;//图片名称设置为保存的时间
      Byte[]  FileByte  =  new  Byte[FileLength]; //图象文件储存到数组 
      Stream  ObjectStream  =  UpFile.InputStream;//建立数据流对像,获取一个 Stream 对象,该对象指向一个上载文件,以准备读取该文件的内容。
  
      ObjectStream.Read(FileByte,0,FileLength); //读取图象文件数据
      string StrSql="Insert Into XS_XC(TP) Values(@Image)";
      db.MyCmd(StrSql,FileLength,FileByte);

      Response.Write("<script>alert('图片保存到数据库成功')</script>");
     }
    }
    else
    {
     Response.Write("<script>alert('对不起,请选择正确的的图片')</script>");
     return;
    }
   
   }
   catch(Exception ex)
   {
    Response.Write("<script>alert('"+ex.Message+"')</script>");
   }
  }

如何显示
新建一个页面,用于存放图片
image.aspx
OleDbCommand cmd=new OleDbCommand();
   cmd.CommandText="select bt,tp from xs_xc";
   cmd.Connection=conn;
   this.Response.ContentType="image/jpeg";
   //this.Response.ContentType="image/bmp";
   //this.Response.ContentType="image/gif";
   OleDbDataReader dr=cmd.ExecuteReader();
   while(dr.Read())
   {
    Response.BinaryWrite((byte[])dr["tp"]);
    //

   }
页面里调用
<image src=image.aspx>

posted on 2006-06-09 13:11  散布的蠕虫  阅读(5144)  评论(1编辑  收藏  举报

导航