无法使用VB.net连接到Access

时间:2014-05-24 09:29:22

标签: vb.net

我想为我的应用程序创建一个登录页面。当一切都完成后我想登录它总是显示

错误:

  

Microsoft Jet数据库引擎无法打开文件' C:\ Users \ Gio \ Documents \ Visual Studio 2012 \ Projects \ CSS \ CSS \ bin \ Debug'。它已由其他用户专门打开,或者您需要获得查看其数据的权限。

代码:

Imports System.Data.OleDb

Public Class Login
    Dim path = System.Windows.Forms.Application.StartupPath
    Private Sub Login_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub

Private Sub loginbtn_Click(sender As Object, e As EventArgs) Handles loginbtn.Click
    Dim connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Gio\Documents\Visual Studio 2012\Projects\CSS\CSS\bin\Debug;")
    Dim command As New OleDbCommand("SELECT [ID] FROM [User] WHERE [usernameField] = Username AND [passwordField] = Password", connection)

    Dim usernameparam As New OleDbParameter("Username", Me.usernamebox.Text)
    Dim passwordparam As New OleDbParameter("Password", Me.passwordbox.Text)

    command.Parameters.Add(usernameparam)
    command.Parameters.Add(passwordparam)
    command.Connection.Open()
    Dim reader As OleDbDataReader = command.ExecuteReader()
    If reader.HasRows Then
        MessageBox.Show("Login Succesful!")
        passwordbox.Text = ""
        Me.Hide()
        Main.Show()

    Else
        MessageBox.Show("Username and Password are incorrect!")
        passwordbox.Text = ""
    End If

    command.Connection.Close()
End Sub


Private Sub exitbtn_Click(sender As Object, e As EventArgs) Handles exitbtn.Click
    Me.Close()
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

更改此行

Dim connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Gio\Documents\Visual Studio 2012\Projects\CSS\CSS\bin\Debug;")

Dim connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Gio\Documents\Visual Studio 2012\Projects\CSS\CSS\bin\Debug\YourMSAccessDB.mdb;")

或在输出目录中加载db

Dim connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\YourMSAccessDB.mdb;")
'".\" is equivalent to your output directory or where your application (exe file) is located.