利用C#在SQL Server2000存取映像(摘抄)

來源:互聯網
上載者:User
 //將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();
   }
  }

 

原文出處:http://blog.codelphi.com/miracle/articles/6053.aspx?Pending=true

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.