从ms访问VB.net应用程序加载图像

时间:2017-07-05 16:46:54

标签: vb.net image ms-access

我知道很多人经常会问这个问题,但我不会问我是否尝试过在本网站上提供的大部分解决方案。

我无法在vb.net中通过ms访问图片框阅读收据图片。

这是我目前的代码。

    Dim i = DataGridView1.CurrentRow.Index
    Dim Dir As String = Path.GetDirectoryName(Application.ExecutablePath) & "\"
    Dim Connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Dir & "DB1.accdb")
    Dim SQL As String = "SELECT Receipt FROM [Inventory] WHERE ID=@DID"
    Dim command As New OleDbCommand(SQL, Connection)
    command.Parameters.AddWithValue("@DID", DataGridView1.Item(0, i).Value)

    Connection.Open()
    Using oleDBCmd As OleDb.OleDbCommand = Connection.CreateCommand()
        oleDBCmd.CommandType = CommandType.Text
        oleDBCmd.CommandText = SQL
        Using oleDbReader As OleDb.OleDbDataReader = oleDBCmd.ExecuteReader()
            oleDbReader.Read()
            Dim ImageBytes As Byte() = CType(oleDbReader(0), Byte())
            Dim ms As New MemoryStream(ImageBytes)
            FormReciept.Show()
            FormReceipt.PictureBox1.Image = Image.FromStream(ms)
        End Using
    End Using
    Connection.Close()

根据特定项目的选定字段,它将根据ID号检索收据,并以图片框所在的另一种形式预览。

这是我得到的错误。

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred 
in System.Data.dll
Additional information: No value given for one or more required parameters.

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

您正在使用参数创建一个命令“command”,然后不使用它...而是创建一个新命令“oleDBCmd”,它没有添加任何参数,这是您实际执行的参数。这就是为什么你得到“没有给定值”的错误。

尝试以下更改,看看是否仍然出现错误,或者收到新错误:

    Dim i = DataGridView1.CurrentRow.Index
    Dim Dir As String = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) & "\"
    Dim Connection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Dir & "DB1.accdb")
    Dim SQL As String = "SELECT Receipt FROM [Inventory] WHERE ID=@DID"
    'Dim command As New OleDb.OleDbCommand(SQL, Connection)
    'command.Parameters.AddWithValue("@DID", DataGridView1.Item(0, i).Value)

    Connection.Open()
    Using oleDBCmd As OleDb.OleDbCommand = Connection.CreateCommand()
        oleDBCmd.CommandType = CommandType.Text
        oleDBCmd.CommandText = SQL
        'add parameter to the command you will actually be executing
        oleDBCmd.Parameters.AddWithValue("@DID", DataGridView1.Item(0, i).Value)
        Using oleDbReader As OleDb.OleDbDataReader = oleDBCmd.ExecuteReader()
            oleDbReader.Read()
            Dim ImageBytes As Byte() = CType(oleDbReader(0), Byte())
            Dim ms As New MemoryStream(ImageBytes)
            FormReciept.Show()
            FormReceipt.PictureBox1.Image = Image.FromStream(ms)
        End Using
    End Using
    Connection.Close()

只是注释掉了“command”oledb命令,因为它没有被执行,并将参数添加到“oleDBCmd”。