上一节我们看到在程序运行的时候出现了错误,为什么读取二进制数据会出现错误呢?@H_403_1@
我重新改写了一下代码,测试读取的二进制数据:@H_403_1@
Private Sub Button4_Click(sender As Object,e As EventArgs) Handles Button4.Click Dim odc As New OleDbConnection() odc.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0;data source=D:\save\博客教程\08 数据库\Northwind.mdb" Dim sql As String = "select 图片 from 类别 where 类别ID=3" Dim imageCmd As New OleDbCommand(sql,odc) Dim datareader As OleDbDataReader odc.Open() Try datareader = imageCmd.ExecuteReader(CommandBehavior.SequentialAccess And CommandBehavior.SingleResult) Dim readerData() As Byte datareader.Read() readerData = datareader(0) ''读取图片二进制数据 Dim sw As New StreamWriter("d:\ddd.txt",False) Dim hexData As String For i As Integer = 0 To readerData.Length - 1 If (i + 1) Mod 8 = 0 Then hexData = Hex(readerData(i)) sw.Write(IIf(hexData.Length = 1,"0" & hexData,hexData)) sw.Write(ControlChars.CrLf) Else hexData = Hex(readerData(i)) sw.Write(IIf(hexData.Length = 1,hexData) & " ") End If Next sw.Close() Catch ex As Exception MessageBox.Show(ex.Message) Finally datareader.Close() odc.Close() End Try End Sub于是在D盘下创建了一个ddd.txt,打开文件可以看到我们已经将数据成功读出并写入了该文件
根据BMP文件的格式可知,BMP前两个字节是BMP文件头,为"BM",对应的十六进制值为 42 4D@H_403_1@
可是我们获得的数据前两个字节并不是424D,但是经过仔细查看会发现数据中间有42 4D 出现在第79字节处:@H_403_1@
@H_403_1@
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。@H_403_1@
学习更多vb.net知识,请参看vb.net 教程 目录@H_403_1@ 原文链接:https://www.f2er.com/vb/256245.html