文章导航PC6首页软件下载单机游戏安卓资源苹果资源

pc软件新闻网络操作系统办公工具编程服务器软件评测

安卓新闻资讯应用教程刷机教程安卓游戏攻略tv资讯深度阅读综合安卓评测

苹果ios资讯苹果手机越狱备份教程美化教程ios软件教程mac教程

单机游戏角色扮演即时战略动作射击棋牌游戏体育竞技模拟经营其它游戏游戏工具

网游cf活动dnf活动lol周免英雄lol礼包

手游最新动态手游评测手游活动新游预告手游问答

您的位置:首页精文荟萃软件资讯 → 数据库中图片存储及读取

数据库中图片存储及读取

时间:2004/10/7 18:16:00来源:本站整理作者:蓝点我要评论(0)

 开发环境:Window 2000、SQLServer2000、.Net Framework SDK正式版
开发语言:C#、ASP.Net
简介:数据库中图片存储及读取

说明:在ASP中,我们用Request.TotalBytes、Request.BinaryRead()来上传图片,这个可恶的BinaryRead()方法非常笨,单个文件上传倒没什么大事,单如果多个图片上专可就花大气力了…!而现在ASP.Net中将会把解决以前ASP中文件上传的种种问题,使你在ASP.Net中轻轻松松开发出功能强大的上传程序,下面大家看看例子啦。



 首先在SQL Server中建立一个图片存储的数库表,SqlScript如下:



if exists (select * from dbo.sysobjects where id = object_id(N"[dbo].[image]") and OBJECTPROPERTY(id, N"IsUserTable") = 1)
drop table [dbo].[image]
GO



CREATE TABLE [dbo].[image] (
 [img_pk] [int] IDENTITY (1, 1) NOT NULL ,
 [img_name] [varchar] (50) NULL ,
 [img_data] [image] NULL ,
 [img_contenttype] [varchar] (50) NULL 
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO



ALTER TABLE [dbo].[image] WITH NOCHECK ADD 
 CONSTRAINT [PK_image] PRIMARY KEY  NONCLUSTERED 
 (
  [img_pk]
 )  ON [PRIMARY] 
GO
------------------------------------------------------------
一、上传图片:
imgupload.aspx文件:
 

  
   文件名 
   

   选择文件 
   

   
  
  看图
 

codebehind文件:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;



namespace study.uploadimage
{
 /// 


 /// imgupload 的摘要说明。
 /// 
 public class imgupload : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Button Button1;
  protected System.Web.UI.HtmlControls.HtmlInputText imgName;
  protected System.Web.UI.HtmlControls.HtmlInputFile UploadFile;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
  }



  private void Button1_Click(object sender, System.EventArgs e)
  {
   Stream imgStream;
   int imgLen;
   string imgName_value;
   string imgContentType;
   string imgUploadedName;
   
   imgStream  = UploadFile.PostedFile.InputStream;
   imgLen =  UploadFile.PostedFile.ContentLength;
   imgUploadedName = UploadFile.PostedFile.FileName;
   byte[] imgBinaryData=new byte[imgLen];
   imgContentType = UploadFile.PostedFile.ContentType;
   imgName_value = imgName.Value;



   try
   {
    if(imgName_value.Length < 1)
    {
     imgName_value = GetLastRightOf("\\",imgUploadedName );
    }
   }
   catch(Exception myEx)
   {
    Response.Write(myEx.Message);
   }



   int n = imgStream.Read(imgBinaryData, 0, imgLen);          
   int NumRowsAffected = MyDatabaseMethod(imgName_value, imgBinaryData, imgContentType);
            if(NumRowsAffected > 0)
             Response.Write( "
 uploaded image " );
   else
             Response.Write ( "
 an error occurred uploading the image.d " );
  }
  public string GetLastRightOf(string LookFor,string myString)
  {
   int StrPos;
   StrPos = myString.LastIndexOf(LookFor);
   return myString.Substring(StrPos + 1);
  }
  public int MyDatabaseMethod(string imgName,byte[] imgbin,string imgcontenttype)
  {
   SqlConnection connection = new SqlConnection(Application["Test_Conn"].ToString());
   string SQL="INSERT INTO Image (img_name,img_data,img_contenttype) VALUES ( @img_name, @img_data,@img_contenttype )";
   SqlCommand command=new SqlCommand ( SQL,connection );
            
   SqlParameter param0=new SqlParameter ( "@img_name", SqlDbType.VarChar,50 );
   param0.Value = imgName;   
   command.Parameters.Add( param0 );            



   SqlParameter param1=new SqlParameter ( "@img_data", SqlDbType.Image );   
   param1.Value = imgbin;
   command.Parameters.Add( param1 );
            
   SqlParameter param2 =new SqlParameter ( "@img_contenttype", SqlDbType.VarChar,50 );
   param2.Value = imgcontenttype;
   command.Parameters.Add( param2 );
   
   connection.Open();
   int numRowsAffected = command.ExecuteNonQuery();
   connection.Close();
   return numRowsAffected;
  }



  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// 
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// 
  private void InitializeComponent()
  {    
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Load += new System.EventHandler(this.Page_Load);



  }
  #endregion



  
 }
}



------------------------------------------------------------



二、浏览图片:
imgvies.aspx文件:
 


 


codebehind文件:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;



namespace study.uploadimage
{
 /// 


 /// imgview 的摘要说明。
 /// 
 public class imgview : System.Web.UI.Page
 {
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   SqlConnection myDSN = new SqlConnection(Application["Test_Conn"].ToString());
   myDSN.Open();



   int imgid = int.Parse(Request.QueryString["id"]);
   string sqlText = "SELECT img_name, img_data, img_contenttype FROM image where img_pk=" + imgid;
   Trace.Write(sqlText);
   SqlCommand MyCommand = new SqlCommand (sqlText, myDSN);
   SqlDataReader dr =MyCommand.ExecuteReader();
   if(dr.Read())
   {
    Response.ContentType = (dr["img_contenttype"].ToString());
    Response.BinaryWrite((byte[])dr["img_data"]);
   }   
   myDSN.Close();
  }



  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// 
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// 
  private void InitializeComponent()
  {    
   this.Load += new System.EventHandler(this.Page_Load);



  }
  #endregion
 }
}




这样这个程序就完成了,简单吧。当然还很多改进之处,希望大家多想想多编编一定可以写出更多的图象上传程序。

相关阅读 Windows错误代码大全 Windows错误代码查询激活windows有什么用Mac QQ和Windows QQ聊天记录怎么合并 Mac QQ和Windows QQ聊天记录Windows 10自动更新怎么关闭 如何关闭Windows 10自动更新windows 10 rs4快速预览版17017下载错误问题Win10秋季创意者更新16291更新了什么 win10 16291更新内容windows10秋季创意者更新时间 windows10秋季创意者更新内容kb3150513补丁更新了什么 Windows 10补丁kb3150513是什么

文章评论
发表评论

热门文章 360快剪辑怎么使用 36金山词霸如何屏幕取词百度收购PPS已敲定!3

最新文章 微信3.6.0测试版更新了微信支付漏洞会造成哪 360快剪辑怎么使用 360快剪辑软件使用方法介酷骑单车是什么 酷骑单车有什么用Apple pay与支付宝有什么区别 Apple pay与贝贝特卖是正品吗 贝贝特卖网可靠吗

人气排行 xp系统停止服务怎么办?xp系统升级win7系统方电脑闹钟怎么设置 win7电脑闹钟怎么设置office2013安装教程图解:手把手教你安装与qq影音闪退怎么办 QQ影音闪退解决方法VeryCD镜像网站逐个数,电驴资料库全集同步推是什么?同步推使用方法介绍QQ2012什么时候出 最新版下载EDiary——一款好用的电子日记本