简体   繁体   English

'C:\\ documents \\ TheHistoryDB.accdb'不是有效路径

[英]'C:\documents\TheHistoryDB.accdb' is not a valid path

Trying to write some history to an access datebase but I keep getting an error stating that the path is invalid . 试图向访问数据库中写入一些历史记录,但是我不断收到错误消息,指出the path is invalid I'm using a connection string and I'm getting it from the wizard itself. 我使用的是连接字符串,并且是从向导本身获取的。 Copy and Paste. 复制和粘贴。 Can anyone help me out? 谁能帮我吗?

Thanks 谢谢

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

The title of your question says "C:\\documents\\TheHistoryDB.accdb" but your code is showing "|DataDirectory|\\bin\\Debug\\TheHistoryDB.accdb". 您的问题的标题为“ C:\\ documents \\ TheHistoryDB.accdb”,但是您的代码显示为“ | DataDirectory | \\ bin \\ Debug \\ TheHistoryDB.accdb”。 Are you sure you have your path correct? 您确定您的路径正确吗?

Normally the folder used for documents would be something like "C:\\Users\\myusername\\Documents". 通常,用于文档的文件夹将类似于“ C:\\ Users \\ myusername \\ Documents”。 Not saying C:\\documents doesn't exist on your computer, but just commenting that's an odd place for it. 不是说您的计算机上不存在C:\\ documents,而只是说那是一个奇怪的地方。

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

This is unrelated, but it doesn't look right to me. 这无关紧要,但对我来说似乎不合适。 It looks like you're trying to access an object, but you're inside a string. 看起来您正在尝试访问对象,但是您位于字符串中。 I'm a C# guy, not VB. 我是C#人,不是VB。 It just looks fishy to me. 在我看来,这真是可恶。 Also, why are you catching the Exception and immediately re-throwing it? 另外,为什么要捕获异常并立即将其重新抛出? Doesn't it kind of defeat the purpose? 难道不是要破坏目的吗?

如果数据库与应用程序位于同一文件夹中,则最好使用startuppath

theHistoryDatabaseConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\TheHistoryDB.accdb"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM