1

I am trying to connect to an Embedded firebird database using a .Net 8.0 application.

I create a connection string to an existing database as follows (in server mode) :

datasource=localhost;database=C:/Db/Test/MyDB.FDB;user id=SYSDBA;password=pwd;port number=3050;dialect=3;pooling=True;server type=0;character set=UTF8;connection lifetime=15;min pool size=0;max pool size=50;packet size=8192;wire crypt=Enabled;client library=fbclient.dll

here all works fine. Then, I simply replace the server type from 0 to 1 and perform some additional tweaks to connect :

FbConnectionStringBuilder conn = new FbConnectionStringBuilder(_connectionStr);
// next line basically to ensure I'm using Embedded mode...
conn.ServerType = FbServerType.Embedded;
// --- test connection - in embedded mode, should not have DataSource nor password
if (conn.ServerType == FbServerType.Embedded) {
    conn.ClientLibrary = "fbclient.dll";
    conn.DataSource = "";
    conn.UserID = "SYSDBA";
    conn.Password = "";
}
else {
    conn.ClientLibrary = "fbclient.dll";
    conn.WireCrypt = FbWireCrypt.Enabled;
}
try {
    using (FbConnection db = new FbConnection(conn.ConnectionString)) {
        db.Open();
        db.Close();
}

When the Firebird service is still running locally on the machine, I get the following error:

Your user name and password are not defined. Ask your database administrator to set up a Firebird login

So I stop the service - then I make sure I add all necessary files (as described e.g. here). I then get another error:

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

As suggested, I made sure my program and dlls are the same bitness (I tried both AnyCPU or x64, knowing I've downloaded the x64 files) - nothing seems to help.

I even tried to enable legacy authentication modes and so on, but none of my attempts helped. Is it at all possible to connect in embedded mode in .Net 8.0?

9
  • The error means its trying to create a connection to the server, not to Firebird embedded. Which fbclient.dll is it trying to load, and does that fbclient.dll have access to plugins/engine12.dll (Firebird 3.0 embedded), or plugins/engine13.dll (Firebird 4.0 embedded)? Commented May 16, 2024 at 14:58
  • That is, your configuration suggest it has a fbclient.dll in the application directory, if that is the case, it must also have a plugins/engineXX.dll in your application directory (and possibly some additional supporting libraries). It can also mean it is loading a different fbclient.dll than you expect because it is in the wrong location compared to the built executable (and for example loads the client-only fbclient.dll in %WINDIR%\System32). Commented May 16, 2024 at 15:02
  • @MarkRotteveel I test the file exists AND try to load the dll via code as Assembly.LoadFile("fbclient.dll"); (I tested the file exsits). I get Bad IL format. error although both my application and the library are in x64... I even tested to load the fbclient.dll located in the Program Files folder - same result! Commented May 16, 2024 at 19:30
  • 2
    Stefan's project is built for "any CPU" which means that it produces 32 bits executable. This executable cannot use 64 bits libraries you downloaded and most likely used 32 bits fbclient.dll found somewhere else. Commented May 17, 2024 at 11:53
  • 1
    @user13964273 I believe Microsoft changed the rules again, because my .NET 8 example application compiled as AnyCPU ran as a 64-bit process, and thus was able to load the 64-bit libraries. Commented Jun 1, 2024 at 7:52

2 Answers 2

3

This documents the steps I did in Visual Studio 2022 to get Firebird Embedded in a .NET 8 application.

  1. Created a C# Console App, with .NET 8

  2. Added FirebirdSql.Data.FirebirdClient version 10.2.0 (latest at this time) with NuGet

  3. First I wrote a simple program and ran it against a Firebird server (Firebird 5.0 in my case):

    using FirebirdSql.Data.FirebirdClient;
    
    var conStringBuilder = new FbConnectionStringBuilder();
    conStringBuilder.UserID = "sysdba";
    conStringBuilder.Password = "masterkey";
    conStringBuilder.Database = @"inet://localhost/C:\db\fb5\fb5testdatabase.fdb";
    
    string conString = conStringBuilder.ToString();
    Console.WriteLine(conString);
    
    using (var connection = new FbConnection(conString))
    {
        connection.Open();
        using var command = new FbCommand("""select CURRENT_USER as "USER" from RDB$DATABASE""", connection);
        using var reader = command.ExecuteReader();
    
        if (reader.Read())
        {
            Console.WriteLine(reader["USER"]);
        }
    }
    
  4. Then I stopped the Firebird server and set the ServerType to Embedded and the Database to the local path:

    var conStringBuilder = new FbConnectionStringBuilder();
    conStringBuilder.ServerType = FbServerType.Embedded;
    conStringBuilder.UserID = "sysdba";
    conStringBuilder.Password = "masterkey";
    conStringBuilder.Database = @"C:\db\fb5\fb5testdatabase.fdb";
    

    As expected, this fails because the fbembed.dll cannot be loaded.

  5. If I just enter

    conStringBuilder.ClientLibrary = "fbclient.dll";
    

    I get the same error you did ("Unable to complete network request to host "xnet://Global\FIREBIRD".'"), because it loads the fbclient.dll from %WINDIR%\System32, which has no Embedded component.

  6. If I point it to the one in the directory with my Firebird 5.0 server installation (which can also serve as a Firebird Embedded), it works:

    conStringBuilder.ClientLibrary = @"C:\Firebird\Firebird-5.0.0.1306-0-windows-x64\fbclient.dll";
    

In short, your problems seems to stem from loading a different fbclient.dll than you think you're loading, and thus not have any access to Firebird Embedded.

If you want to have the Firebird libraries included in your project, and be able to reference fbclient.dll with a relative path, you need to make sure it and its dependencies are in your output directory.

For example, add a folder firebird-embedded in your solution and put the Firebird files in there. That is, basically, the intl, plugins and tzdata folders, and firebird.msg and all DLL files from the Firebird installation directory; this may include some unneeded files, but it will work.

Then edit the solution to include this directory in the output directory:

Change from

  <ItemGroup>
    <Folder Include="firebird-embedded\" />
  </ItemGroup>

to

  <ItemGroup>
    <Content Include="firebird-embedded\**">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

Finally, change the ClientLibrary setting to point to the relative path firebird-embedded\fbclient.dll:

conStringBuilder.ClientLibrary = @"firebird-embedded\fbclient.dll";

When you now compile and run the application, it will pick up the fbclient.dll and the Firebird Embedded files from firebird-embedded in the output directory, and work successfully.

Sign up to request clarification or add additional context in comments.

Comments

1

I don't have much experience with Firebird, however, I assume that you mean the Embedded version as described here: Firebird embedded doc, thus you don't need a dedicated local Firebird server running at all (the "embedded kit" does all the job by itself instead).

I have created a simple .NET8 console app that you can find here (all the required DLLs are already there, so its enough to simply clone, build and run).

... possible to connect in embedded mode in .Net 8.0

So it seems to be possible. Maybe you can try running the example with your database file?


Edit: I have added Firebird 4.0 branch and tried to remove all the unnecessary files, leaving only those that are required for embedded mode. I assume the same approach can be applied to Firebird 5.0, the files can be obtained here - just make sure to grab the correct kit (32 or 64 as @user13964273 mentioned).

4 Comments

Thx for your example - it seems you're using Firebird 3.0 - which I managed to get working too - did you manage the same with Firebird 4.0?
@neggenbe I've created a FireBird 4.0 branch and tried to remove all the unnecessary files, leaving only those that are required for embedded mode.
Works great within your app, thanks so much. Now I still have the issue in my Blazor app - so I think the problem is related to where the files are copied or, in this case, NOT copied properly... I'll give it a try to determine how to make them available in the correct folder!
Ok, got it working when running with the "App" config, but not with IIS - which seems ok in my case as if I use IIS, I'd have a running DB server - I won't use embedded mode!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.