qq_36805380
qq_36805380
采纳率100%
2016-12-07 12:21

vb.net向access插入图片问题

10
  • 希望有大神解惑
  • net
  • 谢谢了!

图片说明代码运行能够插入图片,但是插入的时候是直接插到空的字段(第9、10行)了,求教!
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 TestImg (img) VALUES (@img)"
oleCmd.Parameters.Add("@img", OleDb.OleDbType.Binary).Value = bt
oleCmd.ExecuteNonQuery()
oleCmd = Nothing
DBRelease()
MsgBox("图片插入成功")
End Sub

Private Sub DBInit() '定义
Try
Cnn = New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & Application.StartupPath & "\Test.mdb;User Id=Admin;Jet OLEDB:Database Password=;")
cnn.Open()
Catch exp As OleDb.OleDbException
MsgBox(exp.Message)
End
End Try
End Sub
Private Sub DBRelease()
cnn.Close()
cnn = Nothing
End Sub

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答