【问题标题】:Cannot connect to Access using VB.net无法使用 VB.net 连接到 Access
【发布时间】:2014-05-24 09:29:22
【问题描述】:

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

错误:

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

【问题讨论】:

  • 你是用ms access独占打开access数据库的吗?
  • 如何检查我是否有玉? @jay 感谢您的编辑。这个网站还是新手 :)
  • 抱歉,您没有在连接字符串中指定 ms 访问文件名。
  • 啊,是的,你是对的。但是在我指定它之后,错误发生了变化。它变成了找不到文件'C:\Users\Gio\Documents\Visual Studio 2012\Projects\CSS\CSS\bin\Debug\CSS.mdb'。
  • 使用 Windows 资源管理器浏览这个 'C:\Users\Gio\Documents\Visual Studio 2012\Projects\CSS\CSS\bin\Debug' 并查看 CSS.mdb 文件是否存在,否则你需要将文件复制到那里。

标签: vb.net


【解决方案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;")

或者在你的输出目录中加载数据库

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.

【讨论】:

    猜你喜欢
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    • 2011-08-03
    • 2013-04-13
    相关资源
    最近更新 更多