简体   繁体   English

使用 OleDbConnection.Open() 时 Access-DB(.accdb) 访问崩溃应用程序

[英]Access-DB(.accdb) access crash app when using OleDbConnection.Open()

I recently tried to use access database with C# code inside a little Revit plugin but it crash when I use OleDbConnection.Open()我最近尝试在一个小 Revit 插件中使用带有 C# 代码的访问数据库,但是当我使用 OleDbConnection.Open() 时它崩溃了

Here is my snippets:这是我的片段:

    CPFMainModelView mainModelView;
    static readonly string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\ThomasLECUPPRE(Letit\source\LIB_MainDB.accdb";

    public UserDBManager(CPFMainModelView cmmw)
    {
        mainModelView = cmmw;
        try
        {
            RetrieveprojectList();
        }
        catch (Exception ex)
        {
            mainModelView.Texte = $"{ex.Message}\n\n{ex.StackTrace}\n\n{ex.InnerException}\n\n{ex.Data}";
        }

    }

    public void RetrieveprojectList()
    {
        using (OleDbConnection con = new OleDbConnection(connectionString))
        {
            con.Open();
            OleDbCommand command = new OleDbCommand("SELECT Ref FROM FolderCategory", con);
            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                mainModelView.Texte += $"\n{reader["Ref"]}";
            }
        }
    }

Here is a view of my tiny db in access这是访问中我的小数据库的视图

What did I miss ?我错过了什么 ?

I have already see these thread : C# - OleDbConnection.Open() causing a crash Simple C# connection to .accdb file我已经看到这些线程: C# - OleDbConnection.Open() 导致崩溃简单的 C# 连接到 .accdb 文件

Before use "Provider=Microsoft.ACE.OLEDB.12.0" and build the solution for X86 only, I was using "Provider=Microsoft.Jet.OLEDB.4.0" and build solutino for Any CPU but this thread (in french sorry) tell to it another way.在使用“Provider=Microsoft.ACE.OLEDB.12.0”并仅为 X86 构建解决方案之前,我使用“Provider=Microsoft.Jet.OLEDB.4.0”并为任何 CPU 构建解决方案,但此线程(法语抱歉)告诉以另一种方式。

Thank you for help.谢谢你的帮助。

Install the missing dependancy安装缺少的依赖

SO post 所以帖子

On modern Windows this driver isn't available by default anymore, but you can download as Microsoft Access Database Engine 2010 Redistributable on the MS site.在现代 Windows 上,默认情况下此驱动程序不再可用,但您可以在 MS 站点上下载为Microsoft Access Database Engine 2010 Redistributable If your app is 32 bits be sure to download and install the 32 bits variant because to my knowledge the 32 and 64 bit variant cannot coexist.如果您的应用程序是 32 位,请务必下载并安装 32 位变体,因为据我所知,32 位和 64 位变体不能共存。

Depending on how your app locates its db driver, that might be all that's needed.根据您的应用程序如何定位其 db 驱动程序,这可能就是所需要的。 However, if you use an UDL file there's one extra step - you need to edit that file.但是,如果您使用 UDL 文件,还有一个额外的步骤 - 您需要编辑该文件。 Unfortunately, on a 64bits machine the wizard used to edit UDL files is 64 bits by default, it won't see the JET driver and just slap whatever driver it finds first in the UDL file.不幸的是,在 64 位机器上,默认情况下用于编辑 UDL 文件的向导是 64 位的,它不会看到 JET 驱动程序,而只是拍打它在 UDL 文件中首先找到的任何驱动程序。 There are 2 ways to solve this issue:有两种方法可以解决这个问题:

  1. start the 32 bits UDL wizard like this: C:\\Windows\\syswow64\\rundll32.exe "C:\\Program Files (x86)\\Common Files\\System\\Ole DB\\oledb32.dll",OpenDSLFile C:\\path\\to\\your.udl .像这样启动 32 位 UDL 向导: C:\\Windows\\syswow64\\rundll32.exe "C:\\Program Files (x86)\\Common Files\\System\\Ole DB\\oledb32.dll",OpenDSLFile C:\\path\\to\\your.udl Note that I could use this technique on a Win7 64 Pro, but it didn't work on a Server 2008R2 (could be my mistake, just mentioning)请注意,我可以在 Win7 64 Pro 上使用此技术,但它在 Server 2008R2 上不起作用(可能是我的错误,只是提及)
  2. open the UDL file in Notepad or another text editor, it should more or less have this format:在记事本或其他文本编辑器中打开 UDL 文件,它或多或少应该具有以下格式:

[oledb] ; Everything after this line is an OLE DB initstring Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Path\\To\\The\\database.mdb;Persist Security Info=False

That should allow your app to start correctly.这应该允许您的应用程序正确启动。

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

相关问题 C#-OleDbConnection.Open()导致崩溃 - C# - OleDbConnection.Open() causing a crash OLEDBConnection.Open() 生成“未指定错误” - OLEDBConnection.Open() generates 'Unspecified error' 如果Ole.DB提供程序在系统上可用,为什么OleDbConnection.Open()会抛出无法识别的数据库格式 - Why OleDbConnection.Open() throws Unrecognized database format if Ole.DB provider is available on the system 带参数的Oledb update-command在access-db上不起作用 - Oledb update-command with parameters not working on access-db 将文件转换为字节数组,保存在 Access-DB 中,从 DB 读取并创建文件 - Convert File To Byte-Array, Save In Access-DB, Read From DB And Create File 无法使用accdb检查Access中是否存在用户名 - failing to check if username exists in Access using accdb 为什么拒绝OleDbConnection访问? - Why is OleDbConnection access denied? OleDbConnection.Open()甚至在联系SQL Server之前都会引发“无可用错误消息”错误 - OleDbConnection.Open() throws a “No error message available” error before even contacting the SQL Server 使用OleDbConnection将数据提交到Access数据库时,在C#中调试“ INSERT INTO语句中的语法错误” - Debug “Syntax error in INSERT INTO statement” in C# when submitting data to Access Database using OleDbConnection MS Access / accdb“无法打开数据库”错误(C#) - MS Access/accdb “Cannot open database” error (C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM