数据库存取图片

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();
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值