无法从mdb数据库中检索图像此程序显示错误“参数无效”

时间:2015-09-22 06:47:37

标签: asp.net vb.net

 Try
            'connection string
            Dim dbpath As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
            dbpath = New Uri(dbpath).LocalPath
            Dim my_connection As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=D:\DataBase\KhandagramPS.mdb"
            Dim userTables As DataTable = Nothing
            Dim connection As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection()
            Dim DR As OleDbDataReader

        'Dim source As String

        'query string
        Dim my_query As String = "SELECT sl_no,f_name,dob,sex,add,reg,[class],a_date,a_per,r_a_per,f_status,[name],photo,document FROM " & TextBox2.Text & " where sl_no=" & TextBox3.Text & " ;"

        'create a connection
        Dim my_dbConnection As New OleDbConnection(my_connection)
        my_dbConnection.Open()


        'create a command
        Dim my_Command As New OleDbCommand(my_query, my_dbConnection)


        DR = my_Command.ExecuteReader()


        While (DR.Read())
            txtslno.Text = (DR(0).ToString())
            txtfname.Text = (DR(1).ToString())
            Date1.Text = (DR(2).ToString())
            comsex.Text = (DR(3).ToString())
            txtadd.Text = (DR(4).ToString())
            txtreg.Text = (DR(5).ToString())
            comclass.Text = (DR(6).ToString())
            Date2.Text = (DR(7).ToString())
            txtaper.Text = (DR(8).ToString())
            txtraper.Text = (DR(9).ToString())
            txtfstatus.Text = (DR(10).ToString())
            txtname.Text = (DR(11).ToString())
            Dim ImageBuffer = CType(DR(12), Byte())
            Dim imgStrm As New MemoryStream(ImageBuffer, True)
            imgStrm.Write(ImageBuffer, 0, ImageBuffer.Length)
            Dim img As Image = Image.FromStream(imgStrm)
            PictureBox1.Image = img
        End While

        'close connection
        my_dbConnection.Close()

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

2 个答案:

答案 0 :(得分:0)

我怀疑你的Byte-Array到Image转换有些错误。确保您的Byte-Array没有搞砸。请改用此功能。

Private Function getImage(imageBuffer as Byte())
   Using ms as new IO.MemoryStream(imageBuffer)
       Dim img = ImageIO.FromStream(ms)
       Return img
   End Using
End Function

答案 1 :(得分:0)

我的2美分:你不应该将图像存储在mdb文件中(除非在过去的几年里发生了很多变化)。将图像存储在单独的目录中,并将文件 路径 存储在mdb中,以便在需要时找到图像。