中国站长下载-为中国站长提供最好最全的建站资源! 首 页发布资源有事留言繁體中文
设为首页
加入收藏
联系我们
 
您当前的位置:中国站长下载 -> 文章中心 -> 网页编程 -> .NET编程 -> 文章内容  虚拟主机 域名注册 退出登录 用户管理
栏目导航
· ASP编程 · .NET编程
· PHP编程 · JSP编程
· CGI 专区
热门文章
· sndvol32 - sndvol3...
· [组图] FLASH:《大话李白》...
· 个人网站到底能赚多...
· [图文] Rundll.exe是病毒吗...
· [组图] Flash:制作MV
· 价值12万元的网站SE...
· 网站创业者,你需要...
· 一个成功的网站设计...
· [图文] FLASH:韩国导航条解...
· 中国网站的赚钱模式...
相关文章
· 将图片插入数据库并...
· Linux下安PHP,APACH...
将图片插入数据库并使用asp.net读取出来的正确方法(2)
作者:不详  来源:不详  发布时间:2006-8-14 8:36:49  发布人:chinazhan

 减小字体 增大字体

     附录一:
  
  MainForm.cs
  
  using System;
  
  using System.Drawing;
  
  using System.Collections;
  
  using System.ComponentModel;
  
  using System.Data;
  
  using System.Data.SqlClient;
  
  using System.IO;
  
  using System.Windows.Forms;
  
  
  
  namespace InsertImageToDatabase
  
  {
  
  public class MainForm : System.Windows.Forms.Form
  
  {
  
  private System.Windows.Forms.OpenFileDialog openFileDlg;
  
  private System.Windows.Forms.TextBox filePath;
  
  private System.Windows.Forms.Button browseButton;
  
  private System.Windows.Forms.Button insertButton;
  
  /// <summary>
  
  /// Required designer variable.
  
  /// </summary>
  
  private System.ComponentModel.Container components = null;
  
  
  
  public MainForm()
  
  {
  
  //
  
  // Required for Windows Form Designer support
  
  //
  
  InitializeComponent();
  
  
  
  //
  
  // TODO: Add any constructor code after InitializeComponent call
  
  //
  
  }
  
  
  
  /// <summary>
  
  /// Clean up any resources being used.
  
  /// </summary>
  
  protected override void Dispose( bool disposing )
  
  {
  
  if( disposing )
  
  {
  
  if (components != null)
  
  {
  
  components.Dispose();
  
  }
  
  }
  
  base.Dispose( disposing );
  
  }
  
  
  
  #region Windows Form Designer generated code
  
  /// <summary>
  
  /// Required method for Designer support - do not modify
  
  /// the contents of this method with the code editor.
  
  /// </summary>
  
  private void InitializeComponent()
  
  {
  
  this.openFileDlg = new System.Windows.Forms.OpenFileDialog();
  
  this.filePath = new System.Windows.Forms.TextBox();
  
  this.browseButton = new System.Windows.Forms.Button();
  
  this.insertButton = new System.Windows.Forms.Button();
  
  this.SuspendLayout();
  
  //
  
  // openFileDlg
  
  //
  
  this.openFileDlg.DefaultExt = "*.jpg;*.gif;*.bmp;*.png";
  
  this.openFileDlg.Filter = "Image Files|*.jpg;*.gif;*.bmp;*.png|All Files|*.*";
  
  //
  
  // filePath
  
  //
  
  this.filePath.Location = new System.Drawing.Point(16, 16);
  
  this.filePath.Name = "filePath";
  
  this.filePath.ReadOnly = true;
  
  this.filePath.Size = new System.Drawing.Size(168, 20);
  
  this.filePath.TabIndex = 0;
  
  this.filePath.Text = "";
  
  //
  
  // browseButton
  
  //
  
  this.browseButton.Location = new System.Drawing.Point(200, 16);
  
  this.browseButton.Name = "browseButton";
  
  this.browseButton.TabIndex = 1;
  
  this.browseButton.Text = "&Browse";
  
  this.browseButton.Click += new System.EventHandler(this.browseButton_Click);
  
  //
  
  // insertButton
  
  //
  
  this.insertButton.Enabled = false;
  
  this.insertButton.Location = new System.Drawing.Point(200, 56);
  
  this.insertButton.Name = "insertButton";
  
  this.insertButton.TabIndex = 2;
  
  this.insertButton.Text = "&Insert";
  
  this.insertButton.Click += new System.EventHandler(this.insertButton_Click);
  
  //
  
  // MainForm
  
  //
  
  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  
  this.ClientSize = new System.Drawing.Size(292, 273);
  
  this.Controls.Add(this.insertButton);
  
  this.Controls.Add(this.browseButton);
  
  this.Controls.Add(this.filePath);
  
  this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  
  this.MaximizeBox = false;
  
  this.Name = "MainForm";
  
  this.Text = "Insert Image to Database";
  
  this.ResumeLayout(false);
  
  
  
  }
  
  #endregion
  
  
  
  /// <summary>
  
  /// The main entry point for the application.
  
  /// </summary>
  
  [STAThread]
  
  static void Main()
  
  {
  
  Application.Run(new MainForm());
  
  }
  
  
  
  private void browseButton_Click(object sender, System.EventArgs e)
  
  {
  
  if(openFileDlg.ShowDialog()==DialogResult.OK)
  
  {
  
  filePath.Text=openFileDlg.FileName;
  
  insertButton.Enabled=true;
  
  }
  
  }
  
  
  
  private void insertButton_Click(object sender, System.EventArgs e)
  
  {
  
  FileStream fs=File.OpenRead(filePath.Text);
  
  byte[] content=new byte[fs.Length];
  
  fs.Read(content, 0,content.Length);
  
  fs.Close();
  
  
  
  SqlConnection conn=new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DatabaseImage;Data Source=(local)");
  
  conn.Open();
  
  
  
  SqlCommand comm=conn.CreateCommand();
  
  comm.CommandText="insert into Images(Image, contentType) values(@image, @contentType)";
  
  comm.CommandType=CommandType.Text;
  
  
  
  SqlParameter param=comm.Parameters.Add("@image", SqlDbType.Image);
  
  param.Value=content;
  
  comm.Parameters.Add("@contentType", SqlDbType.NVarChar).Value=
  
  GetContentType(new FileInfo(filePath.Text).Extension.Remove(0,1));
  
  
  
  if(comm.ExecuteNonQuery()==1)
  
  {
  
  MessageBox.Show("Successfully insert image into database!");
  
  }
  
  else
  
  {
  
  MessageBox.Show("Failed to insert image into database");
  
  }
  
  
  
  conn.Close();
  
  }
  
  
  
  private string GetContentType(string extension)
  
  {
  
  string type="jpeg";
  
  if(extension=="jpg")
  
  {
  
  type="jpeg";
  
  }else
  
  {
  
  type=extension;
  
  }
  
  return "image/"+type;
  
  }
  
  }
  
  }
  
  
  
  附录二:
  
  ReadImage.aspx
  
  <%@ Page language="c#" Codebehind="ReadImage.aspx.cs" AutoEventWireup="false" Inherits="ReadImage.ReadImage"%>
  
  ReadImage.aspx.cs
  
  using System;
  
  using System.Collections;
  
  using System.ComponentModel;
  
  using System.Data;
  
  using System.Data.SqlClient;
  
  using System.Drawing;
  
  using System.Web;
  
  using System.Web.SessionState;
  
  using System.Web.UI;
  
  using System.Web.UI.WebControls;
  
  using System.Web.UI.HtmlControls;
  
  
  
  namespace ReadImage
  
  {
  
  /// <summary>
  
  /// Summary description for ReadImage.
  
  /// </summary>
  
  public class ReadImage : System.Web.UI.Page
  
  {
  
  private void Page_Load(object sender, System.EventArgs e)
  
  {
  
  try{
  
  SqlConnection conn=new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DatabaseImage;Data Source=(local)");
  
  conn.Open();
  
  
  
  SqlCommand comm=conn.CreateCommand();
  
  comm.CommandText="select * from Images where id>@id";
  
  comm.CommandType=CommandType.Text;
  
  
  
  comm.Parameters.Add("@id", SqlDbType.BigInt).Value=int.Parse(Request["id"]);
  
  
  
  SqlDataReader reader=comm.ExecuteReader();
  
  while(reader.Read())
  
  {
  
  Response.ContentType=reader["contentType"].ToString();
  
  Response.BinaryWrite((byte[])reader["Image"]);
  
  }
  
  Response.Write("aaaaaa");
  
  Response.End();
  
  
  
  conn.Close();
  
  }
  
  catch
  
  {
  
  Response.End();
  
  }
  
  }
  
  
  
  #region Web Form Designer generated code
  
  override protected void OnInit(EventArgs e)
  
  {
  
  //
  
  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  
  //
  
  InitializeComponent();
  
  base.OnInit(e);
  
  }
  
  
  
  /// <summary>
  
  /// Required method for Designer support - do not modify
  
  /// the contents of this method with the code editor.
  
  /// </summary>
  
  private void InitializeComponent()
  
  {
  
  this.Load += new System.EventHandler(this.Page_Load);
  
  
  
  }
  
  #endregion
  
  }
  
  }  做人要厚道,请注明转自chinazhan中国站长(www.ChinaZhan.com)。

 
[] [返回上一页] [打 印] [收 藏]
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论...]
中国站长下载
中国站长下载

本页只接受PR>=4 IT类站点连接,申请连接,谢谢您们的支持!希望我们的下载站能够真正帮到中国的站长们!
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图
Copyright © 2005-2006 ChinaZhan.Net. All Rights Reserved .