Content of the access ole field for VB. NET operations

Source: Internet
Author: User

Database Structure:
--------------------------------------
Test. mdb [put it in the bin directory]
Table Test (ID automatic number, img ole)
----------------------------------------------
Code:
----------------------------------------------------
Use an openfiledialog with two pictureboxes
----------------------------------------------------------------------
Imports system. Io
Public class form1
Inherits system. Windows. Forms. Form
Dim CNN as data. oledb. oledbconnection
Private sub button4_click (byval sender as system. Object, byval e as system. eventargs) handles button4.click
Me. Dispose (true)
End sub
Private sub button#click (byval sender as system. Object, byval e as system. eventargs) handles button1.click
If OpenFileDialog1.ShowDialog = DialogResult. OK Then
PictureBox1.Image = Image. FromFile (OpenFileDialog1.FileName)
End If
End Sub
Private Sub DBInit ()
Try
Cnn = New Data. OleDb. OleDbConnection ("Provider = Microsoft. JET. OLEDB.4.0; Data Source =" & Application. StartupPath & "/test. mdb ")
Cnn. Open ()
Catch exp As OleDb. OleDbException
MsgBox (exp. Message)
End
End Try
End Sub
Private Sub DBRelease ()
Cnn. Close ()
Cnn = Nothing
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 ("select image first", 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 ("image inserted successfully ")
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 ("no image ")
End If
Else
MsgBox ("no data ")
End If
Dr. Close ()
Dr = Nothing
OleCmd = Nothing
DBRelease ()
End Sub
End Class

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.