代码运行能够插入图片,但是插入的时候是直接插到空的字段(第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