VB.NET 操作 ACCESS OLE 欄位內容

來源:互聯網
上載者:User
access 資料庫結構:
--------------------------------------
test.mdb [放在BIN目錄下]
表 test(id 自動編號, img OLE)
----------------------------------------------

代碼:
----------------------------------------------------
使用 一個 openfiledialog ,兩個 picturebox

----------------------------------------------------------------------
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 Button1_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("請先選擇圖片", 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
End Class



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。