VB.NET 操作 ACCESS OLE 字段内容

王朝vb·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

数据库结构:

--------------------------------------

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航