空路径名不合法 (vb.net 2005 ,access 2000,)

empty path name is not legal (vb.net 2005 ,access 2000,)

[错误]空路径名不合法 问题是我无法将图像插入到 Access 数据库中。哪一行是错误的。提前感谢帮助我的人

此致, 菲祖尔

    Dim OpenFileDialog1 As New OpenFileDialog
    Dim fsreader As New IO.FileStream(OpenFileDialog1.FileName,IO.FileMode.Open, IO.FileAccess.Read)
    Dim breader As New IO.BinaryReader(fsreader)

    Dim imgbuffer(fsreader.Length) As Byte
    breader.Read(imgbuffer, 0, fsreader.Length)
    fsreader.Close()

    cnn.ConnectionString = "provider=microsoft.ace.oledb.12.0; data source = |datadirectory|\db1.accdb;"
    cnn.Open()
    Dim sql As String
    sql = "insert into Table1 Values(" & TextBox1.Text & ",'" & imgbuffer.Length & "')"
    Dim cmd As New OleDb.OleDbCommand(sql, cnn)
    cmd.ExecuteNonQuery()
    cmd.Dispose()
    cnn.Close()

问题出在前两行:

Dim OpenFileDialog1 As New OpenFileDialog
Dim fsreader As New IO.FileStream(OpenFileDialog1.FileName,IO.FileMode.Open, IO.FileAccess.Read)

您创建了一个 OpenFileDialog 实例,但您从未调用它的 ShowDialog 方法,因此 FileName 属性 为 Nothing。你需要这样的东西:

Using OpenFileDialog1 As New OpenFileDialog
    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
        Dim fsreader As New IO.FileStream(OpenFileDialog1.FileName,IO.FileMode.Open, IO.FileAccess.Read)
        'remaining of code here
    End If
End Using

Using 语句确保对话框被正确处理。