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

时间:2015-01-22 09:55:58

标签: vb.net ms-access

[错误]空路径名称不合法          问题是我无法将图像插入Access数据库。哪条线是错误的。在此先感谢谁帮助我

此致 Fizul

    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()

1 个答案:

答案 0 :(得分:0)

问题在于前两行:

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语句确保正确放置对话框。