Search code examples
c#.netado.netfirebirdfirebird-embedded

How do I create a Firebird database file programmatically?


How do I create a Firebird database file programmatically?

I've written code using SQLite (C# .NET, System.Data.SQLite) ... decided to try Firebird.

To install Firebird: Windows 7 -> Visual Studio 2013 -> File -> New -> Project, etc. ---------- then Tools -> Library Package Manager -> Manage NuGet Packages -> search "firebird" -> Firebird ADO.NET Data provider -> Install (button).

Resulted in a) References -> FirebirdSql.Data.FirebirdClient, and b) FirebirdSql.Data.FirebirdClient.5.8.0 subdir ... with Firebird .dll's.

To create a Firebird database file, I've tried (extracted from another StackOverflow post):

int pageSize = 4096;
bool forcedWrites = true;
bool overwrite = false;
var connectionString = new FbConnectionStringBuilder
{
    Database = stPathFilename,
    ServerType = FbServerType.Embedded,
    UserID = "SYSDBA",
    Password = "masterkey",
    ClientLibrary = "fbclient.dll"
}.ToString();
FbConnection.CreateDatabase(connectionString, pageSize, forcedWrites, overwrite);

I had presumed this is standard, except for the stPathFilename. However, this code throughs an exception ... complains about fbclient.dll.

Then I tried

    ClientLibrary = "FirebirdSql.Data.FirebirdClient.dll" 

... which is located in my Debug subdir.

This throws an exception ... "Unable to find an entry point named 'isc_create_database' in DLL 'FirebirdSql.Data.FirebirdClient.dll".

How do I create a Firebird database file programmatically?


Solution

  • You are missing a crucial point, the Firebird ADO.net provider is not like System.Data.SQLite: it is not a Firebird database engine, it is only a client to connect to a Firebird server, by default through a pure C# implementation of the Firebird TCP/IP wire protocol, alternatively using the native client or to an embedded 'server' (a native client API that talks directly to an in-process database engine).

    The first option has no dependencies, but the second and third option have native dependencies that you need to provide yourself (it is not part of FirebirdSql.Data.FirebirdClient).

    For native you need fbclient.dll, for Firebird Embedded you need Firebird Embedded: for Firebird 2.5 and earlier: fbembed.dll and dependencies, for Firebird 3 fbclient.dll + plugins\engine12.dll and dependencies.

    There is little wrong with your code itself, but to get it running you need to do the following to get Firebird Embedded:

    1. Download the latest Firebird 3 zip kit (use 32 bit for a 32-bit or AnyCPU application, 64 bit for a 64-bit application).

      At the time of writing the latest version is Firebird 3.0.1. You can get it from the Firebird 3 download page, look for "Zip kit for manual/custom installs" under Win32 or Win64.

    2. Unzip it in a folder (for example D:\Temp\fb3embedded)
    3. You can keep all the files, but you can delete most of them as they are not necessary for Firebird Embedded (the zipkit is also a full Firebird server). You should retain the following:
      • intl folder and all contents (necessary for character set and collation support)
      • plugins\engine12.dll (the database engine itself, other files in plugins may be deleted, but this may restrict some features)
      • fbclient.dll (main entrypoint for the embedded engine)
      • firebird.msg (contains error messages)
      • icu* (necessary for character set and collation support)
      • You may need to retain the msvcp100.dll and msvcr100.dll if not already installed (but it usually is on most recent Windows systems)

    Technically you can also remove intl, but that will limit character set support.

    In your code, instead of ClientLibrary = "fbclient.dll" specify ClientLibrary = @"D:\Temp\fb3embedded\fbclient.dll", and it will work. For a deployed application, you can also put Firebird Embedded inside your application folder and use a relative path.

    For Firebird 2.5 and earlier, the instructions are similar, but instead you download the Embedded zip kit of that version, unzip it in a folder, and point ClientLibrary to the fbembed.dll (and not fbclient.dll) in that folder.

    Just be aware that FbConnection.CreateDatabase will throw an exception if the database already exists and you have specified overwrite as false.