原理很簡單,就是先把圖檔轉成二進位再上傳就行了
程式碼如下
Dim db As New StudnoListDataContext
'把要更新的資料Select出來
Dim StudClass = (From s In db.Stud Where s.stud_no = studno.Text).First
'把PictureBox.Image放到Bitmap
Dim bmp As Bitmap = PictureBox1.Image
'宣告一個MemoryStream
Dim st As New MemoryStream
'把圖片放到MemoryStream裡面
bmp.Save(st, System.Drawing.Imaging.ImageFormat.Jpeg)
st.Position = 0
'把MemoryStream裡的資料轉成二進位
Dim WriteByte(st.Length - 1) As Byte
If st.CanRead Then
st.Read(WriteByte, 0, st.Length)
st.Close()
End If
'把WriteByte放到Linq的二進位欄位
StudClass.studimg = WriteByte
'更新上去
db.SubmitChanges()