vb.net '保存图片 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If PictureBox1.Image Is Nothing Then MsgBox("请先选择图片", MsgBoxStyle.Exclamation) Exit Sub End If Dim fs As FileStream = New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read) Dim bt(fs.Length) As Byte fs.Read(bt, 0, fs.Length) fs.Close() fs = Nothing Dim oleCmd As OleDb.OleDbCommand = New OleDb.OleDbCommand DBInit() oleCmd.Connection = cnn oleCmd.CommandType = CommandType.Text oleCmd.CommandText = "INSERT INTO test (img) VALUES (@img)" oleCmd.Parameters.Add("@img", OleDb.OleDbType.Binary).Value = bt oleCmd.ExecuteNonQuery() oleCmd = Nothing DBRelease() MsgBox("图片插入成功") End Sub'读取图片 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim oleCmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("SELECT img FROM test WHERE id=1") oleCmd.CommandType = CommandType.Text DBInit() oleCmd.Connection = cnn Dim dr As OleDb.OleDbDataReader = oleCmd.ExecuteReader(CommandBehavior.SingleRow) If dr.Read Then If Not IsDBNull(dr.Item(0)) Then Dim bt() As Byte = dr.Item(0) Dim ms As MemoryStream = New MemoryStream(bt) PictureBox2.Image = Image.FromStream(ms) Else MsgBox("无图片") End If Else MsgBox("无数据") End If dr.Close() dr = Nothing oleCmd = Nothing DBRelease() End Sub
'保存图片
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If PictureBox1.Image Is Nothing Then
MsgBox("请先选择图片", MsgBoxStyle.Exclamation)
Exit Sub
End If
Dim fs As FileStream = New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
Dim bt(fs.Length) As Byte
fs.Read(bt, 0, fs.Length)
fs.Close()
fs = Nothing
Dim oleCmd As OleDb.OleDbCommand = New OleDb.OleDbCommand
DBInit()
oleCmd.Connection = cnn
oleCmd.CommandType = CommandType.Text
oleCmd.CommandText = "INSERT INTO test (img) VALUES (@img)"
oleCmd.Parameters.Add("@img", OleDb.OleDbType.Binary).Value = bt
oleCmd.ExecuteNonQuery()
oleCmd = Nothing
DBRelease()
MsgBox("图片插入成功")
End Sub'读取图片
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim oleCmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("SELECT img FROM test WHERE id=1")
oleCmd.CommandType = CommandType.Text
DBInit()
oleCmd.Connection = cnn
Dim dr As OleDb.OleDbDataReader = oleCmd.ExecuteReader(CommandBehavior.SingleRow)
If dr.Read Then
If Not IsDBNull(dr.Item(0)) Then
Dim bt() As Byte = dr.Item(0)
Dim ms As MemoryStream = New MemoryStream(bt)
PictureBox2.Image = Image.FromStream(ms)
Else
MsgBox("无图片")
End If
Else
MsgBox("无数据")
End If
dr.Close()
dr = Nothing
oleCmd = Nothing
DBRelease()
End Sub