Problem connecting to Firebird 3 embedded with C#

2020-04-17 07:45发布

I am trying to connect to Firebird 3 embedded database from a .NET project. I have copied all the files of Firebird to the executing directory of the application. I have used the following connection string builder:

            var builder = new FbConnectionStringBuilder()
            {
                UserID = v1,
                Password = v2,
                Database = v3,
                ServerType = FbServerType.Embedded,
                Charset = "UTF8",
                ClientLibrary = "fbclient.dll" 
            };

However I get the following connection error

FirebirdSql.Data.FirebirdClient.FbException (0x80004005):
Unable to complete network request to host "xnet://Global\FIREBIRD".

Unable to complete network request to host "xnet://Global\FIREBIRD".

at FirebirdSql.Data.FirebirdClient.FbConnectionInternal.Connect()
at FirebirdSql.Data.FirebirdClient.FbConnectionPoolManager.Pool.CreateNewConnectionIfPossibleImpl(FbConnectionString connectionString)
at FirebirdSql.Data.FirebirdClient.FbConnectionPoolManager.Pool.GetConnection(FbConnection owner)
at FirebirdSql.Data.FirebirdClient.FbConnection.Open()
at DZApp.LoginForm.CheckPassword(String v1, String v2, String v3) in C:\Users\DZ\Desktop\DZApp\DZApp\LoginForm.cs:line 89

I know that the password is not necessary and have removed it but it doesn't work, what could be the problem?

1条回答
The star\"
2楼-- · 2020-04-17 08:28

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.

查看更多
登录 后发表回答