在VB.net中如何将图片转换成二进制保存到Accees中
发布网友
发布时间:2023-09-03 07:19
我来回答
共2个回答
热心网友
时间:2024-11-28 05:35
在access数据库里将字段的类型设置为ole对象
Public img As Byte() '图片处理用的字节数组
img=My.Computer.FileSystem.ReadAllBytes(filePath)'filePath是你图片文件的路径
剩下的就是数据库插入操作了
Dim cn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Data.mdb")
Dim comm As OleDb.OleDbCommand
comm = New OleDb.OleDbCommand( _
"INSERT INTO Photo(BuFan_F,PhotoNo,Photo) Values('" & Me.CobBuFan.Text.Trim & "','" & Me.txtNo.Text.Trim & "',@image)", cn)
'向数据库添加存储了图片数据的二进制数组
comm.Parameters.Add("@image", _
OleDb.OleDbType.Binary, img.Length).Value = img
If cn.State = ConnectionState.Closed Then cn.Open() '打开数据库连接
comm.ExecuteNonQuery() '执行数据库命令
If cn.State = ConnectionState.Open Then cn.Close() '关闭数据库连接
MessageBox.Show("图片成功保存到数据库", "完成", MessageBoxButtons.OK, MessageBoxIcon.Information)
热心网友
时间:2024-11-28 05:36
'数据库里图片字段的类型设置为ole对象
'引用0bjects 2.5
Dim cn As ADODB.Connection
Dim rS As ADODB.Recordset
Dim mstream As ADODB.Stream
Dim SQL, DataPath As String
Private Sub Command1_Click()
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\db1.mdb;"
Set rS = New ADODB.Recordset
SQL = "Select * from bmp表"
rS.Open SQL, cn, adOpenKeyset, adLockOptimistic
Set mstream = New ADODB.Stream
mstream.Type = adTypeBinary
mstream.Open
On Error GoTo jpg
mstream.LoadFromFile App.Path + "\图片.jpg"
rS.AddNew
rS.Fields("图片名") = Left(Dir(App.Path + "\图片.jpg"), Len(Dir(App.Path + "\图片.jpg")) - 4)
rS.Fields("bmp").Value = mstream.Read '二进制文件
rS.Update
MsgBox "图片保存结束!", , App.EXEName
Set rS = Nothing
Set cn = Nothing
Exit Sub
jpg:
MsgBox "图片保存失败!", vbExclamation, App.EXEName
Set rS = Nothing
Set cn = Nothing
End Sub
479628748