繁体   English   中英

C#System.Data.OleDb.OleDbException

[英]C# System.Data.OleDb.OleDbException

嗨,我是C#的新手,正在尝试连接到.accdb Access 2010数据库

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            OleDbConnection connect = new OleDbConnection();
            connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Web Develop\Documents\Storekeeper\storekeeper.accdb;Persist Security Info=False;";
            connect.Open();
            MessageBox.Show("Connection open");
        }
    }
}

我得到这个异常:

System.Data.OleDb.OleDbException类型的第一次机会异常发生在System.Data.dll中

数据库未使用且路径正确我该怎么办?

您可以检查引发的异常上的InnerException属性。 它会告诉您确切的错误是什么。 要查看它,您需要捕获异常,然后显示InnerException消息:

private void Form1_Load(object sender, EventArgs e)
{
    try
    {
        OleDbConnection connect = new OleDbConnection();
        connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Web Develop\Documents\Storekeeper\storekeeper.accdb;Persist Security Info=False;";
        connect.Open();
        MessageBox.Show("Connection open");
    }
    catch (OleDbException e)
    {
        Messagebox.Show(e.InnerException.Message);
    }
}

在MSDN页面的OleDbException上还有捕获和显示嵌入在OleDbException中的错误的其他示例代码。

我认为,这很简单。 自从您在Office 2010中工作以来,我相信您需要:Microsoft.ACE.OLEDB。 14 .0

好。 如果您在64位O / S上具有Office 32位,那么您将很合适。 尝试将“平台输出”更改为x86。 转到项目属性,然后找到“构建”选项卡。 “平台目标”应在此处列出。

现在,即使可行,您也必须调查该决定的后果。 但是至少“您会知道”。

编辑 - - - - - - -

这是您的排列。 您只需要尝试一下。

  1. 连接字符串是对还是错。 如前所述,“ 12”与“ 14”。 (对不起,此链接是关于Excel的。请尝试使用“ TS”中的建议。)

  2. Office 32位正在安装。 我认为,如果您尝试在该计算机上安装“ AccessDatabaseEngine_x64.exe”,则会出现“ Office版本不正确”错误。

  3. 因此,由于#2,您必须安装“ AccessDatabaseEngine.exe”。 这是32位。

  4. “平台输出”。 现在我~~想想~~由于#3,您需要尝试将其设置为x86。

尝试将其放回x86,然后在连接字符串中尝试“ 12”对“ 14”。

编辑 - - - - - - - - -

我从互联网上拉了一个文件。

来自http://old.cba.ua.edu/~jomason/ac289/289AccessFiles.html的 Oren.accdb

然后我在机器上进行了编码。 而且有效。

  private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            using (OleDbConnection connect = new OleDbConnection())
            {
                connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\folder1\data\Oren.accdb;Persist Security Info=False;";
                connect.Open();

                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = connect;
                cmd.CommandText = "Select * from Agreement";

                StringBuilder sb = new StringBuilder();
                IDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    Console.WriteLine(reader[0].ToString());
                    sb.Append(string.Format("{0}, {1}", reader[0].ToString(), reader[1].ToString()) + System.Environment.NewLine);
                }
                reader.Close();

                ReportMessage(sb.ToString());

            }
        }
        catch (System.Data.OleDb.OleDbException lolex)
        {
            ReportException(lolex);
        }
        catch (Exception ex)
        {
            ReportException(ex);
        }

    }




  private void ReportException(Exception ex)
    {
        txtStatus.Text = ex.Message;
    }


    private void ReportException(System.Data.OleDb.OleDbException oleex)
    {

        StringBuilder sb = new StringBuilder();
        sb.Append(oleex.ErrorCode + System.Environment.NewLine);
        sb.Append(oleex.Message + System.Environment.NewLine );
        txtStatus.Text = sb.ToString();
    }

    private void ReportMessage(string msg)
    {
        txtStatus.Text = msg;
    }

编辑

您可以在程序“ Microsoft Access”中打开文件“ storekeeper.accdb”吗? 它不是受密码保护的吗?

您需要在连接字符串上的数据源路径上加上双斜杠,例如'Data Source = C:\\ folder1 \\ data \\ Oren.accdb; Persist Security Info = False;“;'

暂无
暂无

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

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