转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 数据库 >> Sql Server >> 正文
利用C#在SQL Server2000存取图像 For Window         

利用C#在SQL Server2000存取图像 For Window

作者:闵涛 文章来源:闵涛的学习笔记 点击数:3053 更新时间:2007/11/14 11:06:26
   //
   this.pictureBox1.ContextMenu = this.contextMenu1;
   this.pictureBox1.Location = new System.Drawing.Point(0, 0);
   this.pictureBox1.Name = "pictureBox1";
   this.pictureBox1.Size = new System.Drawing.Size(264, 192);
   this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
   this.pictureBox1.TabIndex = 0;
   this.pictureBox1.TabStop = false;
   //
   // menuItem2
   //
   this.menuItem2.Index = 1;
   this.menuItem2.Text = "缩小";
   //this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
   //
   // menuItem3
   //
   this.menuItem3.Index = 2;
   this.menuItem3.Text = "放大";
   //this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(648, 421);
   this.Controls.Add(this.panel2);
   this.Controls.Add(this.panel1);
   this.Controls.Add(this.splitter1);
   this.Controls.Add(this.listBox1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.panel1.ResumeLayout(false);
   this.panel2.ResumeLayout(false);
   this.ResumeLayout(false);

  }
  #endregion

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


  private void Form1_Load(object sender, System.EventArgs e)
  {
   string connString="";
   connString = @"Persist Security Info=False;User ID=sa;Initial Catalog=work;Data Source=(local)";
   this.sqlConnection1.ConnectionString = connString;
   this.FillListBox();
   if (this.listBox1.Items.Count > 0) this.listBox1.SetSelected(0,true);
  }

  private void FillListBox()
  {
   this.listBox1.SelectedIndexChanged -= new System.EventHandler(this.listBox1_SelectedIndexChanged);
   string comm = "select id,name from table1";
   System.Data.DataTable dt = new DataTable("imgTable");
   this.sqlCommand1.CommandType = System.Data.CommandType.Text ;
   this.sqlCommand1.CommandText = comm;
   this.sqlCommand1.Connection = this.sqlConnection1 ;
   System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(this.sqlCommand1);
   this.sqlConnection1.Open();
   da.Fill(dt);
   this.sqlConnection1.Close();
   this.listBox1.DataSource = dt;
   this.listBox1.DisplayMember = "name";
   this.listBox1.ValueMember = "id";
   this.listBox1.Refresh();
   this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
  }

  //将SQL server2000中保存的图像显示在Picture中
  private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   byte[] buffByte = null;
   string comm = @"select img from table1 where id = " + this.listBox1.SelectedValue ;
   this.sqlCommand1 = new System.Data.SqlClient.SqlCommand ();
   this.sqlCommand1.CommandType = System.Data.CommandType.Text ;
   this.sqlCommand1.CommandText = comm;
   this.sqlCommand1.Connection = this.sqlConnection1 ;
   this.sqlConnection1.Open();
   System.Data.SqlClient.SqlDataReader rd = this.sqlCommand1.ExecuteReader();
   while (rd.Read())
   {
    buffByte = ((byte[])rd[0]);
   }
   rd.Close();
   this.sqlConnection1.Close();
   //将图像的字节数组放入内存流
   System.IO.MemoryStream ms = new System.IO.MemoryStream(buffByte);
   //通过流对象建立Bitmap
   System.Drawing.Bitmap bmp = new Bitmap(ms);
   this.pictureBox1.Image = bmp;
  }
  
  //将图像保存到SQL server2000的Image字段中
  private void button2_Click_1(object sender, System.EventArgs e)
  {
   string pathName;
   if (this.openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
   {
    pathName = this.openFileDialog1.FileName;
    System.Drawing.Image img = System.Drawing.Image.FromFile(pathName);
    this.pictureBox1.Image = img;
    
    //将图像读入到字节数组
    System.IO.FileStream fs = new System.IO.FileStream(pathName,System.IO.FileMode.Open,System.IO.FileAccess.Read);
    byte[] buffByte = new byte[fs.Length];
    fs.Read(buffByte,0,(int)fs.Length);
    fs.Close();
    fs = null;
    
    //建立Command命令
    string comm = @"Insert into table1(img,name) values(@img,@name)";
    this.sqlCommand1 = new System.Data.SqlClient.SqlCommand ();
    this.sqlCommand1.CommandType = System.Data.CommandType.Text ;
    this.sqlCommand1.CommandText = comm;
    this.sqlCommand1.Connection = this.sqlConnection1 ;
    //创建Parameter
    this.sqlCommand1.Parameters.Add("@img",System.Data.SqlDbType.Image);
    this.sqlCommand1.Parameters[0].Value = buffByte;
    this.sqlCommand1.Parameters.Add("@name",System.Data.SqlDbType.VarChar);
    this.sqlCommand1.Parameters[1].Value =pathName.Substring(pathName.LastIndexOf("
\\")+1);
    try
    {
     this.sqlConnection1.Open();
     this.sqlCommand1.ExecuteNonQuery();
     this.sqlConnection1.Close();
    }
    catch(System.Exception ee)
    {
     MessageBox.Show(ee.Message );
    }
    buffByte = null;

    this.FillListBox();
   }
  }

//  private void menuItem1_Click(object sender, System.EventArgs e)
//  {
//   this.saveFileDialog1.FileName = ((System.Data.DataRowView)this.listBox1.SelectedItem)[1].ToString();
//   this.saveFileDialog1.Filter = "JPG (*.jpg)|*.jpg|GIF (*.gif)|*.gif|位图(*.bmp)|*.bmp";
//   if (this.saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
/

上一页  [1] [2] [3]  下一页


[系统软件]InstallShield Express for delphi制作安装程序定…  [系统软件](转帖) 忘记登录密码的解决方案 for XP/2003/2000
[系统软件]交叉编译sshd for IXP425 ARM  Platform by MVL 3…  [Delphi程序]《关于VisiBroker For Delphi的使用》(4)
[Delphi程序]一个超链接Image控件!(For D3,D4,D5,D6)源代码  [Delphi程序]Five of the best tools for Delphi
[Delphi程序]《关于VisiBroker For Delphi的使用》(3)  [Delphi程序]《关于VisiBroker For Delphi的使用》(2)
[Delphi程序]《关于VisiBroker For Delphi的使用》  [Delphi程序]Delphi for .Net 编译器预览 - by John Kaster
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Sql Server  · MySql
    · Access  · ORACLE
    · SyBase  · 其他
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台