数据库存取图片

原创 2006年05月17日 23:36:00

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace 数据库相片
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.PictureBox pictureBox1;
  private System.Windows.Forms.PictureBox pictureBox2;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.OpenFileDialog openFileDialog1;
  private System.Windows.Forms.Button button3;
  private System.Data.SqlClient.SqlConnection sqlConnection1;
  private System.Windows.Forms.Button button4;
  private System.Windows.Forms.TextBox textBox1;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.pictureBox1 = new System.Windows.Forms.PictureBox();
   this.pictureBox2 = new System.Windows.Forms.PictureBox();
   this.button1 = new System.Windows.Forms.Button();
   this.button2 = new System.Windows.Forms.Button();
   this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
   this.button3 = new System.Windows.Forms.Button();
   this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
   this.button4 = new System.Windows.Forms.Button();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.SuspendLayout();
   //
   // pictureBox1
   //
   this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
   this.pictureBox1.Location = new System.Drawing.Point(16, 40);
   this.pictureBox1.Name = "pictureBox1";
   this.pictureBox1.Size = new System.Drawing.Size(104, 144);
   this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
   this.pictureBox1.TabIndex = 0;
   this.pictureBox1.TabStop = false;
   //
   // pictureBox2
   //
   this.pictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
   this.pictureBox2.Location = new System.Drawing.Point(200, 40);
   this.pictureBox2.Name = "pictureBox2";
   this.pictureBox2.Size = new System.Drawing.Size(104, 144);
   this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
   this.pictureBox2.TabIndex = 1;
   this.pictureBox2.TabStop = false;
   //
   // button1
   //
   this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
   this.button1.Location = new System.Drawing.Point(32, 8);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(48, 23);
   this.button1.TabIndex = 2;
   this.button1.Text = "保存相片";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // button2
   //
   this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
   this.button2.Location = new System.Drawing.Point(240, 8);
   this.button2.Name = "button2";
   this.button2.Size = new System.Drawing.Size(48, 23);
   this.button2.TabIndex = 3;
   this.button2.Text = "提取相片";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // button3
   //
   this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
   this.button3.Location = new System.Drawing.Point(16, 8);
   this.button3.Name = "button3";
   this.button3.Size = new System.Drawing.Size(8, 23);
   this.button3.TabIndex = 4;
   this.button3.Text = "选择相片";
   this.button3.Visible = false;
   this.button3.Click += new System.EventHandler(this.button3_Click);
   //
   // sqlConnection1
   //
   this.sqlConnection1.ConnectionString = "workstation id=E8CF7AF1B27E44A;packet size=4096;user id=sa;initial catalog=BookMa" +
    "nager;persist security info=False";
   //
   // button4
   //
   this.button4.Location = new System.Drawing.Point(336, 8);
   this.button4.Name = "button4";
   this.button4.Size = new System.Drawing.Size(75, 168);
   this.button4.TabIndex = 5;
   this.button4.Text = "换页";
   this.button4.Click += new System.EventHandler(this.button4_Click);
   //
   // textBox1
   //
   this.textBox1.Location = new System.Drawing.Point(120, 8);
   this.textBox1.Name = "textBox1";
   this.textBox1.Size = new System.Drawing.Size(96, 21);
   this.textBox1.TabIndex = 6;
   this.textBox1.Text = "";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(336, 190);
   this.Controls.Add(this.textBox1);
   this.Controls.Add(this.button4);
   this.Controls.Add(this.button3);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.pictureBox2);
   this.Controls.Add(this.pictureBox1);
   this.Name = "Form1";
   this.Text = "数据库相片问题";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void button3_Click(object sender, System.EventArgs e)
  {
   
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
  
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
   if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
   {
    Image img=Image.FromFile(this.openFileDialog1.FileName);
    this.pictureBox1.Image=img;
    
    FileInfo obj=new FileInfo(this.openFileDialog1.FileName);
    byte[] Content =new byte[obj.Length];
    FileStream imgStream=obj.OpenRead();
    //把文件内容读入字节数组
    imgStream.Read(Content,0,Content.Length);
    imgStream.Close();
    obj=null;
    System.Data.SqlClient.SqlCommand cmd=new SqlCommand();
    System.Data.SqlClient.SqlParameter pName=new SqlParameter("@借书证号",Type.GetType("System.Data.SqlDbType.NVarChar"));
    pName.Value=this.textBox1.Text;
    System.Data.SqlClient.SqlParameter pImg=new SqlParameter("@相片",Type.GetType("System.Data.SqlDbType.Image"));
    pImg.Value=Content;
    cmd.CommandText="Insert into 读者相片表 values(@借书证号,@相片)";
    cmd.Parameters.Add(pName);
    cmd.Parameters.Add(pImg);
    cmd.Connection=this.sqlConnection1;
    this.sqlConnection1.Open();
    cmd.ExecuteNonQuery();
    this.sqlConnection1.Close();
   }
  }

  private void button2_Click(object sender, System.EventArgs e)
  {
   byte[] buffByte = null;
   string sql="select 相片 from 读者相片表 where 借书证号='"+this.textBox1.Text+"'";
   SqlCommand cmd=new SqlCommand(sql,this.sqlConnection1);
   cmd.CommandType=CommandType.Text;
   this.sqlConnection1.Open();
   SqlDataReader dr=cmd.ExecuteReader();
   dr.Read();
   buffByte = ((byte[])dr["相片"]);
   MemoryStream ms=new MemoryStream((byte[])dr["相片"]);
   System.Drawing.Image image=System.Drawing.Image.FromStream(ms,true);
   this.pictureBox2.Image = image;
   dr.Close();
   this.sqlConnection1.Close();
  }

  private void button4_Click(object sender, System.EventArgs e)
  {
   Form frm=new Form2();
   frm.ShowDialog();
  }
 }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

存取数据库图片

  • 2013-03-26 11:21
  • 23.63MB
  • 下载

小谈c#数据库存取图片的三种方式

第一种方式 文件夹与数据库配合近来做了不少关于这块的功能 ,随着网络的飞速发展,网络存取图片已不再是神话,而成为了一种时尚,如果是你 是用Asp.net开发的话,可能更多的人会考虑使用数据库存储图片的...

Android 数据库存取图片

数据库中存取图片通常使用两种方式,一种是保存图片所在路径,二是将图片以二进制的形式存储(sqlite3支持BLOB数据类型)。对于两种方法的使用,好像第二种方法不如第一种方法更受程序员欢迎,他们认为,...

图片以二进制存取数据库

  • 2007-12-18 11:04
  • 267KB
  • 下载

图片文件的数据库存取

首先,我们要明白图片是以二进制的形式保存在数据库中的,那么把图片保存到数据库中的步骤大体上有这几步1.将图片转换为二进制数组(byte[]);2.把转换后的二进制数组(byte[])作为参数传递给要执...
  • moust
  • moust
  • 2011-06-22 12:51
  • 189

vc 存取jpg图片到数据库

  • 2006-02-23 09:05
  • 298KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)