简体   繁体   English

C#System.Data.OleDb.OleDbException

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

Hi am new to C# and am trying to connect to .accdb access 2010 database 嗨,我是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");
        }
    }
}

and I get this exception: 我得到这个异常:

A first chance exception of type System.Data.OleDb.OleDbException occurred in System.Data.dll System.Data.OleDb.OleDbException类型的第一次机会异常发生在System.Data.dll中

The database is not in use and the path is correct what do I do? 数据库未使用且路径正确我该怎么办?

There should be an InnerException property on the thrown exception which you can examine. 您可以检查引发的异常上的InnerException属性。 It will tell you what the exact error is. 它会告诉您确切的错误是什么。 To see it, you need to catch the exception, and then show the InnerException message: 要查看它,您需要捕获异常,然后显示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);
    }
}

There is additional example code for capturing and displaying the errors embedded in an OleDbException at the MSDN page for OleDbException . 在MSDN页面的OleDbException上还有捕获和显示嵌入在OleDbException中的错误的其他示例代码。

I think, this is simple. 我认为,这很简单。 Since you on the office 2010, I believe you need: Microsoft.ACE.OLEDB. 自从您在Office 2010中工作以来,我相信您需要:Microsoft.ACE.OLEDB。 14 .0 14 .0

Ok. 好。 If you have Office 32 bit on a 64 bit O/S, then you're gonna have fits. 如果您在64位O / S上具有Office 32位,那么您将很合适。 Try changing the "Platform Output" to x86. 尝试将“平台输出”更改为x86。 Go to your project properties and find the "Build" tab. 转到项目属性,然后找到“构建”选项卡。 "Platform target" should be listed there. “平台目标”应在此处列出。

Now, even if that works, you'll have to investigate the ramifications of that decision. 现在,即使可行,您也必须调查该决定的后果。 But at least "you would know". 但是至少“您会知道”。

EDIT-------------- 编辑 - - - - - - -

Here are your permutations. 这是您的排列。 And you're gonna just have to experiment with them. 您只需要尝试一下。

  1. The connection string, is it right or wrong. 连接字符串是对还是错。 "12" vs "14" as previously mentioned. 如前所述,“ 12”与“ 14”。 (Sorry, the link is about Excel. Try using the suggestion from "TS".) (对不起,此链接是关于Excel的。请尝试使用“ TS”中的建议。)

  2. Office 32 bit being installed. Office 32位正在安装。 I think if you tried to install "AccessDatabaseEngine_x64.exe" on that machine, it would give you a "Office version not right" error. 我认为,如果您尝试在该计算机上安装“ AccessDatabaseEngine_x64.exe”,则会出现“ Office版本不正确”错误。

  3. So because of #2, you gotta install "AccessDatabaseEngine.exe". 因此,由于#2,您必须安装“ AccessDatabaseEngine.exe”。 Which is 32 bit. 这是32位。

  4. The "Platform Output". “平台输出”。 Now I ~~think~~ because of #3, you need to experiment with setting it to x86. 现在我~~想想~~由于#3,您需要尝试将其设置为x86。

Try putting it back to x86, and then trying the "12" vs "14" in the connection string. 尝试将其放回x86,然后在连接字符串中尝试“ 12”对“ 14”。

EDIT----------------- 编辑 - - - - - - - - -

I pulled a file off the internet. 我从互联网上拉了一个文件。

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

And I coded this up on my machine. 然后我在机器上进行了编码。 And it works. 而且有效。

  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;
    }

EDIT 编辑

Can you open the file "storekeeper.accdb" in the program "Microsoft Access". 您可以在程序“ Microsoft Access”中打开文件“ storekeeper.accdb”吗? It's not password protected is it? 它不是受密码保护的吗?

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

暂无
暂无

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

相关问题 C#DBF文件错误System.Data.OleDb.OleDbException - C# DBF file error System.Data.OleDb.OleDbException C#System.Data.OleDb.OleDbException错误 - C# System.Data.OleDb.OleDbException Error System.Data.dll c# 中发生类型为“System.Data.OleDb.OleDbException”的未处理异常 - An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll c# System.Data.OleDb.OleDbException:“标准表达式中的数据类型不匹配。” C# 错误 - System.Data.OleDb.OleDbException: 'Data type mismatch in criteria expression.' C# error System.Data.OleDb.OleDbException:'条件表达式中的数据类型不匹配。 在C#中 - System.Data.OleDb.OleDbException: 'Data type mismatch in criteria expression. in c# c#窗口应用程序system.data.oledb.oledbexception操作必须使用可更新的查询 - c# window application system.data.oledb.oledbexception operation must use an updateable query C# 中的 SQL 查询(System.Data.OleDb.OleDbException:'查询表达式中的语法错误(缺少运算符)) - SQL query in C# (System.Data.OleDb.OleDbException: 'Syntax error (missing operator) in query expression) 选择查询C#中发生类型为'System.Data.OleDb.OleDbException'的未处理异常 - An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in Select query C# 如何修复'System.Data.OleDb.OleDbException'? - How to fix 'System.Data.OleDb.OleDbException'? C# - INSERT INTO 语句中的语法错误(System.Data.dll 中发生类型为“System.Data.OleDb.OleDbException”的未处理异常) - C# - Syntax error in INSERT INTO statement (An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM