System.Data.OleDb.OleDbException' 发生在 System.Data.dll

System.Data.OleDb.OleDbException' occurred in System.Data.dll

大家好,我是 C# 语言和 Visual Studio 平台的新手,最近我正在学习如何使用 visual Studio 连接访问数据库,我第一次使用相同的代码连接了数据库,但过了一段时间当我再次编译时,标题中给出了错误。 为什么会这样?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Clinic_Management_System
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();

        }

        private void Login_Load(object sender, EventArgs e)
        {
            try
            {

                OleDbConnection connection = new OleDbConnection();
                connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users
                \Hassan Gillani\Documents\Clinic Management System.accdb;  Persist Security Info = False; ";
                connection.Open();
                label1.Text = "Connected to Clinic Management System Database";
                connection.Close();
            }
            catch (Exception exp)
            {

                MessageBox.Show("Error " + exp);
            }

        }
    }
}

请访问给定喜欢看屏短 http://s33.postimg.org/5ltm4dtnj/Error.png

使用逐字字符 (@) 并在中间拆分路径不是一个好主意。
路径中的空格很重要,因此用于连接的文件名是

 C:\Users                \Hassan Gillani\Documents\Clinic Management System.accdb;  

如果您尝试在此字符串上使用 File.Exists,您会得到错误的结果。

不要在路径中间拆分连接字符串

   connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;
    Data Source=C:\Users\Hassan Gillani\Documents\Clinic Management System.accdb;  
    Persist Security Info = False; ";