Problem connecting to Firebird 3 embedded with C# in .NET

徘徊边缘 提交于 2019-12-02 05:42:51

I am able to reproduce this error with a simple application when it does not use the fbclient.dll with access to Firebird Embedded, but instead loads a fbclient.dll on the search path (eg in C:\Windows\System32 or C:\Windows\SysWoW64 if the application is 32 bits).

In that mode, fbclient tries to establish a 'local' connection to a Firebird instance through the XNET protocol (that is through xnet://Global\FIREBIRD). When no Firebird is running, this then fails.

In my attempts to reproduce this, I have found the following conditions can trigger this:

  • Only fbclient.dll is deployed in the folder of the application without the rest of Firebird Embedded:

    fbclient attempts to create a local connection through XNET, which fails.

  • The embedded engine (engine12.dll) is not present or in the wrong location. If fbclient.dll is at C:\path\to\firebird\fbclient.dll, then engine12.dll should be at C:\path\to\firebird\plugins\engine12.dll

    fbclient attempts to create a local connection through XNET, which fails.

    This is an explicit example of the previous item.

  • fbclient.dll is not deployed in the folder of the application:

    fbclient on the path (eg in Windows\System32 or SysWoW64) is used instead, which attempts to create a local connection through XNET, which fails.

    This case will trigger a DllNotFoundException instead when there is no fbclient.dll of the appropriate bitness on the path.

  • fbclient.dll + Firebird embedded have different bitness from application (eg x86 or AnyCPU application with a 64 bit Firebird, or a x64 application with a 32 bit Firebird)

    fbclient with appropriate bitness on the path (eg in Windows\System32 or SysWoW64) is used instead, which attempts to create a local connection through XNET, which fails.

    This case may trigger a BadImageFormatException instead when there is no fbclient.dll of the appropriate bitness on the path.

As an aside: under these conditions, if a Firebird instance is running, it will result in an error "connection lost to database" instead, probably because the Firebird ADO.net Provider does not supply a password as Firebird Embedded does not need it (although I haven't verified that), while a local connection does.

In short:

  • ensure that Firebird Embedded is correctly deployed together with your application, and
  • ensure that Firebird Embedded has the correct bitness for your application.

    That means, for C# x86 or AnyCPU: 32 bit, for x64: 64 bit. Or for AnyCPU, disable 'Prefer 32-bit' in the Program Properties > Build for Platform target: AnyCPU to ensure it loads 64 bit DLLs instead.

The files necessary for a working Firebird 3 Embedded deployment are (quoted from a blogpost I wrote about using Firebird Embedded from Java):

fb
|--intl
|  |--fbintl.conf
|  \--fbintl.dll
|--plugins
|  |--engine12.dll
|  |--fbtrace.dll
|  |--legacy_auth.dll
|  |--legacy_usermanager.dll
|  |--srp.dll
|  |--udr_engine.conf
|  \--udr_engine.dll
|--fbclient.dll
|--icudt52.dll
|--icudt52l.dll
|--icuin52.dll
\--icuuc52.dll

The fb folder is a trimmed down version of a normal Firebird installation. It is possible that some of the DLLs in the plugins folder are not necessary (this may require tweaking firebird.conf), and error logging suggests it might be necessary to include ib_util.dll as well, but the example program works without it. If you need additional configuration, then you can include a firebird.conf.

Contrary to what is shown in the quoted tree, only engine12.dll is really required in the plugins folder.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!