C/S 圖片操作

來源:互聯網
上載者:User

從資料庫中讀取:
方法一:
SqlCommand Cmd = new SqlCommand("SELECT * FROM student", MyCn);
SqlDataAdapter da = new SqlDataAdapter(Cmd);
DataSet ds = new DataSet();
da.Fill(ds, "student");
Byte[] byteBLOBData =  new Byte[0];
byteBLOBData = (Byte[])(ds.Tables["student"].Rows[index]["ZP"]);
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
this.pictureBox2.Image= Image.FromStream(stmBLOBData);

方法二:
OleDbDataAdapter MyDa=new OleDbDataAdapter("select ZP from picture",MyCn);
   DataSet ds=new DataSet();
   MyDa.Fill(ds,"picture");
foreach(DataRow rFirst in ds.Tables["picture"].Rows)
    {
     bData=(byte[])rFirst["ZP"];
    }

MemoryStream stream=new MemoryStream(bData,true);
//MemoryStream stream=new MemoryStream(bData);
    // Create a bitmap from the stream
    Bitmap bmp = new Bitmap(stream);

    try
    {
     //Showing
     this.pictureBox1.Image=bmp;
     //this.pictureBox1.Image=Image.FromStream(stream);
    }
    catch{};
    // Close the stream
    stream.Close();

=========================================================================
插入資料庫中:
SqlCommand MyCmd=new SqlCommand("INSERT INTO student(XH,ZP) VALUES(@XH,@ZP)",MyCn);

   //Read jpg into file stream, and from there into Byte array.
   FileStream fsBLOBFile =  new FileStream(strBLOBFilePath,FileMode.Open, FileAccess.Read);
   Byte[] bytBLOBData = new Byte[fsBLOBFile.Length];
   fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length);
   fsBLOBFile.Close();

   MyCmd.Parameters.Add("@XH",this.txtBox_XH.Text.Trim());

   //Create parameter for insert command and add to SqlCommand object.
   SqlParameter prm = new  SqlParameter("@ZP", SqlDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false,0, 0, null, DataRowVersion.Current, bytBLOBData);
   MyCmd.Parameters.Add(prm);

   
   try
   {
    MyCn.Open();
    MyCmd.ExecuteNonQuery();
    MessageBox.Show("儲存到資料庫成功!","提示");
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message);
   }
   MyCn.Close();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.