將SQL server2000中保存的圖像顯示在Picture中

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

發佈了21 篇原創文章 · 獲贊 4 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章