1

试图将一些历史记录写入访问数据库,但我不断收到一条错误消息,指出the path is invalid. 我正在使用连接字符串,并且从向导本身获取它。复制和粘贴。谁能帮我吗?

谢谢

Imports System.IO
Imports System.Data.OleDb
Public Class theControls

'The History Database Connection String
Dim theHistoryDatabaseConn As New OleDbConnection

Private Sub ComboBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles theAddressBar.KeyDown
    'Navigate to Webpage stated in theAddressBar
    If e.KeyValue = Keys.Enter Then
        theBrowser.Navigate(theAddressBar.Text)
        e.SuppressKeyPress = True
    End If
End Sub

Private Sub goForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles goForward.Click
    theBrowser.GoForward()
End Sub

Private Sub goBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles goBack.Click
    theBrowser.GoBack()
End Sub

Private Sub theBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles theBrowser.DocumentCompleted

    'Set Tab Text to current web page
    Form1.TabControl1.SelectedTab.Text = theBrowser.Url.Host.ToString

    'The History

    theHistoryDatabaseConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\bin\Debug\TheHistoryDB.accdb"


    Dim theCommand As OleDbCommand = New OleDbCommand("INSERT INTO TheHistory ([Site]) VALUES (theBrowser.URL.Host)", theHistoryDatabaseConn)

    theCommand.Parameters.Add("@Site", OleDbType.Char, 255).Value = theBrowser.Url.Host.ToString

    Try
        theHistoryDatabaseConn.Open()
        theCommand.ExecuteNonQuery()

    Catch ex As Exception
        Throw ex
    Finally
        theHistoryDatabaseConn.Close()

    End Try
    theHistoryDatabaseConn.Close()
End Sub

Private Sub theBrowser_ProgressChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs) Handles theBrowser.ProgressChanged

    'Status Bar Text
    Label1.Text = theBrowser.StatusText.ToString
End Sub
End Class
4

2 回答 2

1

您的问题标题为“C:\documents\TheHistoryDB.accdb”,但您的代码显示“|DataDirectory|\bin\Debug\TheHistoryDB.accdb”。你确定你的路径正确吗?

通常用于文档的文件夹类似于“C:\Users\myusername\Documents”。不是说 C:\documents 在您的计算机上不存在,而只是评论它是一个奇怪的地方。

Dim theCommand As OleDbCommand = New OleDbCommand("INSERT INTO TheHistory ([Site]) VALUES (theBrowser.URL.Host.ToString)", theHistoryDatabaseConn)

这无关紧要,但对我来说看起来不正确。看起来您正在尝试访问一个对象,但您在一个字符串中。我是 C# 人,不是 VB。对我来说,这看起来很可疑。另外,您为什么要捕获异常并立即重新抛出它?这不是有点违背目的吗?

于 2013-05-24T03:01:40.763 回答
0

如果您在与应用程序相同的文件夹中有数据库,则更好地使用startuppath

theHistoryDatabaseConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\TheHistoryDB.accdb"
于 2013-05-24T04:01:01.607 回答